Extending a context collection

14 posts, 1 answers
  1. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 15 Sep 2010 Link to this post

    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?
  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 16 Sep 2010 Link to this post

    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
  3. DevCraft banner
  4. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 16 Sep 2010 Link to this post

    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.
  5. Answer
    Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 17 Sep 2010 Link to this post

    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
  6. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 17 Sep 2010 Link to this post

    That is EXACTLY what I needed :D

    Thank you so much
  7. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 18 Oct 2010 Link to this post

    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
  8. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 21 Oct 2010 Link to this post

    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
  9. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 02 Jan 2011 Link to this post

    Did this make Q3 by any chance?
  10. Dimitar Kapitanov
    Admin
    Dimitar Kapitanov avatar
    632 posts

    Posted 03 Jan 2011 Link to this post

    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.
  11. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 03 Jan 2011 Link to this post

    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.
  12. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 03 Jan 2011 Link to this post

    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.
  13. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 03 Jan 2011 Link to this post

    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.
  14. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 03 Jan 2011 Link to this post

    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.
  15. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 03 Jan 2011 Link to this post

    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.
Back to Top
DevCraft banner