我的应用分为4层:
-
Core
:我放了一些通用和接口类 . -
Model
:我的代码优先类,以及与域相关的其他类,例如实体配置和存储库等 . -
Vm
:视图模型的实时位置 . 引用Model
. -
Desktop
:桌面应用程序所在的位置 . 引用Vm
.
我已将实体框架安装到 Model
, ViewModel
和 Desktop
中 .
我的问题是:仅仅将它安装到_1816080中就足够了吗?为什么要重复一次?
[编辑]
- 这是我的 Repository 和 UnitOfWrok 的实现( IRepository 和 Repository 将在 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 回答
将所有存储库(实体框架)方法移动到Core中的接口后面,并引用ViewModel项目中的这些接口 .
Core.dll
Model.dll - 这个项目实现了Core的接口,只包含数据库方法(在你的情况下为Entity框架)
ViewModel.dll
在top startUp项目中创建存储库实现实例(例如Main方法)并将其作为参数放到ViewModel中:
使用此方法,只有“Model.dll”需要引用Entity Framework