C# DbContext、存储库和服务之间的粘合剂,我明白了吗?
背景: 这是我第一个使用存储库、服务和实体框架的项目。现在我主要关注数据层,以及我应该如何构建它。我只是在使用SQLite,但我需要Dropbox和将JSON对象读/写到本地存储的能力,所以我希望通过创建一个数据层,我可以在以后添加这些功能 我所拥有的: IRepository.csC# DbContext、存储库和服务之间的粘合剂,我明白了吗?,c#,entity-framework,repository-pattern,C#,Entity Framework,Repository Pattern,背景: 这是我第一个使用存储库、服务和实体框架的项目。现在我主要关注数据层,以及我应该如何构建它。我只是在使用SQLite,但我需要Dropbox和将JSON对象读/写到本地存储的能力,所以我希望通过创建一个数据层,我可以在以后添加这些功能 我所拥有的: IRepository.cs using System; namespace Premier.Data.Repositories { public interface IRepository<TEntity> where
using System;
namespace Premier.Data.Repositories
{
public interface IRepository<TEntity> where TEntity : class
{
void Create(TEntity entity);
void Remove(TEntity entity);
void Update(TEntity entity);
}
}
using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
namespace Premier.Data.Repositories
{
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected DbContext context;
protected IDbSet<TEntity> entities;
public Repository(DbContext dbContext)
{
context = dbContext;
entities = dbContext.Set<TEntity>();
}
public void Create(TEntity entity)
{
entities.Add(entity);
}
public void Update(TEntity entity)
{
entities.Attach(entity);
context.Entry(entity).State = EntityState.Modified;
}
protected IQueryable<TEntity> Query()
{
return entities;
}
public void Remove(TEntity entity)
{
entities.Remove(entity);
}
}
使用系统;
命名空间Premier.Data.Repositories
{
公共接口假定,其中tenty:类
{
无效创建(tenty实体);
无效删除(潜在实体);
无效更新(潜在实体);
}
}
Repository.cs
using System;
namespace Premier.Data.Repositories
{
public interface IRepository<TEntity> where TEntity : class
{
void Create(TEntity entity);
void Remove(TEntity entity);
void Update(TEntity entity);
}
}
using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
namespace Premier.Data.Repositories
{
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected DbContext context;
protected IDbSet<TEntity> entities;
public Repository(DbContext dbContext)
{
context = dbContext;
entities = dbContext.Set<TEntity>();
}
public void Create(TEntity entity)
{
entities.Add(entity);
}
public void Update(TEntity entity)
{
entities.Attach(entity);
context.Entry(entity).State = EntityState.Modified;
}
protected IQueryable<TEntity> Query()
{
return entities;
}
public void Remove(TEntity entity)
{
entities.Remove(entity);
}
}
使用系统;
使用系统数据;
使用System.Data.Entity;
使用System.Linq;
命名空间Premier.Data.Repositories
{
公共类存储库:IRepository,其中tenty:class
{
受保护的数据库上下文;
受保护的IDbSet实体;
公共存储库(DbContext DbContext)
{
context=dbContext;
entities=dbContext.Set();
}
公共void创建(TEntity实体)
{
实体。添加(实体);
}
公共无效更新(TEntity实体)
{
实体。附(实体);
context.Entry(entity.State=EntityState.Modified;
}
受保护的IQueryable查询()
{
返回实体;
}
公共无效删除(TEntity实体)
{
实体。移除(实体);
}
}
我不明白的是:
据我所知,服务与存储库对话,我的WPF项目只与服务通信。这是正确的,还是视图模型也会访问存储库?
无论我在何处访问这些存储库,如何确保DbContext具有最新的数据?例如,在“部门”视图中,可以添加或删除部门。此项目中运行的连续服务的每个间隔都需要最新的详细信息,只要可用(或在间隔时间内)。最初,我打算使用factory和singleton之间的混合来创建存储库,并让它们共享DbContext。但是,我读到一个SO问题,说全局DbContext是个坏主意。所以现在我想知道,如何回答如何从我的持续服务中获取最新信息的问题
谢谢你的时间,我很感激。回答#1
是的,您是正确的,服务与存储库对话,WPF应用程序将仅与服务通信。不应在ViewModels中直接使用存储库,这是不可取的
答复#2
访问这些存储库时,可以创建DbContext的新实例以获取最新数据,也可以使用DbContext的现有实例(更新数据库之前创建的实例)。如果使用新实例,则将获取最新数据。但是如果使用现有DbContext,则它将获取旧数据。1)我对WPF不是很有经验,但我记得可以将一些控件直接绑定到数据库(数据绑定)。在这种情况下,您甚至不需要存储库模式
2) 无论如何,您的通用存储库实现得非常好。IMHO,我更喜欢只定义DbContext
受保护的字段。我认为IDbSet
字段是多余的,因为您可以通过上下文访问它(就像您在构造函数中所做的那样)。因此,不要使用
entities.Add(entity);
您可以使用以下选项:
dbContext.Set<TEntity>().Add(entity);
dbContext.Set().Add(实体);
此外,如果您想在所有存储库之间共享一个上下文,有一个很好的模式称为工作单元。只需谷歌:通用存储库+工作单元+EF。简单地说,它是一个围绕您使用的所有存储库的包装类。您只需将上下文对象传递给工作单元构造函数,它就是sh因此,当您调用Save Changes
时,您使用任何repo更改的所有内容都会同时保存
我认为您需要定义自己的上下文(继承自DbContext)以及可能的接口,以便引入依赖注入
希望这有帮助
编辑:查看此问题和答案