简体   繁体   English

在分层架构中引用 MVVM 中的实体框架

[英]Referencing Entity Framework in MVVM in layered architecture

My app is layered in 4 layers:我的应用程序分为 4 层:

  • Core : where I put some general and interface classes. Core :我放了一些通用和接口类。
  • Model : where my code-first classes, and other related to domain, such as entity configurations and repositories etc. Model :我的代码优先类,以及其他与域相关的类,例如实体配置和存储库等。
  • Vm : where the view models live. Vm :视图模型所在的位置。 Referencing Model .参考Model
  • Desktop : where the desktop app lives. Desktop :桌面应用程序所在的位置。 Referencing Vm .引用Vm

I have installed Entity Framework into Model , ViewModel and Desktop .我已将 Entity Framework 安装到ModelViewModelDesktop

My question is: is it enough to install it into Model only?我的问题是:仅将其安装到Model就足够了吗? Why to repeat again?为什么又要重复了?

[Edit] [编辑]

  • Here is my implementation of Repository and UnitOfWrok ( IRepository and Repository will be on Core ):这是我对RepositoryUnitOfWrok 的实现IRepositoryRepository将在Core 上):

     public interface IRepository<TEntity> where 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> where TEntity : class { protected readonly DbContext Context; public Repository(DbContext context) { Context = context; } public TEntity Get(int id) { return Context.Set<TEntity>().Find(id); } and so on... }
  • Now, the new next interfaces and classed will be on Model现在,新的下一个接口和类将在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 my ViewModel现在,在我的 ViewModel

     private UnitOfWork Currentunitofwork; Currentunitofwork = new UnitOfWork(new CmsDbContext());

I followed a description of a tutor in udemy.我遵循了 udemy 一位导师的描述。

Is this separation correct?这种分离是否正确?

Move all repository (Entity framework) methods behind interfaces in the Core and reference those interfaces in the ViewModel project.将所有存储库(实体框架)方法移到 Core 中的接口后面,并在 ViewModel 项目中引用这些接口。

Core.dll核心文件

public class SomeModel {}

public interface ISomeModelRepositoryService
{
    SomeModel GetById(int id);
    void Save(SomeModel model);
}

Model.dll - this project implements Core's interfaces and contains only database methods (Entity framework in your case) Model.dll - 这个项目实现了 Core 的接口并且只包含数据库方法(在你的例子中是实体框架)

public class SomeModelRepositoryService : ISomeModelRepositoryService
{
    public SomeModel GetById(int id)
    {
        //Entity framework code
    }

    public void Save(SomeModel model)
    {
        //Entity framework code
    }
}

ViewModel.dll视图模型.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 for example) and put it to ViewModel as parameters:在顶部启动项目中创建存储库实现实例(例如 Main 方法)并将其作为参数放入 ViewModel:

ISomeModelRepositoryService repositoryService = new SomeModelRepositoryService();

SomeModelViewModel viewmodel = new SomeModelViewModel(repositoryService);

With this approach only "Model.dll" need to have references to Entity Framework使用这种方法,只有“Model.dll”需要引用实体框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM