I'm using Domain driven N-layered application architecture with EF code first
in my recent project, I defined my Repository
contracts, In Domain
layer.
A basic contract to make other Repositories
less verbose:
public interface IRepository<TEntity, in TKey> where TEntity : class
{
TEntity GetById(TKey id);
void Create(TEntity entity);
void Update(TEntity entity);
void Delete(TEntity entity);
}
And specialized Repositories
per each Aggregation root
, e.g:
public interface IOrderRepository : IRepository<Order, int>
{
IEnumerable<Order> FindAllOrders();
IEnumerable<Order> Find(string text);
//other methods that return Order aggregation root
}
As you see, all of these methods depend on Domain entities
.
But in some cases, an application's UI
, needs some data that isn't Entity
, that data may made from two or more enteritis's data(View-Model
s), in these cases, I define the View-Model
s in Application layer
, because they are closely depend on an Application's
needs and not to the Domain
.
So, I think I have 2 way's to show data as View-Models
in the UI
:
- Leave the specialized
Repository
depends onEntities
only, and map the results ofRepositories
's method toView-Models
when I want to show to user(inApplication Layer
usually). - Add some methods to my specialized
Repositories
that return their results asView-Models
directly, and use these returned values, inApplication Layer
and thenUI
(these specializedRepositories
's contracts that I call themReadonly Repository Contract
s, put inApplication Layer
unlike the otherRepositories
'e contract that put inDomain
).
Suppose, my UI
needs a View-Model
with 3 or 4 properties(from 3 or 4 big Entities
).
It's data could be generate with simple projection, but in case 1, because my methods could not access to View-Models
, I have to fetch all the fields of all 3 or 4 tables with sometimes, huge joins, and then map the results to View-Models
.
But, in case 2, I could simply use projection and fill the View-Model
s directly.
So, I think in performance point of view, the case 2 is better than case 1. but I read that Repository
should depend on Entities
and not View-Models
in design point of view.
Is there any better way that does not cause the Domain
Layer depend on the Application layer
, and also doesn't hit the performance? or is it acceptable that for reading queries, my Repositories
depend on View-Models
?(case2)
string text
) into LINQ statements that return domain objects or projections from entities. – Reinsure