gpt4 book ai didi

c# - 在 Entity Framework 中使用 UnitOfWork 和存储库模式

转载 作者:行者123 更新时间:2023-11-30 13:22:56 25 4
gpt4 key购买 nike

我将在我的数据访问层中使用存储库和 UnitOfwork 来执行此操作,看看一个联系人 aggregateroot

 public interface IAggregateRoot
{
}

这是我的通用存储库界面:

 public interface IRepository<T>
{
IEnumerable<T> GetAll();
T FindBy(params Object[] keyValues);
void Add(T entity);
void Update(T entity);
void Delete(T entity);

}

和我在模型中的 POCO Contact 类

 public class Contact :IAggregateRoot
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public DateTime CreationTime { get; set; }
}

这是我的 IContactRepository 继承自 IRepository 并且可能有自己的方法

 public interface IContactRepository : IRepository<Contact>
{
}

现在我已经像这样在 IUitOfWork 和 UnitOfwork 中完成了

public interface IUnitOfWork 
{
IRepository<Contact> ContactRepository { get; }
}

public class UnitOfWork : IUnitOfWork
{
private readonly StatosContext _statosContext = new StatosContext();
private IRepository<Contact> _contactUsRepository;

public IRepository<Contact> ContactRepository
{
get { return _contactUsRepository ?? (_contactUsRepository = new Repository<Contact>(_statosContext)); }
}
}

还有关于我的存储库

public class Repository<T> : IRepository<T> where T : class, IAggregateRoot
{
//implementing methods
}

我可以通过在服务中使用 UnitOfwork 访问存储库来执行所有 CRUD 操作,示例:

_unitOfWork.ContactRepository.Add(contact);
_unitOfWork.SaveChanges();

但我想这样做

_

ContactRepository.Add(contact);
_unitOfWork.SaveChanges();

(通过_unitOfWork.ContactRepository获取CRUD和泛型方法通过_ContactRepository否)因为我想通过某些特定查询获取 ContactRepository 方法,有人帮忙吗??

最佳答案

这不是您问题的直接答案,但它可能会稍微简化事情并减少重复。

当您使用例如EntityFramework Power Tools 对 Code First 进行逆向工程(或者一般只使用 Code First),您最终得到 DbContext 类,它作为 UoW 和存储库合二为一,例如:

public partial class YourDbContext : DbContext
{
public DbSet<Contact> Contacts {get; set;}
}

现在,如果你想让事情可测试,有一个简单的方法:引入一个非常薄的接口(interface):

public interface IDbContext
{
IDbSet<T> EntitySet<T>() where T : class;
int SaveChanges();
//you can reveal more methods from the original DbContext, like `GetValidationErrors` method or whatever you really need.
}

然后用部分类的第二部分制作另一个文件:

public partial class YourDbContext : IDbContext
{
public IDbSet<T> EntitySet<T>() where T : class
{
return Set<T>();
}
}

哒哒!现在你可以注入(inject) IDbContextYourDbContext 支持它:

//context is an injected IDbContext:
var contact = context.EntitySet<Contact>().Single(x => x.Id == 2);
contact.Name = "Updated name";
context.EntitySet<Contact>().Add(new Contact { Name = "Brand new" });
context.SaveChanges();

现在,如果您想控制上下文的处理,那么您必须编写自己的(gasp)IDbContextFactory(是否通用,具体取决于你需要什么)并注入(inject)那个工厂。

现在无需编写您自己的FindAddUpdate 方法,DbContext 将适本地处理它,更容易引入显式事务,并且一切都很好地隐藏在接口(interface)后面(IDbContextIDbSet)。

顺便说一句,IDbContextFactory 相当于 NHibernate 的 ISessionFactoryIDbContext - ISession。我希望 EF 也能开箱即用。

关于c# - 在 Entity Framework 中使用 UnitOfWork 和存储库模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16467576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com