惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

博客园 - sinlight23

Web开发框架趋势 ASP.NET MVC - PageData的应用 ASP.NET拾遗 - Health Monitoring .NET Xml序列化时会忽略值为"默认值"的Property或Field ASP.NET MVC 实现模式 - ModelBuilder Enumerable.Range和自定义的IntRange/DateRange HOWTO: Web Deploy时服务器报登录失败的解决方法 HOWTO: 为GitHub for Windows指定代理服务器 ASP.NET MVC - 在MVC 3项目中使用ASP.NET Bundling and Minification机制 脚印:关于错误编码的管理的一些思考 脚印:软件开发随想录 脚印:关于扩展方法的使用 脚印:一次重构讨论 HOWTO: IE8下处理iframe自适应高度 Microsoft ASP.NET 2.0 AJAX 相关信息备忘 MVC 模式在javascript中的应用 VS2010 "SQL Server 2005 Database Project" 使用笔记(二) VS2010 "SQL Server 2005 Database Project" 使用笔记 腳印: 初學者的心態
脚印:记录一次重构,将规则生产和规则消费(执行委托)分离
sinlight23 · 2011-07-19 · via 博客园 - sinlight23

重构前:

Cache.Add(DXCacheKey.A, Register(DXCacheKey.A, DXCacheKeyType.A, dal.GetItemA, dal.GetItemAById)); Cache.Add(DXCacheKey.B, Register(DXCacheKey.B, DXCacheKeyType.B, dal.GetItemB, dal.GetItemBById));
...
...

此次重构的目标是将规则的建立和使用进行分离(IDXCacheItemWrapper), 副产品是将某一形式的规则进行了封装(DXCacheItemWrapper)。

分离的好处:

一是,增强了代码的的可读性。因为这是个较为自然的过程,先创建规则,然后使用它。

二是,其他形式的规则,只要符合IDXCacheItemWrapper,也可以加入到计算任务中来。

三是,规则建立好后,不必立即使用,可以延迟计算。在此期间,可以对规则集进行筛选,增补等操作。

publicclass Class1
{
public Class1()
{
DAL dal
=new DAL();
List
<IDXCacheItemWrapper> list =new List<IDXCacheItemWrapper>
{
new DXCacheItemWrapper<ItemA, int>(DXCacheKey.A, DXCacheKeyType.A, dal.GetItemA, dal.GetItemAById),
new DXCacheItemWrapper<ItemB, int>(DXCacheKey.B, DXCacheKeyType.B, dal.GetItemB, dal.GetItemBById),
new DXCacheItemWrapper<ItemC, int>(DXCacheKey.C, DXCacheKeyType.C, dal.GetItemC, dal.GetItemCById),
};
foreach (IDXCacheItemWrapper w in list)
{
var value
= w.GetValue();
Cache.Add(w.Key.ToString(), value);
}
}

}

publicinterface IDXCacheItemWrapper
{
DXCacheKey Key {
get; set; }object GetValue();
}
publicclass DXCacheItemWrapper<T1, T2> : IDXCacheItemWrapper
{
public DXCacheItemWrapper(DXCacheKey key, DXCacheKeyType keyType, Func<T1> getData1, Func<T2, T1> getData2)
{
Key
= key;
KeyType
= keyType;
GetData1
= getData1;
GetData2
= getData2;
}
publicobject GetValue()
{
return GetValue<T1, T2>(Key, KeyType, GetData1, GetData2);
}
public T1 GetValue<T1, T2>(DXCacheKey key, DXCacheKeyType keyType, Func<T1> f1, Func<T2, T1> f2)
{
returndefault(T1);
}
public DXCacheKey Key { get; set; }
public DXCacheKeyType KeyType { get; set; }
public Func<T1> GetData1 { get; set; }
public Func<T2, T1> GetData2 { get; set; }
}