需要 CodeContracts 的建议

发布于 2024-10-06 02:50:39 字数 857 浏览 6 评论 0原文

嗨,大家好 我是 CC 新手,需要您的建议。我在上一个项目中从 CC 开始。我有一份 WCF 合同,应由第三方执行。我想将代码合同分配给服务合同。假设我有一个 Car 类(服务合约),它是 OperationContract ICar。 ICar 有一个 GetCar() 方法,正如我上面所说,应该由我们的客户实现。我为 Icar 创建一个代码 Contract 类,并在 GetCar 方法中使用 CarContractHelper 类验证合同。我引入了一个帮助程序类,它通过以下方式验证 Car 类:Contract.Ensure(CarContractHelper.Validate(Contract.Result()))。这是解决问题的正确方法吗?或者有什么更好的办法吗? 我还在它的设置器中验证 Car 类的每个属性成员,您对此有何看法,是必要的还是矫枉过正? 谢谢 当然,我会标记正确的答案并投票:)

更新


@StriplingWarrior 我引入了汽车验证助手来处理重复的合同验证代码。例如:

Contract.Requires(!string.IsNullOrWhiteSpace(car.Id);
Contract.Requires(car.Mileage > 0);

等等。我没有到处编写 Contract.Requires 代码,而是为每个业务对象创建一个辅助类来验证业务对象的合同。这就是我想知道的,如果这是一个正确的方法,或者也许有更好的方法? 谢谢。

PS 我正在验证它们自己的 setter 中的每个属性,因为不变方法对我不起作用(?),据我所知,当调用类的任何方法时,不变方法都有效,在我的情况下,wcf 合同不起作用没有任何方法。所以我选择验证内部属性设置器。这是一个好方法吗?

Hi guys
I'm new to CC and I need your suggestion. I started with CC in my last project. I have a WCF contract, which should be implemented by third parties. I want to assign code contracts to service contracts. Let's say I have a class Car (Service Contract) and it's OperationContract ICar. ICar has a method GetCar() which as I said above, should be implemented by our clients. I create a code Contract class for Icar and in GetCar method, I validate the contract using CarContractHelper class. I introduced a helper class which validates the Car class the following way: Contract.Ensure(CarContractHelper.Validate(Contract.Result<Car>())). Is this a correct approach to solve the problem? Or is there any better way?
I'm also validating each property member of Car class in it's setters, what do you think about this, is it necessary or it's an overkill?
Thanks
And of course, I will mark the correct answer and vote up :)

UPDATE


@StriplingWarrior I introduced the car validation helper to deal with the repetitive contract validation code. for example:

Contract.Requires(!string.IsNullOrWhiteSpace(car.Id);
Contract.Requires(car.Mileage > 0);

etc. Instead of writing everywhere Contract.Requires code, I just have one helper class for each business object which validates the business object's contract. And that's what I want to know, if this is a correct approach or maybe there is a better way?
thanks.

P.S. I'm validating every property in their own setters because invariant method won't work for me (?), as far as I know, invariants works when any method of the class is called, and in my case, wcf contract doesn't have any methods. So I choose to validate inside properties setter. Is it a good approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

地狱即天堂 2024-10-13 02:50:39

我没有对代码契约做太多研究,但这里有一些观察结果:

代码契约的优点之一是能够查看特定方法的契约并了解预期内容。看到 CarContractHelper.Validate(Contract.Result()) 并没有真正告诉我任何信息,所以我必须深入了解(例如)返回值的 CarId 大于零。因此,在某种程度上,我宁愿看到 Contract.Ensure(Contract.Result().CarId > 0),也许还有一些其他要求。另一方面,我可以看到这会如何创建大量重复的合约代码,如果(例如)您向 Car 类添加了新属性,这些代码将很难管理。

代码契约的另一个潜在优势是能够对您的需求进行编译时检查。例如,考虑以下代码:

var car = _carService.GetCar();
_crashTestUtil.TestCar(car);

如果 TestCar 的合同要求 car.CarId > 0,并且 GetCar() 未能确保情况确实如此,编译器可以警告您这一事实。为了使其与验证帮助器类一起使用,我想您需要确保您的验证帮助器类使用代码契约方法来进行契约检查。

即使您正在对 Car 类的属性设置器执行检查,我确实认为在此方法返回之前验证所有必需的值是否已设置仍然有意义。

I haven't done much with Code Contracts, but here are some observations:

One of the advantages of code contracts is the ability to look at the contract for a particular method and know what's expected. Seeing CarContractHelper.Validate(Contract.Result<Car>()) doesn't really tell me anything, so I would have to drill down to know that (for example) the return value's CarId is greater than zero. So in one way I would rather see Contract.Ensure(Contract.Result<Car>().CarId > 0), and maybe a few other requirements. On the other hand, I can see how this could create a lot of repetitive contract code that would be hard to manage if (for instance) you added a new property to the Car class.

Another potential advantage of code contracts is the ability to have compile-time checks on your requirements. For example, consider the following code:

var car = _carService.GetCar();
_crashTestUtil.TestCar(car);

If TestCar's contract requires that car.CarId > 0, and GetCar() failed to ensure that this was the case, the compiler can warn you about this fact. In order for this to work with a validation helper class I imagine that you'll need to make sure your validation helper class also uses code contract methods to do your contract checks.

Even though you are performing checks on the Car class's property setters, I do think it still makes sense to validate that all required values have been set before this method returns.

请别遗忘我 2024-10-13 02:50:39

虽然代码契约可以用于此目的,但我个人不会以这种方式使用它。我认为纯粹的验证属于领域层(假设您使用某种领域驱动的方法),并且汽车应该能够在有或没有代码契约的情况下进行验证。

在我看来,如果您的 API 处理无效的汽车或抛出适当的异常,那就更好了。如果您使用代码合约来检查汽车的有效性,您只有 2 个选项:1. 汽车有效 2. 汽车无效,ContractException(或您自己的异常)。

我是代码合同的支持者,但宁愿看到合同更详细。您绝对应该尝试使不变式发挥作用。另一种方法是在您的界面上定义合约。

Altough Code Contracts can be used for this, I personally would not use it this way. I think pure validation belongs to the Domain Layer ( provided you're using a somewhat Domain Driven approach) , and the car should be able to be validated with or without Code Contracts.

In my opinion, it would be better if your API handled the invalid car or threw an appropriate exception. If you're using Code Contracts to check the car's validity, you only have 2 options: 1. Car is valid 2. Car is invalid, ContractException ( or your own exception).

I'm pro-Code Contracts, but would rather see the Contracts more verbose. You should definitely try to make the invariant work. An alternative is to define Contracts on your interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文