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
Printed Date: 23-Feb-2020 at 3:48am

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

Posted By: mgood
Date Posted: 24-Apr-2012 at 12:26pm
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