a.要实现仓储,首先要定义仓储接口。在领域层定义仓储接口,IProductRepository.cs。

public interface IProductRepository
{
void CreateProduct<T>(T productspu) where T:class,IAggregationRoot;
}

产品持久化仓储的实现不应当在领域层实现,因为领域就和仓储实现紧密的绑定在了一起,导致业务和技术没有分离。之所以把接口定义在领域层是因为,领域层对数据的访问只要调用接口就行了。

b.在基础结构层中新建项目实现仓储实现,在基础结构层新建项目取名DDD.Repositories,以后将所有的仓储的实现都写在这个项目中。

领域层是核心层,仓储应该依赖领域层,所以在此,为DDD.Repositories添加依赖项目——Product.Domain。

c.在DDD.Repositories项目下添加文件夹,取名ProductRepositories,在此文件夹内放置针对产品的仓储,在此文件夹下面添加EFCore针对产品聚合仓储的实现ProductEFCorerepositoy

public class ProductEFCoreRepository : IProductRepository
{
private readonly DbContext dbcontext; public ProductEFCoreRepository(DbContext dbcontext)
{
this.dbcontext = dbcontext;
}
public void CreateProduct<T>(T productspu) where T : class, IAggregationRoot
{
//这里的ProductEFCoreContext就是上节 在产品领域层创建的ProductEFCoreContext类,我们用它生成的数据库
var db = dbcontext as ProductEFCoreContext;
var newproductspu = productspu as ProductSPU;
try
{
db.ProductSPU.Add(newproductspu);
}
catch (Exception ex)
{ throw ex;
}
}
}

以上就是产品仓储的实现,它和领域的业务逻辑是完全分离的,只是在仓储里面会用到领域对象,领域对象是业务的核心。

最新文章

  1. 【PHP】月末・月初の出力方法
  2. OpenCV计算点到直线的距离 数学法
  3. javascript时间的一些问题
  4. HTML5手机APP开发入(3)
  5. redis实现有序的消息队列
  6. JS的十大经典算法排序
  7. Android中表示尺寸的六种度量单位
  8. HDU 5637 Transform 搜索
  9. 轻量级jQuery工具提示插件tooltipsy使用方法
  10. YII 实现布局
  11. CSharp设计模式读书笔记(6):建造者模式(学习难度:★★★★☆,使用频率:★★☆☆☆)
  12. linux内核之链表操作解析
  13. [Paxos] Paxos Made Simple 读后感
  14. Spring mybatis源码篇章-MybatisDAO文件解析(一)
  15. 开源社交系统ThinkSNS v4.6.1更新日志及功能详解!
  16. R语言数据集的技术
  17. 起泡排序(Bubble sort)
  18. 深度解析synchronized的实现原理(并发一)
  19. 【转】Yelp是如何实现每天运行数百万个测试的
  20. Unity3D 移动摇杆处理

热门文章

  1. Debian 上创建新的用户
  2. js进阶 12-8 如何知道上一个函数的返回值是什么(如何判断上一个函数是否执行成功)
  3. VC ADO “ParameterDirectionEnum”:“enum” 类型等 重定义问题 解决方案
  4. [CSS] Control Image Aspect Ratio Using CSS
  5. C# 二分法查找和排序
  6. 学习Numpy基础操作
  7. windows 下安装git
  8. 【u211】编码
  9. cors-synchronous-requests-not-working-in-firefox
  10. [TypeScript] Using ES6 and ESNext with TypeScript