c# - Referencing Entity Framework in MVVM in layered architecture -


my app layered in 4 layers:

  • core: put general , interface classes.
  • model: code-first classes, , other related domain, such entity configurations , repositories etc.
  • vm: view models live. referencing model.
  • desktop: desktop app lives. referencing vm.

i have installed entity framework model, viewmodel , desktop.

my question is: enough install model only? why repeat again?

[edit]

  • here implementation of repository , unitofwrok ( irepository , repository on core):

    public interface irepository<tentity> tentity : class {    tentity get(int id);    ienumerable<tentity> getall();    ienumerable<tentity> find(expression<func<tentity, bool>> predicate);     tentity singleordefault(expression<func<tentity, bool>> predicate);     void add(tentity entity);    void addrange(ienumerable<tentity> entities);     void remove(tentity entity);    void removerange(ienumerable<tentity> entities); }  public class repository<tentity> : irepository<tentity> tentity : class {    protected readonly dbcontext context;     public repository(dbcontext context)    {        context = context;    }     public tentity get(int id)    {        return context.set<tentity>().find(id);    }     , on... } 
  • now, new next interfaces , classed on model

    public interface icountryrepository : irepository<country> {}  public class countryrepository : repository<country>, icountryrepository {     public countryrepository(cmsdbcontext cmsdbcontext)         : base(cmsdbcontext) {} }  interface iunitofwork : idisposable {     icountryrepository countryrepository { get; } }  public class unitofwork : iunitofwork {     private readonly cmsdbcontext _context;      public unitofwork(cmsdbcontext context)     {         _context = context;         countryrepository = new countryrepository(_context);     }      public icountryrepository countryrepository { get; private set; }      public int commit()     {         return _context.savechanges();     }      public void dispose()     {         _context.dispose();     } } 
  • now, in viewmodel

    private unitofwork currentunitofwork;  currentunitofwork = new unitofwork(new cmsdbcontext()); 

i followed description of tutor in udemy.

is separation correct?

move repository (entity framework) methods behind interfaces in core , reference interfaces in viewmodel project.

core.dll

public class somemodel {}  public interface isomemodelrepositoryservice {     somemodel getbyid(int id);     void save(somemodel model); } 

model.dll - project implements core's interfaces , contains database methods (entity framework in case)

public class somemodelrepositoryservice : isomemodelrepositoryservice {     public somemodel getbyid(int id)     {         //entity framework code     }      public void save(somemodel model)     {         //entity framework code     } } 

viewmodel.dll

public class somemodelviewmodel {     private isomemodelrepositoryservice _repositoryservice;      public somemodel model { get; set; }      public somemodelviewmodel(isomemodelrepositoryservice repositoryservice)     {         _repositoryservice = repositoryservice;     }      public void save()     {         _repositoryservice.save(this.model);     } } 

create repository implementation instances in top startup project(main method example) , put viewmodel parameters:

isomemodelrepositoryservice repositoryservice = new somemodelrepositoryservice();  somemodelviewmodel viewmodel = new somemodelviewmodel(repositoryservice); 

with approach "model.dll" need have references entity framework


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -