最近一直在看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的学习的更多相关文章

  1. 规约模式(Specification Pattern)

    前期准备之规约模式(Specification Pattern) 一.前言 在专题二中已经应用DDD和SOA的思想简单构建了一个网上书店的网站,接下来的专题中将会对该网站补充更多的DDD的内容.本专题 ...

  2. [.NET领域驱动设计实战系列]专题三:前期准备之规约模式(Specification Pattern)

    一.前言 在专题二中已经应用DDD和SOA的思想简单构建了一个网上书店的网站,接下来的专题中将会对该网站补充更多的DDD的内容.本专题作为一个准备专题,因为在后面一个专题中将会网上书店中的仓储实现引入 ...

  3. 规约模式Specification Pattern

    什么是规约模式 规约模式允许我们将一小块领域知识封装到一个单元中,即规约,然后可以在code base中对其进行复用. 它可以用来解决在查询中泛滥着GetBySomething方法的问题,以及对查询条 ...

  4. 规约模式(Specification Pattern)

    一.引言 最近在看一个项目的源码时(DDD),对里面的一些设计思想和设计思路有了一些疑问.当看到(Repository层)中使用了 spec.SatisfiedBy() 时,感觉有点懵.于是在项目中搜 ...

  5. step_by_step_ABP规约模式

    一段时间没有在github 上浏览ABP项目,几天前看到ABP新增规约模式,开始了解并学习文档   记录一下 Introduction 介绍 Specification pattern is a pa ...

  6. 设计模式:规约模式(Specification-Pattern)

    "其实地上本没有路,走的人多了,也便成了路"--鲁迅<故乡> 这句话很好的描述了设计模式的由来.前辈们通过实践和总结,将优秀的编程思想沉淀成设计模式,为开发者提供了解决 ...

  7. 生产环境下实践DDD中的规约模式

    最近的开发工作涉及到两个模块“任务”和“日周报”.关系是日周报消费任务,因为用户在写日周报的时候,需要按一定的规则筛选当前用户的任务,作为日周报的一部分提交.整个项目采用类似于Orchard那种平台加 ...

  8. [.NET领域驱动设计实战系列]专题五:网上书店规约模式、工作单元模式的引入以及购物车的实现

    一.前言 在前面2篇博文中,我分别介绍了规约模式和工作单元模式,有了前面2篇博文的铺垫之后,下面就具体看看如何把这两种模式引入到之前的网上书店案例里. 二.规约模式的引入 在第三专题我们已经详细介绍了 ...

  9. DDD~领域服务的规约模式

    回到目录 规 约(Specification)模式:第一次看到这东西是在microsoft NLayer项目中,它是微软对DDD的解说,就像petshop告诉了我们MVC如何使用一样,这个规约模式最重 ...

随机推荐

  1. Windows 7 激活时的坑

    前段时间,桌面上有两个文件用各种方法删除不了. 然后今天终于进了PE系统,使用DG把这两货干掉了. 重启进入Windows,提示我 不是正版,今天必须激活,桌面变成了一片黑... 打开小马激活工具OE ...

  2. [Centos 6]升级安装GCC(2)

    摘要 上篇文章升级了下gcc,但发现并没有起到作用. 安装 上篇文章: 升级GCC 升级之后,检查gcc版本 strings /usr/lib/libstdc++.so. | grep GLIBCXX ...

  3. Tortoise SVN 版本控制常用操作汇总(show log)

    1.如何查看SVN上当前代码库的最新版本号是多少? 打开右键菜单中的 show log,然后看到一系列版本更新历史,最上面的那一行,即是最新版本号,所谓的 head revision. 2.如何查看本 ...

  4. PHP判断变量是否存在及函数isset() 、empty()与is_null的区别

    一.举例说明 A.如何判断一个变量是否定义? <?php // 假设不存在$test 变量 if (isset($test)) { echo '$test 已经set', '<br/> ...

  5. javascript基础02

    javascript基础02 1.数据类型 数据类型的描述在上篇的扩展中有写到链接 由于ECMAScript数据类型具有动态性,因此的确没有再定义其他数据类型的必要.这句话很重要. 如果以后再数据类型 ...

  6. C语言中史上最愚蠢的Bug

    C语言中史上最愚蠢的Bug   本文来自“The most stupid C bug ever”,很有意思,分享给大家.我相信这样的bug,就算你是高手你也会犯的.你来看看作者犯的这个Bug吧.. 首 ...

  7. XmlException: 名称不能以“<”字符(十六进制值 0x3C)开头

    svn 版本冲突引起的: 产看config 文件里面的内容是否冲突,这个问题可能和代码无关

  8. 使用jvisualvm.exe 的Btrace插件介绍/使用教程

    一.背景        在生产环境中可能经常遇到各种问题,定位问题需要获取程序运行时的数据信息,如方法参数.返回值.全局变量.堆栈信息等.为了获取这些数据信息,我们可以 通过改写代码,增加日志信息的打 ...

  9. C#通过事件跨类调用WPF主窗口中的控件

    xaml.cs文件: using System; using System.Timers; using System.Windows; using System.Windows.Forms; name ...

  10. 【python】描述符descriptor

    开始看官方文档,各种看不懂,只看到一句Properties, bound and unbound methods, static methods, and class methods are all ...