design patterns - Repositories / Lazy Loading / Persistance -
i have trouble repository pattern. or maybe unclear points. sake of these questions have simple example domain 2 entity aggregates.
public class category { string name {get;set;} category parent {get;set;} ilist<category> children {get;set;} } public class transaction { category owner {get;set;} string name ... bla bla } in particular domain model these 2 not form single aggregate. such, have 2 standard irepository implementations each of 2 entities.
question one: when dealing validation, such deletion of category, repository needs relational checks. common practise repositories talk (and such injected references) other repositories , generate errors, keeping neatly disconnected other layers or delegate database layer?
question two: similarly, when updating entity (aggregate) save/change/delete validation needs done on various components. presumably reasonably doable when have unit of work buffers changes , commits when validation succeed?
question three: loading single category results in entire object tree loaded when not using lazy loading. limit real option implement lazy loading and/or change tracking in entities?
for 3 questions recomend implement unitofwork pattern. ususaly implement iunitofwork part of repositories. when want work more 1 of repositories inject (the same) unitofwork want work (that 1 of ways prefer). when work done call commit() on unitofwork savechanges on entity frameworks context. unitofwork wrapper around entity frameworks context.
question one: relational checks done database. if fails entity framework throw exception , should handle in higher app layer, lets call service layer work unitofwork.
question two: use unitofwork pattern.
question three: should use lazy loading if dont want load 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 simle implementation of unitofwork pattern started(its bit different described unitofwork contains repositories mine other way around...).
Comments
Post a Comment