首页 文章

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

提问于
浏览
-1

我的应用分为4层:

  • Core :我放了一些通用和接口类 .

  • Model :我的代码优先类,以及与域相关的其他类,例如实体配置和存储库等 .

  • Vm :视图模型的实时位置 . 引用 Model .

  • Desktop :桌面应用程序所在的位置 . 引用 Vm .

我已将实体框架安装到 ModelViewModelDesktop 中 .

我的问题是:仅仅将它安装到_1816080中就足够了吗?为什么要重复一次?

[编辑]

  • 这是我的 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...
}
  • 现在,新的下一个接口和分类将在 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();
    }
}
  • 现在,在我的ViewModel中
private UnitOfWork Currentunitofwork;

Currentunitofwork = new UnitOfWork(new CmsDbContext());

我跟着udemy的导师描述 .

这种分离是否正确?

1 回答

  • 1

    将所有存储库(实体框架)方法移动到Core中的接口后面,并引用ViewModel项目中的这些接口 .

    Core.dll

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

    Model.dll - 这个项目实现了Core的接口,只包含数据库方法(在你的情况下为Entity框架)

    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);
        }
    }
    

    在top startUp项目中创建存储库实现实例(例如Main方法)并将其作为参数放到ViewModel中:

    ISomeModelRepositoryService repositoryService = new SomeModelRepositoryService();
    
    SomeModelViewModel viewmodel = new SomeModelViewModel(repositoryService);
    

    使用此方法,只有“Model.dll”需要引用Entity Framework

相关问题