This is a migrated thread and some comments may be shown as answers.

Extending a context collection

13 Answers 85 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
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?

13 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 16 Sep 2010, 04:03 PM
Hello Steve,

 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            
        }
    }
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:
static void Main(string[] args)
        {
            NorthwindEntityDiagrams db = new NorthwindEntityDiagrams();
            Customer defCustomer = db.GetFirstOrDefaultCustomer();
        }
I hope that my answer was useful to you.


Sincerely yours,
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)

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
Damyan Bogoev
Telerik team
answered on 17 Sep 2010, 04:04 PM
Hello Steve,

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
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
0
Damyan Bogoev
Telerik team
answered on 21 Oct 2010, 05:51 PM
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 

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
Dimitar Kapitanov
Telerik team
answered on 03 Jan 2011, 08:09 AM
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
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
0
Damyan Bogoev
Telerik team
answered on 03 Jan 2011, 10:53 AM
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:

OpenAccessContextBase context = OpenAccessContextBase.GetContext(persistentObject);

Hope that helps.

Kind regards,
Damyan Bogoev
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
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
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
Damyan Bogoev
Telerik team
answered on 03 Jan 2011, 03:34 PM
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
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
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
Damyan Bogoev
Telerik team
answered on 03 Jan 2011, 03:57 PM
Hi Steve,

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
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Tags
Development (API, general questions)
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
PetarP
Telerik team
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Damyan Bogoev
Telerik team
Dimitar Kapitanov
Telerik team
Share this question
or