如何使用工作单元和存储库模式来添加简单的添加功能

编程入门 行业动态 更新时间:2024-10-28 04:27:03
本文介绍了如何使用工作单元和存储库模式来添加简单的添加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的测试看起来像这样

[Fact] public void SimpleAddTest() { // Arrange var authorizationsToBeAdded = new List<PatientPayerAuthInfo> { new PatientPayerAuthInfo (), new PatientPayerAuthInfo () }.ToList(); var persistentAuthorizations = new List<PatientPayerAuthInfo> { new PatientPayerAuthInfo {PatientPayerAuthInfoId = 1 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 2 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 3 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 4 } }.AsQueryable(); var mockSet = new Mock<DbSet<PatientPayerAuthInfo>>(); mockSet.As<IQueryable<PatientPayerAuthInfo>>().Setup(m => m.Provider).Returns(persistentAuthorizations.Provider); mockSet.As<IQueryable<PatientPayerAuthInfo>>().Setup(m => m.Expression).Returns(persistentAuthorizations.Expression); mockSet.As<IQueryable<PatientPayerAuthInfo>>().Setup(m => m.ElementType).Returns(persistentAuthorizations.ElementType); mockSet.As<IQueryable<PatientPayerAuthInfo>>().Setup(m => m.GetEnumerator()).Returns(persistentAuthorizations.GetEnumerator()); var mockedUnitOfWork = new Mock<IUOW<DBContext>>(); var service = new PatientPayerService(mockedUnitOfWork.Object); // Act var sut = service.AddPatientPayerAuthInfos(authorizationsToBeAdded); // Assert }

服务层功能如下

public void AddPatientPayerAuthInfos(IEnumerable<PatientPayerAuthInfo> patientPayerAuthInfos) { foreach (var patientPayerAuthInfo in patientPayerAuthInfos) { UOW.PatientPayerAuthInfos.Add(patientPayerAuthInfo); } UOW.SaveChanges(); }

AND存储库实现为

public virtual void Add(T entity) { DbEntityEntry dbEntityEntry = DbContext.Entry(entity); if (dbEntityEntry.State != EntityState.Detached) { dbEntityEntry.State = EntityState.Added; } else { DbSet.Add(entity); } }

然后有一个提交方法,该方法调用EF的SaveChanges.

which then has a commit method that calls EF's SaveChanges.

我的问题是我们如何使用persistentAuthorizations来设置mockedUnitOfWork,以便当我使用authorizationsToBeAdded添加两个对象时,persistentAuthorizations的总数将变为6,最初为4.

my question is how we can Setup mockedUnitOfWork using persistentAuthorizations so that when i add Two objects using authorizationsToBeAdded then the total count of persistentAuthorizations becomes 6, which are 4 initially.

或者纠正我,如果我走错了路.

or correct me if i am on wrong track.

public interface IRepository<T> where T : class { void Add(T entity); } public interface IUOW<U> where U : DbContext, IDisposable { IRepository<PatientPayerAuthInfo> PatientPayerAuthInfos { get; } void SaveChanges(); }

推荐答案

使用列表作为persistentAuthorizations的基础,例如:

Use a list as base for persistentAuthorizations like:

var data = new List<PatientPayerAuthInfo> { new PatientPayerAuthInfo {PatientPayerAuthInfoId = 1 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 2 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 3 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 4 } }; var persistentAuthorizations = data.AsQueryable();

然后您可以像这样设置mockedUnitOfWork:

Then you can setup mockedUnitOfWork like this:

var repositoy = new Mock<IRepository<PatientPayerAuthInfo>>(); // when adding data to the repository, add the item to 'data' repositoy.Setup(r => r.Add(It.IsAny<PatientPayerAuthInfo>())) .Callback(delegate(PatientPayerAuthInfo y) { data.Add(y); }); // when accessing 'PatientPayerAuthInfos', use the repository mock var mockedUnitOfWork = new Mock<IUOW<DBContext>>(); mockedUnitOfWork.SetupGet(x => x.PatientPayerAuthInfos).Returns(() => repositoy.Object);

更多推荐

如何使用工作单元和存储库模式来添加简单的添加功能

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

发布评论

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

>www.elefans.com

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