How could I access special functions baked into a Custom DbContext through
a Generic DbContext which lives within a Generic Repository?
Here is my set up thus far:
Database-First Entity Framework (generates a EDMX)
Separated POCOs into a different project
Left Custom DbContext under EDMX to be used as a bounded Countext
Created a UOW
Created a generic Repository (IRepository)
Mapped each entity into the UOW through concrete implementations of
IRepository
UOW instantiates the Custom DbContext created by the EDMX and passes it
along to each Repository
So far all is well.
Now, the problem stems out of that last part. The UOW instantiates the
Custom DbContext, injects it into the Repositories and everything works...
for the most part.
Each Repository implementation takes the DbContext, creates a DbSet and
exposes the usual CRUD operations. All Entities are accessible through
generics, so if I want to implement say GetAll() I will simply return
DbSet and it will already be mapped to the Entity .
BUT, when I try to access a Function Import from DbContext within the
Repository... I CAN'T.
It makes sense that I can't of course: the generic repository takes a
DbContext as input, but it knows nothing of the Custom DbContext created
by the EDMX, thus, all such functions added into the Custom DbContext are
not known to the DbContext within the repository.
In other words:
I can access the Function Imports from the Custom DbContext while in the UOW
I pass the Custom DbContext to the constructor of each Entity Repository
The Repository expects any class that derives from DbContext, so the
Custom DbContext does the trick
When the Repository tries to access the Function Import through the
DbContext is has access to, it can't, because it has no knowledge of them
Obviously I can't just use the Custom DbContext everywhere, because I
would be marrying the Repository to a specific Custom DbContext, and I'll
be needing more than one since I'm created several Bounded Context.
Alas, THE QUESTION: How could I call the Function Import from within the
Repository without marrying it to a specific Custom DbContext?
Workarounds:
I know I could use reflection, but I'm trying to stir away from it for
performance reasons (I know it is not that terrible, but still... the
present question is about finding out a better way).
I have managed to get what I needed using DbContext.SqlQuery() to execute
the Stored Procedure (which was mapped to the Function Import in the
EDMX). Yet again, since I can easily swap Function Imports in the EDMX, I
would like to find a way to access it within the Repository.
Hope it all makes sense. I appreciate any light anyone can shed onto the
matter.
No comments:
Post a Comment