This question is locked. New answers and comments are not allowed.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve
asked on 15 Sep 2010, 03:17 PM
I can't find the documentation on extending the context to add extension methods...I have a user table and I want to make some custom extension methods to get me a specific person instead of FirstOrDefaulting every time.
Cant that be done?
Cant that be done?
13 Answers, 1 is accepted
0
Hello Steve,
The above class will extend my context with the GetFirstOrDefaultCustomer. So the next time I create an instance of NorthwindEntityDiagrams I will have that method available:
I hope that my answer was useful to you.
Petar
the Telerik team
Extending of the context can be done as you would normally extend any other partial class. So for example if my context class is named NorthwindEntityDiagrams I can have a class like this:
public
partial
class
NorthwindEntityDiagrams
{
public
Customer GetFirstOrDefaultCustomer()
{
//Your custom code
}
}
static
void
Main(
string
[] args)
{
NorthwindEntityDiagrams db =
new
NorthwindEntityDiagrams();
Customer defCustomer = db.GetFirstOrDefaultCustomer();
}
Petar
the Telerik team
Do you want to have your say when we set our development plans?
Do you want to know when a feature you care about is added or when a bug fixed?
Explore the
Telerik Public Issue Tracking
system and vote to affect the priority of the items
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 16 Sep 2010, 05:12 PM
Yeah, no that I get, and do already...
I want to extend using the collections themselves (if that makes sense)
So then I wouldn't have to pass in the context or make my own in the method...just give it the ID, and I get back the object using the existing context.
I want to extend using the collections themselves (if that makes sense)
Model context =
new
Model();
var student = context.Students.Get(
"43EDBBCA-D76B-44C0-BCF5-87A5443B93F1"
);
So then I wouldn't have to pass in the context or make my own in the method...just give it the ID, and I get back the object using the existing context.
0
Accepted
Hello Steve,
You can define an extension method the following way:
Hope that helps.
Sincerely yours,
Damyan Bogoev
the Telerik team
You can define an extension method the following way:
public
static
class
Extensions
{
public
static
Student Get(
this
IQueryable<Student> students,
string
studentId)
{
return
students.FirstOrDefault(st => st.ID.Equals(studentId));
}
}
Hope that helps.
Sincerely yours,
Damyan Bogoev
the Telerik team
Do you want to have your say when we set our development plans?
Do you want to know when a feature you care about is added or when a bug fixed?
Explore the
Telerik Public Issue Tracking
system and vote to affect the priority of the items
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 17 Sep 2010, 04:35 PM
That is EXACTLY what I needed :D
Thank you so much
Thank you so much
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 18 Oct 2010, 06:48 PM
Hey Guys,
Would you be able to tell me how to .SaveChanges in one of these extension methods? I have a method to sort the items in my collection, but I'm not sure how to persist those changes...
Steve
Would you be able to tell me how to .SaveChanges in one of these extension methods? I have a method to sort the items in my collection, but I'm not sure how to persist those changes...
Steve
0
Hello Steve,
I am afraid that currently you are unable to achieve this goal. In order to do that you should be able to retrieve the OpenAccessContext instance which manages the corresponding persistent object similar to the
approach. We will do our best to implement this feature for the next Q3 release.
I am sorry for the inconvenience caused.
Best wishes,
Damyan Bogoev
the Telerik team
I am afraid that currently you are unable to achieve this goal. In order to do that you should be able to retrieve the OpenAccessContext instance which manages the corresponding persistent object similar to the
Database.GetContext(instance)
as
IObjectScope
approach. We will do our best to implement this feature for the next Q3 release.
I am sorry for the inconvenience caused.
Best wishes,
Damyan Bogoev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 03 Jan 2011, 02:37 AM
Did this make Q3 by any chance?
0
Hi Steve,
We were not able to incorporate all the existing API into the context API. We are targeting that for Q1 2011 but my estimates are that we will able to roll out some of it in an intermediate builds before the Q1.
Regards,
Dimitar Kapitanov
the Telerik team
We were not able to incorporate all the existing API into the context API. We are targeting that for Q1 2011 but my estimates are that we will able to roll out some of it in an intermediate builds before the Q1.
Regards,
Dimitar Kapitanov
the Telerik team
0
Hi Steve,
Actually you could retrieve the OpenAccessContextBase instance that manages a persistent object using the OpenAccessContextBase.GetContext method that we introduced in Q3 release:
Hope that helps.
Kind regards,
Damyan Bogoev
the Telerik team
Actually you could retrieve the OpenAccessContextBase instance that manages a persistent object using the OpenAccessContextBase.GetContext method that we introduced in Q3 release:
OpenAccessContextBase context = OpenAccessContextBase.GetContext(persistentObject);
Hope that helps.
Kind regards,
Damyan Bogoev
the Telerik team
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 03 Jan 2011, 02:48 PM
Hmmm, okay so in the case of the example below
I would have to do a FirstOrDefault, get an object, then get the context using this?
Would I then cast this to be my Models Context?
Does this mean I'll always have to make a dummy call to the DB to get a persistant object? Or can I just use the "students" variable somehow to grab the context.
public
static
class
Extensions
{
public
static
Student Get(
this
IQueryable<Student> students,
string
studentId)
{
return
students.FirstOrDefault(st => st.ID.Equals(studentId));
}
}
I would have to do a FirstOrDefault, get an object, then get the context using this?
AccessContextBase context = OpenAccessContextBase.GetContext(persistentObject);
Would I then cast this to be my Models Context?
Does this mean I'll always have to make a dummy call to the DB to get a persistant object? Or can I just use the "students" variable somehow to grab the context.
0
Hi Steve,
1. I am afraid that the OpenAccessContextBase.GetContext method can be used only with single instance not with a collection of persistent objects.
2. It depends on the scenario. You could cast the context to Model only for read operations to be able to work with the corresponding IQueryable collection. Casting is not needed in case of create, delete and update operations.
Hope that helps. If any other questions arise please contact us back.
Regards,
Damyan Bogoev
the Telerik team
1. I am afraid that the OpenAccessContextBase.GetContext method can be used only with single instance not with a collection of persistent objects.
2. It depends on the scenario. You could cast the context to Model only for read operations to be able to work with the corresponding IQueryable collection. Casting is not needed in case of create, delete and update operations.
Hope that helps. If any other questions arise please contact us back.
Regards,
Damyan Bogoev
the Telerik team
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 03 Jan 2011, 03:45 PM
I don't think at the moment I need to do any CRUD as much as just query other collections in the model to return the proper data.
0
Hi Steve,
In this case you should cast the OpenAccessContextBase instance to Model to be able to use the IQueryable endpoints:
Hope you will find the provided information useful.
Greetings,
Damyan Bogoev
the Telerik team
In this case you should cast the OpenAccessContextBase instance to Model to be able to use the IQueryable endpoints:
Model context = OpenAccessContextBase.GetContext(persistentObject)
as
Model;
if
(context !=
null
)
{
var persistentCapableObject = context.EndpointName.FirstOrDefault(filter);
...
}
Hope you will find the provided information useful.
Greetings,
Damyan Bogoev
the Telerik team