.net C#Dryioc学习,工作单元以及仓储模式(二) - // 注册 PersonService 到容器中
前言:
上次说到通过构造注入的方式实现控制反转,实际上在使用Ioc容器时,可以使用单例模式,一般第三方或微软自带的Ioc容器可以直接使用single方法使其注册成为单例;(先不说这些,下面来实现一个仓储模型以及工作单元[CRUD]).下次再细说单例、作用域、List插件注册方式.
这篇文章我们来使用仓储+工作单元实现一个简单的CRUD操作;仓储+工作单元的作用:它们用于数据存储和管理,以提供更好的代码组织和可测试性。在使用这些模式时,通常会将数据访问逻辑与业务逻辑分开,从而使代码更易于维护和测试。同时,它们还可以帮助解决并发问题和数据一致性问题。
上代码:
//定义一个Person Model
public class Person
{
//prop 加两下TAB快速构造属性
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
//定义一个仓储接口(一般用Repository结尾)
public Interface IPersonRepository
{
//CRUD
Person GetById(int id);
IEnumerableGetAll();
void Add(Person person);
void Delete(int id);
void Update(Person person);
}
//PersonRepository 实现仓储接口
public class PersonRepository:IPersonRepository
{
//假定使用EF来操作数据库
private readonly DbContext _dbcontext;
//构造函数可以使用ctor来快速构造
public PersonRepository(DbContext dbContext)
{
_dbContext = dbContext;
}
public Person GetById(int id)
{
return _dbContext.Set().Find(id);
}
public IEnumerableGetAll()
{
return _dbContext.Set().ToList();
}
public void Add(Person person)
{
_dbContext.Set().Add(person);
_dbContext.SaveChanges();
}
public void Update(Person person)
{
_dbContext.Set().Update(person);
_dbContext.SaveChanges();
}
public void Delete(int id)
{
var person = GetById(id);
if (person != null)
{
_dbContext.Set().Remove(person);
_dbContext.SaveChanges();
}
}
}
仓储实现后,接下来实现工作单元:
//该工作单元包含一个Save的操作,用于提交数据库
//还是先定义一个工作单元的接口
public interface IUnitOfWork
{
void SaveChanges();
}
//实现工作单元接口
public class UnitOfWork:IUnitOfWork
{
//还是使用EF来模拟提交数据库操作 ctor
private readonly DbContext _dbContext;
public UnitOfWork(DbContext dbcontext)
{
_dbContext=dbcontext;
}
public void SaveChanges()
{
_dbContext.SaveChanges();
}
}
好了,工作单元和仓储都已经实现,接下来开始使用:
假定我们有一个PersonService
public class PersonService
{
//将两个接口通过构造注入 注入进来
private readonly IPersonRepository _personRepository;
private readonly IUnitOfWork _unitOfWork;
public PersonService(IPersonRepository personRepository, IUnitOfWork unitOfWork)
{
_personRepository = personRepository;
_unitOfWork = unitOfWork;
}
public void AddPerson(Person person)
{
_personRepository.Add(person);
_unitOfWork.SaveChanges();
}
public void UpdatePerson(Person person)
{
_personRepository.Update(person);
_unitOfWork.SaveChanges();
}
public void DeletePerson(int id)
{
_personRepository.Delete(id);
_unitOfWork.SaveChanges();
}
public Person GetById(int id)
{
return _personRepository.GetById(id);
}
public IEnumerableGetAll()
{
return _personRepository.GetAll();
}
}
//使用服务(DryIoc容器)
// 注册 PersonService 到容器中
container.Register();
container.Register();
container.Register();
// 在需要使用 PersonService 的地方获取它的实例并调用方法
using (var scope = container.OpenScope())
{
var personService = scope.Resolve();
var person = new Person {Id=0, Name = "John Doe", Age = 30 }; 拼多多帮助力入口
personService.AddPerson(person);
}
以上就实现了仓储+工作单元简单的CRUD操作,下面说明一下为什么要这么做:
使用工作单元,确保了数据的唯一性!,假设有多个dbcontext实例需要更改若有一个地方出现修改失败或者异常等,无法保证数据库中数据唯一,使用工作单元的好处就体现出来了。
在仓储中调用EF的SaveChanges方法是为了将当前修改的数据保存到数据库中,而在工作单元中再次调用EF的SaveChanges方法是为了确保所有操作的原子性,即要么全部成功提交,要么全部回滚。
举个例子,假设在一个操作中需要对多个实体进行修改,删除或新增,如果这些操作都分别在仓储中调用EF的SaveChanges方法,那么如果其中某个操作失败了,其他操作可能已经成功提交到了数据库中,这就会导致数据不一致的问题。而在工作单元中统一调用EF的SaveChanges方法,可以确保所有操作的原子性,即要么全部成功提交,要么全部回滚。
实际应用中仓储和工作单元应该使用泛型T类,将其封装好实现后在对应的类实现操作。
而操作数据库又涉及到AutoMapper的使用 [为了不操作源数据;数据库和实体之间再做一个映射] ;下次再说吧。
参考链接,net