您对托管合同工具库有何看法
我最近看到这个视频 http://channel9.msdn.com/pdc2008/TL51/ 有关托管合同工具的信息图书馆看起来确实很有趣。 遗憾的是,他们似乎不会将其包含到语言本身中,这会像 Spec# 中那样更加优雅。 事实上,在 C#4.0 中拥有这两个选项就好了,因为合同会给业务代码增加很多噪音。
这里有人使用过它并有一些现实世界的反馈吗? 您还可以将契约添加到类 Properties 甚至变量中吗? 类似的东西
decimal Percentage (min 0, max 1)
string NotNullString (not null, regex("??"))
也许会很好。
I recently saw this video
http://channel9.msdn.com/pdc2008/TL51/ about the managed Contract tools library which certainly looks very interesting. Sadly it seems they won't include this into the language itself which would have been more elegant as in Spec#. It would be nice to have is as both options in C#4.0 in fact as the Contracts adds a lot of noise to the business code.
Have anybody here used it and have some real world feedback? Could you also add contracts to class Properties and even variables? Something like
decimal Percentage (min 0, max 1)
string NotNullString (not null, regex("??"))
would maybe been nice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在尝试,但我认为该库还太年轻,无法在大型项目中认真使用,至少在启用静态检查的情况下是这样:编译非常慢,而且具体的警告读起来不是很清楚。
运行时检查可以毫无问题地使用,因为它们似乎是作为 Debug.Assert 实现的。 至少你记录了方法。
为了向属性添加契约,我会向设置的属性添加约束,但在这种特定情况下,我认为最好编写一个可以实际封装需求的类,只允许构造好的对象。 无论如何:
ps:在我看来,C# 的趋势是朝着动态类型的方向发展,而不是走向严格和强类型的编码方法。 我认为 DbC 在强类型方面效果更好,至少因为它允许您添加更多要求类型和功能。
I'm trying it, but I think that the library is too young to be used seriously on big projects, at least with the static checks enabled: compiling is very slow and the specific warning are not very clear to read.
Runtime checkings can be used without problems, because they seems to be implemented as Debug.Assert. At least you document methods.
In order to add contracts to properties i would add constraints to the set property, but in this specific case I think it would be better writing a class that could actually encapsulate the requirements, allowing the construction of good objects only. Anyway:
p.s: It seems to me that the trend in C# goes in the dynamic typing direction, instead of going to the strict and strong typing coding methods. I think that DbC works better with strong typing, at least because it let you add more requirements to types and to functions.
看起来 Contract Library 非常适合 Linq2Sql。 在您的 SQL 数据库中定义字段和约束,并且可以为您生成合同。 这可能是对合约的一个很好的介绍。
It seems the Contract Library could be a nice fit for Linq2Sql. Define fields and constraint in your sql database and contracts could be generated for you. This could be a nice introduction to Contracts.