在实体框架中使用UnitOfWork和存储库模式

编程入门 行业动态 更新时间:2024-10-27 14:32:48
本文介绍了在实体框架中使用UnitOfWork和存储库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我将在数据访问层中使用存储库和UnitOfwork来查看一位联系人的聚合根

I'm gonna to use repository and UnitOfwork in my data access layer to do this take a look at one contact aggregateroot

public interface IAggregateRoot { }

这是我的通用存储库接口:

this is my Generic repository interface :

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类

and my POCO Contact class in Model

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,也许也拥有它方法

and this my IContactRepository that inherit from IRepository and also maybe has it is own method

public interface IContactRepository : IRepository<Contact> { }

现在我已经在IUitOfWork和UnitOfwork中做到了

Now I have done in IUitOfWork and UnitOfwork like this

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 }

我可以使用Service中的UnitOfwork访问存储库来执行所有CRUD操作,例如:

I can do all CRUD operation with accessing Repositories with UnitOfwork in Service , example :

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

但我想这样做

_

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

(通过_ContactRepository获得CRUD和通用方法,由_unitOfWork.ContactRepository获得)因为我想要要获取ContactRepository方法来处理某些特定查询,请任何人帮助吗?

(get CRUD and generic method via _ContactRepository No by _unitOfWork.ContactRepository) Because I want to get ContactRepository method to some specific queries , anybody help please ??

推荐答案

这不是您问题的直接答案,但可能会简化一些工作,并且减少重复。

It's not a direct answer to your question, but it might simplify things a little bit and reduce duplication.

使用例如EntityFramework Power Tools可以对Code First(或通常只使用Code First)进行逆向工程,最后得到 DbContext 类,该类用作一个UoW和一个存储库,例如:

When you use e.g. EntityFramework Power Tools to reverse-engineer Code First (or just use Code First in general), you end up with the DbContext class that serves as a UoW and repository in one, e.g.:

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

现在,如果您希望事物可以测试,那么很容易方式:引入一个非常薄的接口:

Now, if you want things to be testable, there's an easy way: introduce a very thin 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. }

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

then make another file with second part of the partial class:

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

Ta-da!现在,您可以注入 IDbContext 和 YourDbContext 进行备份:

Ta-da! Now you can inject IDbContext with YourDbContext backing it up:

//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 (是否通用,取决于您的需要)并注入该工厂。

Now if you want to have control over the disposal of the context, then you'd have to write your own (gasp) IDbContextFactory (generic or not, depending what you need) and inject that factory instead.

无需编写自己的查找,添加或更新方法, DbContext 将适当地处理该问题,更容易引入显式事务,并且所有内容都很好地隐藏在接口后面( IDbContext , IDbSet )。

No need to write your own Find, Add or Update methods now, DbContext will handle that appropriately, it's easier to introduce explicit transactions and everything is nicely hidden behind interfaces (IDbContext, IDbSet).

顺便说一下, IDbContextFactory 相当于NHibernate的 ISessionFactory 和 IDbContext - ISession 。我希望EF也能做到这一点。

By the way, the IDbContextFactory would be an equivalent to NHibernate's ISessionFactory and IDbContext - ISession. I wish EF had this out of the box, too.

更多推荐

在实体框架中使用UnitOfWork和存储库模式

本文发布于:2023-11-06 22:21:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   模式   UnitOfWork

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!