我仍然对存储库模式有些困惑。我想使用此模式的主要原因是避免从域调用 EF 4.1 特定的数据访问操作。我宁愿从 IRepository 接口调用通用 CRUD 操作。这将使测试更容易,如果将来我必须更改数据访问框架,我将能够这样做而无需重构大量代码。
这是我的情况的一个例子:
我在数据库中有 3 个表:Group
、Person
和GroupPersonMap
. GroupPersonMap
是一个链接表,只包含主键Group
和Person
主键。我用 VS 2010 设计器创建了 3 个表的 EF 模型。EF 足够聪明,可以假设GroupPersonMap
是一个链接表,因此它不会在设计器中显示它。我想使用我现有的域对象而不是 EF 生成的类,所以我关闭了模型的代码生成。
我现有的匹配EF模型的类如下:
public class Group
{
public int GroupId { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> People { get; set; }
}
public class Person
{
public int PersonId {get; set; }
public string FirstName { get; set; }
public virtual ICollection<Group> Groups { get; set; }
}
我有一个像这样的通用存储库接口:
public interface IRepository<T> where T: class
{
IQueryable<T> GetAll();
T Add(T entity);
T Update(T entity);
void Delete(T entity);
void Save()
}
和一个通用的 EF 存储库:
public class EF4Repository<T> : IRepository<T> where T: class
{
public DbContext Context { get; private set; }
private DbSet<T> _dbSet;
public EF4Repository(string connectionString)
{
Context = new DbContext(connectionString);
_dbSet = Context.Set<T>();
}
public EF4Repository(DbContext context)
{
Context = context;
_dbSet = Context.Set<T>();
}
public IQueryable<T> GetAll()
{
// code
}
public T Insert(T entity)
{
// code
}
public T Update(T entity)
{
Context.Entry(entity).State = System.Data.EntityState.Modified;
Context.SaveChanges();
}
public void Delete(T entity)
{
// code
}
public void Save()
{
// code
}
}
现在假设我只想将现有的映射Group
到现有的Person
. 我将不得不执行以下操作:
EFRepository<Group> groupRepository = new EFRepository<Group>("name=connString");
EFRepository<Person> personRepository = new EFRepository<Person>("name=connString");
var group = groupRepository.GetAll().Where(g => g.GroupId == 5).First();
var person = personRepository.GetAll().Where(p => p.PersonId == 2).First();
group.People.Add(person);
groupRepository.Update(group);
但这不起作用,因为 EF 认为Person
是新的,并且会尝试重新INSERT
进入Person
数据库,这将导致主键约束错误。我必须使用DbSet
'Attach
方法告诉 EFPerson
数据库中已经存在,所以只需在表之间Group
和表中创建一个映射。Person
GroupPersonMap
因此,为了附加Person
到上下文,我现在必须Attach
向我的 IRepository 添加一个方法:
public interface IRepository<T> where T: class
{
// existing methods
T Attach(T entity);
}
修复主键约束错误:
EFRepository<Group> groupRepository = new EFRepository<Group>("name=connString");
EFRepository<Person> personRepository = new EFRepository<Person>(groupRepository.Context);
var group = groupRepository.GetAll().Where(g => g.GroupId == 5).First();
var person = personRepository.GetAll().Where(p => p.PersonId == 2).First();
personRepository.Attach(person);
group.People.Add(person);
groupRepository.Update(group);
固定的。现在我必须处理另一个问题,Group
即每次创建组/人员映射时都会在数据库中进行更新。这是因为在我的EFRepository.Update()
方法中,实体状态被显式设置为Modified'. I must set the Group's state to
Unchanged so the
Group 表不会被修改。
为了解决这个问题,我必须向我的 IRepository 添加某种Update
重载,它不会更新根实体,或者Group
,在这种情况下:
public interface IRepository<T> where T: class
{
// existing methods
T Update(T entity, bool updateRootEntity);
}
Update 方法的 EF4 实现如下所示:
T Update(T entity, bool updateRootEntity)
{
if (updateRootEntity)
Context.Entry(entity).State = System.Data.EntityState.Modified;
else
Context.Entry(entity).State = System.Data.EntityState.Unchanged;
Context.SaveChanges();
}
我的问题是:我以正确的方式接近这个吗?当我开始使用 EF 和存储库模式时,我的存储库开始看起来以 EF 为中心。感谢您阅读这篇长文