Questions regarding Entity Framework + DDD

It's hard for me to find direct examples of using EF in a DDD style template. This is also my first time using DDD and have a few questions regarding the layout of the solution and how to use some of the DDD templates.

1) Most of the examples that I saw regarding the use of the w / EF repository template just show specialized model interfaces, such as IContactRepository, and then a specific type that implements the interface. Ideally, I would like to use something like IRepository, which has a basic set of functions for CRUD operations. Then I could create specialized repositories if necessary, for example IContactRepository: IRepository, when necessary, since most of my models do not need to be expanded. Am I barking the wrong tree? Can someone provide me with examples of this implementation style?

2) Now I have my solutio, divided into the following three projects: Models (contains my EDM), Repositories and Services. Is this suitable or is there another approach to the layout that I am not considering and should be?

3) I saw examples of repositories having Query (Func <T>) / Query () methods that return IQueryable. Is it smelly or something frowned?

+3
source share
3 answers

I would like to answer # 3 ...

I think this is less "smelly" and more "lazy." Here is a typical "repository" that I saw around the internet ...

public interface IRepository {
  // Query operations.
  IQueryable<T> All<T>();
  IQueryable<T> Find<T>(Expression<Func<T, bool>> expression);
  T Single<T>(Expression<Func<T, bool>> expression);

  // Save operations.
  T Add<T>(T objectToAdd);
  void Delete<T>(T objectToDelete);
  T Update<T>(T objectToUpdate);
}

To my knowledge, this is a smaller repository and more of a “session” or “unit of work”. This is a convenient way to abstract from the database technology used and just talk with an extremely common interface. So rename it to ISession. This is a sample I made recently.

public class PeopleRepository {
  private readonly ISession session;

  public PeopleRepository(ISession session) {
    this.session = session;
  }

  public virtual IEnumerable<Person> Active() {
    return session.Find<Person>(p => p.Active).OrderBy(p => p.LastName).ThenBy(p => p.FirstName);
  }

  public virtual IEnumerable<Person> ByLastName(string name) {
    return session.Find<Person>(p => p.Active && p.LastName.StartsWith(lastName)).OrderBy(p => p.LastName).ThenBy(p => p.FirstName);
  }

  public virtual void DeletePerson(int personId) { 
    // We don't really delete people; we mark them as inactive.
    var person = session.Single<Person>(p => p.Id == personId);
    person.Active = false;
    session.Update(person);
  }
}

ISession . , PersonRepository , Person.

, .

+2

EF DDD, , EF . , , EF, , "Entity" , EF.

, . DDD , , . , , .

: EF 'Entities' , DAL , EF 'Entities'. .

, IQueryable , DDD. , , "".

EF .NET 4.0 Persistence Notorance, ...

+2
0
source

Source: https://habr.com/ru/post/1719443/


All Articles