规约模式Specification的学习
最近一直在看DDD开发 规约似乎用得很普遍。 但是还是理解不了。所以记录下学习的进度。~
规约(Specification)模式
目的:查询语句和查询条件的分离
写了一个关于规约的模拟小程序
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
OrderInfo o = new OrderInfo()
{
ID = i + 1,
OrderTime = DateTime.Now,
PorductID = new Random().Next(800, 810),
Price = new Random().Next(123, 321),
UserID = i + 1,
UserName = "ZZQ" + i.ToString()
};
SimulateDataBase.Add(o);
}
IRepository<OrderInfo> orderRespository = new OrderRepository();
OrderInfo orderInfo = orderRespository.GetEntity(new OrderInfoSpecification(2));
Console.WriteLine(orderInfo.UserName);
Console.Read();
}
}
public class OrderInfo
{
public int ID { get; set; }
public int UserID { get; set; }
public string UserName { get; set; }
public int PorductID { get; set; }
public decimal Price { get; set; }
public DateTime OrderTime { get; set; }
}
public interface IRepository<TEntity>
where TEntity : class,new()
{
void Add(TEntity entity);
IEnumerable<TEntity> GetEntitys(ISpecification<TEntity> specification);
TEntity GetEntity(ISpecification<TEntity> specification);
}
public interface IOrderRepository : IRepository<OrderInfo>
{ }
public class OrderRepository : IOrderRepository
{
public void Add(OrderInfo entity)
{
SimulateDataBase.Add(entity);
} public IEnumerable<OrderInfo> GetEntitys(ISpecification<OrderInfo> specification)
{
return SimulateDataBase.OrderInfoList.Where(specification.IsSatisfiedBy);
} public OrderInfo GetEntity(ISpecification<OrderInfo> specification)
{
return SimulateDataBase.OrderInfoList.Where(specification.IsSatisfiedBy).FirstOrDefault();
}
} public interface ISpecification<TEntity>
{
bool IsSatisfiedBy(TEntity obj);
//ISpecification<TEntity> And(ISpecification<TEntity> other);
//ISpecification<TEntity> Or(ISpecification<TEntity> other);
//ISpecification<TEntity> AndNot(ISpecification<TEntity> other);
//ISpecification<TEntity> Not();
Expression<Func<TEntity, bool>> GetExpression();
}
public abstract class Specification<TEntity> : ISpecification<TEntity>
{
public bool IsSatisfiedBy(TEntity obj)
{
return this.GetExpression().Compile()(obj);
}
public abstract Expression<Func<TEntity, bool>> GetExpression();
}
public class OrderInfoSpecification : Specification<OrderInfo>
{
//private readonly OrderInfo order;
//public OrderInfoSpecification(OrderInfo order)
//{
// this.order = order;
//}
private readonly int userId;
public OrderInfoSpecification(int userId)
{
this.userId = userId;
}
public override Expression<Func<OrderInfo, bool>> GetExpression()
{
return x => x.UserID == userId;
}
}
public static class SimulateDataBase
{
private static List<OrderInfo> orderInfoList = new List<OrderInfo>(); public static IList<OrderInfo> OrderInfoList
{
get { return orderInfoList; }
} public static void Add(OrderInfo order)
{
orderInfoList.Add(order);
}
}
规约模式Specification的学习的更多相关文章
- 规约模式(Specification Pattern)
前期准备之规约模式(Specification Pattern) 一.前言 在专题二中已经应用DDD和SOA的思想简单构建了一个网上书店的网站,接下来的专题中将会对该网站补充更多的DDD的内容.本专题 ...
- [.NET领域驱动设计实战系列]专题三:前期准备之规约模式(Specification Pattern)
一.前言 在专题二中已经应用DDD和SOA的思想简单构建了一个网上书店的网站,接下来的专题中将会对该网站补充更多的DDD的内容.本专题作为一个准备专题,因为在后面一个专题中将会网上书店中的仓储实现引入 ...
- 规约模式Specification Pattern
什么是规约模式 规约模式允许我们将一小块领域知识封装到一个单元中,即规约,然后可以在code base中对其进行复用. 它可以用来解决在查询中泛滥着GetBySomething方法的问题,以及对查询条 ...
- 规约模式(Specification Pattern)
一.引言 最近在看一个项目的源码时(DDD),对里面的一些设计思想和设计思路有了一些疑问.当看到(Repository层)中使用了 spec.SatisfiedBy() 时,感觉有点懵.于是在项目中搜 ...
- step_by_step_ABP规约模式
一段时间没有在github 上浏览ABP项目,几天前看到ABP新增规约模式,开始了解并学习文档 记录一下 Introduction 介绍 Specification pattern is a pa ...
- 设计模式:规约模式(Specification-Pattern)
"其实地上本没有路,走的人多了,也便成了路"--鲁迅<故乡> 这句话很好的描述了设计模式的由来.前辈们通过实践和总结,将优秀的编程思想沉淀成设计模式,为开发者提供了解决 ...
- 生产环境下实践DDD中的规约模式
最近的开发工作涉及到两个模块“任务”和“日周报”.关系是日周报消费任务,因为用户在写日周报的时候,需要按一定的规则筛选当前用户的任务,作为日周报的一部分提交.整个项目采用类似于Orchard那种平台加 ...
- [.NET领域驱动设计实战系列]专题五:网上书店规约模式、工作单元模式的引入以及购物车的实现
一.前言 在前面2篇博文中,我分别介绍了规约模式和工作单元模式,有了前面2篇博文的铺垫之后,下面就具体看看如何把这两种模式引入到之前的网上书店案例里. 二.规约模式的引入 在第三专题我们已经详细介绍了 ...
- DDD~领域服务的规约模式
回到目录 规 约(Specification)模式:第一次看到这东西是在microsoft NLayer项目中,它是微软对DDD的解说,就像petshop告诉了我们MVC如何使用一样,这个规约模式最重 ...
随机推荐
- C# byte[]、struct、intptr等的相互转换
1.struct byte[]互相转换 //struct转换为byte[] public static byte[] StructToBytes(object structObj) { int siz ...
- mate标签
<meta charset='utf-8'> <!-- 优先使用 IE 最新版本和 Chrome --> <meta http-equiv="X-UA-C ...
- HTML a标签、4个伪类、常用属性(下载)、锚链接(待扩展:邮件、电话、短信、GPS)
HTML 超链接<a> 1.超链接可以是一个字.一个词.一组词.一幅图像,您可以点击这些内容来跳转到新的文档或者当前文档中的某个部分. 2.当您把鼠标指针移动到网页中的某个链接上时,箭头会 ...
- [Head First设计模式]生活中学设计模式——状态模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
- WampServer数据库导入sql文件
WampServer中MySQL如何导入sql文件: http://jingyan.baidu.com/article/3c343ff7f9c7940d377963c0.html
- HDU 5023 A Corrupt Mayor's Performance Art 线段树区间更新+状态压缩
Link: http://acm.hdu.edu.cn/showproblem.php?pid=5023 #include <cstdio> #include <cstring&g ...
- 页面localStorage用作数据缓存的简易封装
最近做了一些前端控件的封装,需要用到数据本地存储,开始采用cookie,发现很容易就超过了cookie的容量限制,于是改用localStorage,但localStorage过于简单,没有任何管理和限 ...
- angularjs 笔记(1) -- 引导
首先: 1,引入angularJS文件,<script type="text/javascript" src="angularjs.min.js"> ...
- 虚拟机ping不通主机,但是主机可以ping通虚拟机
http://chris2013.blog.51cto.com/6931081/1209278
- Effective Python2 读书笔记2
Item 14: Prefer Exceptions to Returning None Functions that returns None to indicate special meaning ...