Properties vs. Attributes
http://blogs.msdn.com/b/ericlippert/archive/2009/02/02/properties-vs-attributes.aspx
Here is yet another question I got from a C# user recently:
I have a class that represents a business rule. I want to add some rule metadata that could be used by consumers to retrieve a friendlier rule name, description, and anything else that makes sense. Should this information be exposed as an attribute or property on the class?
I would say to absolutely go for a property in this case, for four reasons.
First, properties are highly discoverable. Consumers of this class can use IntelliSense to see that there is a property on the class called "Description" much more easily than they can see that there is an attribute.
Second, properties are much easier to use than attributes. You don't want to muck around with the code to extract strings from metadata attributes unless you really have to.
Third, data such as names and descriptions is highly likely to be localized in the future. Making it a property means that the property getter code can read the string out of a resource, a resource which you can hand off to your localization experts when it comes time to ship the Japanese version.
And fourth, let's go back to basic object-oriented design principles. You are attempting to model something -- in this case, the class is modeling a "business rule".
Note that a business rule is not a class. Nor is a rule an interface. A rule is neither a property nor a method. A rule isn't anyprogramming language construct. A rule is a rule; classes and structs and interfaces and whatnot are mechanisms that we use to implement model elements that represent the desired semantics in a manner that we as software developers findamenable to our tools. But let's be careful to not confuse the thing being modeled with the mechanisms we use to model it.
Properties and fields and interfaces and classes and whatnot are part of the model; each one of those things should represent something in the model world. If a property of a "rule" is its "description" then there should be something in the model that you're implementing which represents this. We have invented properties specifically to model the "an x has the property y" relationship, so use them.
That's not at all what attributes are for. Think about a typical usage of attributes:
[Obsolete]
[Serializable]
public class Giraffe : Animal
{ ...
Attributes typically do not have anything to do with the semantics of the thing being modeled. Attributes are facts about the mechanisms - the classes and fields and formal parameters and whatnot. Clearly this does not mean "a giraffe has anobsolete and a serializable." This also does not mean that giraffes are obsolete or serializable. That doesn't make any sense. This says that the class named Giraffe is obsolete and the class named Giraffe is serializable.
In short: use attributes to describe your mechanisms, use properties to model the domain.
总结:
使用attribute来描述机制,原理
使用property模拟领域
Properties vs. Attributes的更多相关文章
- jquery 使用attr() 函数对复选框无效的原因,javascript那些事儿——properties和attributes
复选框是网站开发的时候经常用到的网页标签之一,常见的在页面上对复选框的操作包括取值和修改复选框的状态.在jquery中,常见的操作标签的值得函数为attr,然而在操作复选框的时候,通常采用的却是pro ...
- 剖析AngularJS作用域
一.概要 在AngularJS中,子作用域(child scope)基本上都要继承自父作用域(parent scope). 但,事无绝对,也有特例,那就是指令中scope设置项为对象时,即scope: ...
- 也许你需要点实用的-Web前端笔试题
之前发的一篇博客里没有附上答案,现在有空整理了下发出来,希望能帮助到正在找工作的你,还是那句话:技术只有自己真正理解了才是自己的东西,共勉. Web前端笔试题 Html+css 1.对WEB标准以及w ...
- 深入理解JS的delete
原文链接: Understanding delete原文作者: Kangax原文日期: 2010年01月10日 翻译日期: 2014年02月07日 翻译人员: 铁锚 !!!!!!!!草稿版本的翻译完成 ...
- 【译】微型ORM:PetaPoco【不完整的翻译】
PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...
- jquery checkbox勾选/取消勾选的诡异问题
<form> 你爱好的运动是?<input type="checkbox" id="CheckedAll" />全选/全不选<br ...
- 《驾驭Core Data》 第三章 数据建模
本文由海水的味道编译整理,请勿转载,请勿用于商业用途. 当前版本号:0.1.2 第三章数据建模 Core Data栈配置好之后,接下来的工作就是设计对象图,在Core Data框架中,对象图被表 ...
- $('#checkbox').attr('checked'); 返回的是checked或者是undefined解决办法
$('#checkbox').attr('checked'); 返回的是checked或者是undefined解决办法 <input type='checkbox' id='cb'/> ...
- jquery checkbox勾选取消勾选的诡异问题
jquery checkbox勾选/取消勾选的诡异问题jquery checkbox勾选/取消勾选的诡异问题 <form> 你爱好的运动是?<input type=&q ...
随机推荐
- ACM——A + B Problem (1)
A + B Problem (1) 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:5907 测试通过:151 ...
- Struts2 多文件下载
Step1:导入支持jar包 commons-fileupload-1.3.1.jar commons-io-2.4.jar jstl-1.2.jar standard-1.1.2.jar commo ...
- OC - 2.OC基础知识介绍
一.基础语法 1> OC语言和C语言 C语言是面向过程的语言,OC语言是面向对象的语言 OC语言继承了C语言,并增加了面向对象的思想 以下内容只介绍OC语言与C语言的不同之处 2> 关键字 ...
- Mac OS X 在Finder新建文本文件
Automator 新建一个 Application添加一个动作 "Run AppleScript"代码如下 on run {input, parameters} tell app ...
- 10.14_魅族手机音乐播放无故暂停,MetroUICss-tile中的字如何居中
(1)魅族手机,播放音乐,无故暂停.不管是自带的音乐播放程序,还是下载的其他音乐播放器都是如此.而且,手机上有个Google服务耗电量非常高,会经常弹出登陆Google账号的弹出项. (2)Metro ...
- 给python类动态添加方法(method)
群里有人问如何做到 def foo(): pass class Bar(object): pass Bar.set_instance_method(foo) b = Bar() b.foo() 这个其 ...
- EF支持复杂类型的实现
本节,将介绍如何手动构造复杂类型(ComplexType)以及复杂类型的简单操作.通常,复杂类型是指那些由几个简单的类型组合而成的类型.比如:一张Customer表,其中有FristName和Last ...
- js生成随机字符串或者随机数
//返回一个指定范围内的随机数 function createRandomNum(Min,Max){ let Range = Max - Min; let Rand = Math.random(); ...
- 基于jQuery的图片左右轮播,基本原理通用
毕竟新人,写点基础的小东西,希望能和大家沟通交流,提高自己的水平. 这个是应用较多的轮播部分,希望能和大家分享一下思路,拓宽视野. 话不多说,上内容. 我的思路很简单就是通过判断index值的大小变化 ...
- PhoneGap与WAP站静态化
最近在参与的WAP站项目,决定将所有页面都静态化处理,登录验证.价格数据等都通ajax动态的方式实现.开始这么规划的目前是为了页面提高网站加载速度及SEO,最近看到了一篇报道phonegap buil ...