在C#中“契约式设计” [英] 'Design By Contract' in C#
问题描述
我想尝试通过合同一点点的设计在我最新的C#应用程序,并希望有类似的语法:
公共字符串美孚()
{
集合{
Assert.IsNotNull(值);
Assert.IsTrue(value.Contains(酒吧));
_foo =价值;
}
}
我知道我可以从一个单元测试框架这样的静态方法,但我想知道,如果这样的事情已经内置到语言,或者已经有某种框架左右浮动。我可以写我自己的断言功能,只是不想推倒重来。
C#4.0 code合约
块引用>微软在.NET Framework 4.0版发布了设计库合同。一个该库的最酷的功能是,它也配备了一个静态分析工具(类似的FxCop我猜),它利用放置在code合同的细节。
下面是一些微软的资源:
下面是其他一些资源:
I wanted to try a little design by contract in my latest C# application and wanted to have syntax akin to:
public string Foo() { set { Assert.IsNotNull(value); Assert.IsTrue(value.Contains("bar")); _foo = value; } }
I know I can get static methods like this from a unit test framework, but I wanted to know if something like this was already built-in to the language or if there was already some kind of framework floating around. I can write my own Assert functions, just don't want to reinvent the wheel.
解决方案C# 4.0 Code Contracts
Microsoft has released a library for design by contract in version 4.0 of the .net framework. One of the coolest features of that library is that it also comes with a static analysis tools (similar to FxCop I guess) that leverages the details of the contracts you place on the code.
Here are some Microsoft resources:
- The main Microsoft Research site
- The user manual
- The 2008 PDC presentation
- The 2009 PDC presentation
Here are some other resources:
- Code Contracts for .NET 4.0 - Spec# Comes Alive
- .NET Code Contracts and TDD Are Complementary
- Code Contracts Primer – Part 5: Utilizing Object Invariants
- Code Contracts Primer – Part 6 Interface Contracts
这篇关于在C#中“契约式设计”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!