DDD实战4 实现产品仓储
2024-08-31 22:50:49
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;
}
}
}
以上就是产品仓储的实现,它和领域的业务逻辑是完全分离的,只是在仓储里面会用到领域对象,领域对象是业务的核心。
最新文章
- 【PHP】月末・月初の出力方法
- OpenCV计算点到直线的距离 数学法
- javascript时间的一些问题
- HTML5手机APP开发入(3)
- redis实现有序的消息队列
- JS的十大经典算法排序
- Android中表示尺寸的六种度量单位
- HDU 5637 Transform 搜索
- 轻量级jQuery工具提示插件tooltipsy使用方法
- YII 实现布局
- CSharp设计模式读书笔记(6):建造者模式(学习难度:★★★★☆,使用频率:★★☆☆☆)
- linux内核之链表操作解析
- [Paxos] Paxos Made Simple 读后感
- Spring mybatis源码篇章-MybatisDAO文件解析(一)
- 开源社交系统ThinkSNS v4.6.1更新日志及功能详解!
- R语言数据集的技术
- 起泡排序(Bubble sort)
- 深度解析synchronized的实现原理(并发一)
- 【转】Yelp是如何实现每天运行数百万个测试的
- Unity3D 移动摇杆处理
热门文章
- Debian 上创建新的用户
- js进阶 12-8 如何知道上一个函数的返回值是什么(如何判断上一个函数是否执行成功)
- VC ADO “ParameterDirectionEnum”:“enum” 类型等 重定义问题 解决方案
- [CSS] Control Image Aspect Ratio Using CSS
- C# 二分法查找和排序
- 学习Numpy基础操作
- windows 下安装git
- 【u211】编码
- cors-synchronous-requests-not-working-in-firefox
- [TypeScript] Using ES6 and ESNext with TypeScript