Print Page | Close Window

Adding optional includes on Repository<> WithIdAsync() method

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3403
Printed Date: 17-Nov-2019 at 9:39pm


Topic: Adding optional includes on Repository<> WithIdAsync() method
Posted By: JohnBloom
Subject: Adding optional includes on Repository<> WithIdAsync() method
Date Posted: 24-Apr-2012 at 10:46am

I really like the WithIdAsync function on the generic repository class. It has saved me from writing a bunch of little functions to load items by id. I noticed that FindAsync has the ability to add includes to it. There is often times when we want to include something when we load one item as well. Is this possible to do now or is this a feature request*?

*I realize that this question refers to the contrib library and if it is a feature I might be willing to jump in there and figure it out. As of now I have been head down moving through our transition to Cocktail** so I havent had time to figure the whole Git thing out.


**As a futher aside we just released our changes to our customers! Cocktail FTW!



-------------
-John Bloom



Replies:
Posted By: mgood
Date Posted: 24-Apr-2012 at 12:26pm
John,
The idea behind WithIdAsync is that it always fetches the entity graph consistently per UoW type. In your specifc UoW you can use a subclass of Repository<T> and redefine the query you want it to use by overriding the GetKeyQuery method. I've done that in TempHire for example.
 
In places where you need to retrieve an entity by ID with a non-standard query, you would use FindAsync instead or actually define a different UoW type configured with a repository serving the purpose for that area of the application. Don't be shy about creating different UoW types optimized for the various areas and workflows of your application.
 
    public class StaffingResourceRepository : Repository<StaffingResource>
    {
        public StaffingResourceRepository(IEntityManagerProvider<TempHireEntities> entityManagerProvider)
            : base(entityManagerProvider)
        {
        }
 
        public new TempHireEntities EntityManager
        {
            get { return (TempHireEntitiesbase.EntityManager; }
        }
 
        protected override IEntityQuery GetKeyQuery(params object[] keyValues)
        {
            return EntityManager.StaffingResources
                .Where(r => r.Id == (Guid) keyValues[0])
                .Include(r => r.Addresses)
                .Include(r => r.PhoneNumbers);
        }
    }
 


Posted By: mgood
Date Posted: 24-Apr-2012 at 12:27pm
Oh BTW, congrats on the release.



Print Page | Close Window