當(dāng)前位置:
首頁 >
DDD领域驱动之干货(二)
發(fā)布時間:2025/4/16
37
豆豆
生活随笔
收集整理的這篇文章主要介紹了
DDD领域驱动之干货(二)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
基于倉儲的實現(xiàn)
1、前言:本著第一節(jié)寫的有些糊涂,主要是自己喜歡實干,不太喜歡用文字表述,就這樣吧。下面切入正題。
博客園里面有很多的大佬,我這里就不一一解釋概覽,有興趣的朋友可以去看大佬們寫的概覽。好了不多說了。我們先來看看倉儲的實現(xiàn)。
2、上一節(jié)中我們已經(jīng)實現(xiàn)了model,現(xiàn)在我們就來實現(xiàn)倉儲。
首先聲明一個借口,這里我定義為IRepository,而這個接口我要用做泛型,所以接口如下:
倉儲的實現(xiàn)3、現(xiàn)在給這個接口寫上接口的方法:
1 using Mio.Domain.BaseModel; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Linq.Expressions; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace Mio.Domain.Repositories 10 { 11 /// <summary> 12 /// 在DDD中倉儲只能操作聚合根 13 /// </summary> 14 /// <typeparam name="TEntity"></typeparam> 15 public interface IRepository<TEntity> where TEntity : AggregateRoot 16 { 17 IEnumerable<TEntity> LoadListAll(Expression<Func<TEntity, bool>> predicate); 18 IQueryable<TEntity> LoadAll(Expression<Func<TEntity, bool>> predicate); 19 20 IEnumerable<TEntity> LoadListForSql(string sql); 21 IQueryable<TEntity> LoadForSql(string sql); 22 23 /// <summary> 24 /// 根據(jù)聚合根的ID值,從倉儲中讀取聚合根。 25 /// </summary> 26 /// <param name="key">聚合根的ID值。</param> 27 /// <returns>聚合根實例。</returns> 28 TEntity GetKey(Guid key); 29 /// <summary> 30 /// 將指定的聚合根添加到倉儲中。 31 /// </summary> 32 /// <param name="aggregateRoot">需要添加到倉儲的聚合根實例。</param> 33 void Add(TEntity aggregateRoot); 34 /// <summary> 35 /// 將指定的聚合根從倉儲中移除。 36 /// </summary> 37 /// <param name="aggregateRoot">需要從倉儲中移除的聚合根。</param> 38 void Remove(TEntity aggregateRoot); 39 /// <summary> 40 /// 更新指定的聚合根。 41 /// </summary> 42 /// <param name="aggregateRoot">需要更新的聚合根。</param> 43 void Update(TEntity aggregateRoot); 44 } 45 } View Code接口的方法寫完了。
上節(jié)說了每一個領(lǐng)域模型都是特有的,所以在這里每一個領(lǐng)域模型都有一個固定的倉儲實現(xiàn)。
好了,現(xiàn)在接口方法我們寫完了,現(xiàn)在去寫實現(xiàn)類。
我這里只做了一個測試所以簡單的貼下代碼。
1 using Mio.Domain.Model; 2 using Mio.Domain.Repositories; 3 using Mio.Repository.EFRepository; 4 using System; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace Mio.Repository.ModelRepository 11 { 12 /// <summary> 13 /// 用戶角色 14 /// </summary> 15 public class UserRoleRepositoryImpl : RepositoryImpl<UserRole>, IUserRoleRepository 16 { 17 public Role GetRoleForUser(User user) 18 { 19 var context = lazy.Context as MioContext; 20 if (context != null) 21 { 22 string sql = "SELECT * FROM Role as a INNER JOIN UserRole as b ON a.Id = b.RoleId INNER JOIN [User] as c ON c.Id = b.Id WHERE c.Id = '"+user.Id+"'"; 23 return context.Set<Role>().SqlQuery(sql).FirstOrDefault(); 24 25 //var query = from role in context.Role 26 // from userRole in context.Userrole 27 // from usr in context.User 28 // where role.Id == userRole.RoleId && 29 // usr.Id == userRole.Id && 30 // usr.Id == user.Id 31 // select role; 32 //return query.FirstOrDefault(); 33 } 34 throw new InvalidOperationException("The provided repository context is invalid."); 35 } 36 } 37 } View Code差點忘記了我的項目架構(gòu)圖。如下圖。
測試如下:
其中引用了automapper。automapper的實現(xiàn)將在下一節(jié)介紹,還有其中的工作單元也會在下一節(jié)引入。
轉(zhuǎn)載于:https://www.cnblogs.com/edna-lzh/p/6888930.html
總結(jié)
以上是生活随笔為你收集整理的DDD领域驱动之干货(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Laravel 有哪些核心的内容?
- 下一篇: Robots at Warehouse(