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 ...
随机推荐
- mysql嵌套查询
select * from(select t.`name` `name`,count(*) count from company t group by t.`name`) aa where aa.co ...
- 记一次ios使用OAuth 2.0写的接口获取token的小错
1.用ios原生网络请求的话,请求参数不能这样传 而要这样传 2.用afnetworking的话,要注意各个参数有没有错误,参数可以直接这样传
- iOS lanchImage 和icon的设置
1 icon的设置 打开项目中的Assets.xcassets 这里边有一个icon 首先需要有icon 的尺寸 尺寸如下: 29*29 2x 29*29 3x 40*40 2x 40* ...
- javascript变量,类型 第9节
1.变量,数据类型 <html> <head> <title>变量</title> </head> <body> <h3& ...
- Library vector Type
vector的定义 vector是C++标准库里最常用的容器,它之所以被称为容器,是因为它可以存放多个对象,所有在用一个容器中的对象都应该具有相同的类型. vector也是一个类模板,这也是它能存放多 ...
- HOW TO: Creating your MSI installer using Microsoft Visual Studio* 2008
Quote from: http://software.intel.com/en-us/articles/how-to-creating-your-msi-installer-using-visual ...
- ScrollView 尽量避免嵌套RelativeLayout,非常惨痛的教训
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...
- thinkphp 整合 ucenter
http://xcodebox.com/2013/06/8855.html 参考 1 ucenter源码目录下 /advanced/examples/api目录 copy到thinkphp项目根目 ...
- ubuntu上 安装 基于sphinx 的 coreseek 全文搜索
原生sphinx不支持中文, sphinx-for-chinese匹配中文时也不返回结果 ,真纠结, 最好试了 coreseek,这个能正确返回结果了, 所以记录一下 1 http://www.co ...
- git 401 错误
错误信息:error: The requested URL returned error: 401 Unauthorized while accessing https://git.oschina.n ...