使用工作单元和不同的目标保存信息
Save information using Unit Of Work and different targets
我的 .NET 网络服务中有一个命令需要在几个不同的地方保存信息:
- 1 个本地数据库 SQL 服务器使用 Entity Framework
- 2 个不同的网络服务使用 REST
我想使用工作单元模式。当您只有一个数据库并使用数据上下文时,这很容易。
但是您知道如何实现具有混合目标的工作单元吗?我知道我不能依赖分布式事务,没有它我也能活下去。但是工作单元的原则是我想保留的。
有什么想法、建议或模式可供我使用吗?
您不需要将数据库保存逻辑与 REST 保存逻辑混合。您可以让 UoW 模块专门与数据库一起工作,并在顶部添加一个额外的服务层,它利用 UoW 保存在数据库中,然后调用 REST 端点也保存在那里。服务层将在通用方法下编排不同的操作。
您能否提供一些示例代码,以便为您提供更合适的建议?
编辑:
好的,这是一些通用示例:
这是您的工作单位 class:
public class UnitOfWork : IUnitOfWork, IDisposable
{
private DatabaseContext context = new DatabaseContext();
private IDepartmentRepository departmentRepository;
private ICustomerRepository customerRepository;
public IDepartmentRepository DepartmentRepository
{
get
{
if (this.departmentRepository == null)
{
this.departmentRepository = new DepartmentRepository(context);
}
return departmentRepository;
}
}
public ICustomerRepository CustomerRepository
{
get
{
if (this.customerRepository == null)
{
this.customerRepository = new CustomerRepository(context);
}
return customerRepository;
}
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
您的服务层的一部分可能如下所示:
class CustomerService : ICustomerService
{
private IUnitOfWork unitOfWork;
public CustomerService(IUnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public void AddCustomer(Customer customer)
{
this.unitOfWork.CustomerRepository.Add(customer);
this.unitOfWork.Save();
// Call REST here
}
public void DeleteCustomer(int customerId)
{
this.unitOfWork.CustomerRepository.DeleteById(customerId);
this.unitOfWork.Save();
// Call REST here
}
}
在您的控制器中,您只能通过服务层进行操作。在当前示例中,如果您想要 Add/Delete 客户等,您将实例化一个新的 CustomerService...
请注意,这几乎是一个伪代码,可能无法准确满足您的需求,但如果没有关于您的上下文的任何信息,也无法做更多的事情。
我的 .NET 网络服务中有一个命令需要在几个不同的地方保存信息:
- 1 个本地数据库 SQL 服务器使用 Entity Framework
- 2 个不同的网络服务使用 REST
我想使用工作单元模式。当您只有一个数据库并使用数据上下文时,这很容易。
但是您知道如何实现具有混合目标的工作单元吗?我知道我不能依赖分布式事务,没有它我也能活下去。但是工作单元的原则是我想保留的。
有什么想法、建议或模式可供我使用吗?
您不需要将数据库保存逻辑与 REST 保存逻辑混合。您可以让 UoW 模块专门与数据库一起工作,并在顶部添加一个额外的服务层,它利用 UoW 保存在数据库中,然后调用 REST 端点也保存在那里。服务层将在通用方法下编排不同的操作。
您能否提供一些示例代码,以便为您提供更合适的建议?
编辑:
好的,这是一些通用示例:
这是您的工作单位 class:
public class UnitOfWork : IUnitOfWork, IDisposable
{
private DatabaseContext context = new DatabaseContext();
private IDepartmentRepository departmentRepository;
private ICustomerRepository customerRepository;
public IDepartmentRepository DepartmentRepository
{
get
{
if (this.departmentRepository == null)
{
this.departmentRepository = new DepartmentRepository(context);
}
return departmentRepository;
}
}
public ICustomerRepository CustomerRepository
{
get
{
if (this.customerRepository == null)
{
this.customerRepository = new CustomerRepository(context);
}
return customerRepository;
}
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
您的服务层的一部分可能如下所示:
class CustomerService : ICustomerService
{
private IUnitOfWork unitOfWork;
public CustomerService(IUnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public void AddCustomer(Customer customer)
{
this.unitOfWork.CustomerRepository.Add(customer);
this.unitOfWork.Save();
// Call REST here
}
public void DeleteCustomer(int customerId)
{
this.unitOfWork.CustomerRepository.DeleteById(customerId);
this.unitOfWork.Save();
// Call REST here
}
}
在您的控制器中,您只能通过服务层进行操作。在当前示例中,如果您想要 Add/Delete 客户等,您将实例化一个新的 CustomerService...
请注意,这几乎是一个伪代码,可能无法准确满足您的需求,但如果没有关于您的上下文的任何信息,也无法做更多的事情。