基于仓储的实现
1、前言:本着第一节写的有些糊涂,主要是自己喜欢实干,不太喜欢用文字表述,就这样吧。下面切入正题。
博客园里面有很多的大佬,我这里就不一一解释概览,有兴趣的朋友可以去看大佬们写的概览。好了不多说了。我们先来看看仓储的实现。
2、上一节中我们已经实现了model,现在我们就来实现仓储。
首先声明一个借口,这里我定义为IRepository,而这个接口我要用做泛型,所以接口如下:
仓储的实现
3、现在给这个接口写上接口的方法:
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 /// 根据聚合根的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 }