开发者

Repositories / Lazy Loading / Persistance

开发者 https://www.devze.com 2023-02-24 17:13 出处:网络
I have some trouble with the repository pattern. Or maybe just some unclear points. For sake of these questions I have a simple example domain with two entity aggregates.

I have some trouble with the repository pattern. Or maybe just some unclear points. For sake of these questions I have a simple example domain with two entity aggregates.

public class Category
{
  string Name {get;set;}
  Category Parent {get;set;}
  IList<Category> Children {get;set;}
}

public class Transaction
{
  Category Owner {get开发者_C百科;set;}
  string Name
  ... bla bla
}

In this particular Domain model these two do not form a single aggregate. As such, I have two standard IRepository implementations for each of the two entities.

Question One: When dealing with validation, such as deletion of a category, a repository needs to do relational checks. Is it a common practise that repositories talk (and as such are injected with references) to other repositories and generate errors, thus keeping it neatly disconnected from the other layers or do they delegate this to the database layer?

Question Two: Similarly, when updating an entity (aggregate) often save/change/delete validation needs to be done on various components. Presumably this is only reasonably doable when you have a unit of work that buffers changes and only commits when all validation succeed?

Question Three: Loading a single category results in an entire object tree to be loaded when not using lazy loading. To limit this the only real option is to implement lazy loading and/or change tracking in your entities?


For all your three questions I would recomend you to implement UnitOfWork pattern. I ususaly implement IUnitOfWork that is a part of my repositories. When I want to work with more then one of my repositories I inject (the same) UnitOfWork into all I want to work with (That is just one of the ways that I prefer). Then when all work is done I call Commit() on my UnitOfWork which does savechanges on entity frameworks context. UnitOfWork is wrapper around entity frameworks context.

Question one: Relational checks are done by database. If something fails entity framework will throw exception and you should handle it in you higher app layer, lets call it service layer where you work with your UnitOfWork.

Question two: Use UnitOfWork pattern.

Question three: You should use lazy loading if you dont want to load the whole object tree.

Repository:

    public interface IRepository<T>
{
    IUnitOfWork UnitOfWork { get; set; }
    IQueryable<T> All();
    void Delete(T item);
    void Save(T item);
    void Update(T item);
    IQueryable<T> Find(Func<T, bool> expression);
    void Attach(T item);        
}

UnitOfWork:

    public interface IUnitOfWork : IDisposable
{
    ObjectContext Context { get; set; }
    void Commit();
    bool LazyLoadingEnabled { get; set; }
    bool ProxyCreationEnabled { get; set; }
    string ConnectionString { get; set; }
}

OR here is very simle implementation of UnitOfWork pattern to get you started(Its a bit different from what i described UnitOfWork contains Repositories mine is the other way around...).

0

精彩评论

暂无评论...
验证码 换一张
取 消