Combining code contracts and regex

五迷三道 提交于 2019-12-06 08:15:21

Another way is avoid 'primitive obsession' and use a class tailored to your purpose, e.g.:

public SimpleClass(Email address)
{
    // no need to check, it must be valid :)
}

... and then encapsulate all your validation logic in the Email class. You'll still have the "string format" issues about validation, but I think a better idiom for this is to create a method called Email.TryParse, and fashion it along the lines of int.TryParse.

Just mark IsValid as public and you'll be fine. All "components" of a public surface contract have to be public as well, otherwise there is no way for a caller to check that the contract is satisfied.

@AI-CII I understand that, but that would be a design flaw as well, exposing implementation details to consumers.

  1. A contract on a public method is not an implementation detail. A Contract.Requires says "hey, I require this to be true for me to do some work for you." If "this" isn't visible to the caller, how can the caller verify that the contract is satisfied?

  2. You aren't exposing the implementation details of the method IsValid, you are only exposing what must be satisfied for the callee to do its job.

As Jason already stated, Code Contracts requires the method to be public as you already figured out yourself thanks to the exception message.

I understand however that simply making it public doesn't feel right. Perhaps the regex condition can be encapsulated to a static global function of a helper class?

E.g. If it were to check whether a string is a valid URL.

UrlHelper.IsValidUrl( string url )

This interested me so I started doing some googling. There is a solution! Although I would still prefer the helper class with the static method where possible.

It is called Code Contract Abbreviators. You need to include the sourcefile to your project yourself however.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!