.NET Guard Class Library?

限于喜欢 提交于 2019-11-28 18:24:17

There is CuttingEdge.Conditions. Usage example from the page:

public ICollection GetData(Nullable<int> id, string xml, ICollection col)
{
    // Check all preconditions:
    id.Requires("id")
        .IsNotNull()          // throws ArgumentNullException on failure
        .IsInRange(1, 999)    // ArgumentOutOfRangeException on failure
        .IsNotEqualTo(128);   // throws ArgumentException on failure

    xml.Requires("xml")
        .StartsWith("<data>") // throws ArgumentException on failure
        .EndsWith("</data>"); // throws ArgumentException on failure

    col.Requires("col")
        .IsNotNull()          // throws ArgumentNullException on failure
        .IsEmpty();           // throws ArgumentException on failure

    // Do some work

    // Example: Call a method that should not return null
    object result = BuildResults(xml, col);

    // Check all postconditions:
    result.Ensures("result")
        .IsOfType(typeof(ICollection)); // throws PostconditionException on failure

    return (ICollection)result;
}

Another nice approach, which isn't packaged in a library, but could easily be, on Paint.Net blog:

public static void Copy<T>(T[] dst, long dstOffset, T[] src, long srcOffset, long length)
{
    Validate.Begin()
            .IsNotNull(dst, "dst")
            .IsNotNull(src, "src")
            .Check()
            .IsPositive(length)
            .IsIndexInRange(dst, dstOffset, "dstOffset")
            .IsIndexInRange(dst, dstOffset + length, "dstOffset + length")
            .IsIndexInRange(src, srcOffset, "srcOffset")
            .IsIndexInRange(src, srcOffset + length, "srcOffset + length")
            .Check();

    for (int di = dstOffset; di < dstOffset + length; ++di)
        dst[di] = src[di - dstOffset + srcOffset];
}

I use it in my project and you could borrow the code from there.

Given Microsoft's Code Contracts coming out with .NET 4.0 I'd try to find one which is mostly compatible, if possible - and if not, write it yourself. That way when you upgrade to .NET 4.0 (eventually) the migration will be easier.

There are several methods you can use.

My favorite is using Aspect Oriented Programming. Check out PostSharp.

You can also take a look at Spec#, an extension to C#

In 4.0, you will have a full-featured contract library.

Finally, a collegue of mine has come up with a pretty useful guard library: http://blueonionsoftware.com/blog.aspx?p=ca49cb62-7ea2-43c5-96aa-91d11774fb48

I don't know of any that are commercially available. There is some support for this type of code in the patterns & practices Enterprise Library. There are also a lot of open source projects that appear to do this as well (to varying degrees) on CodePlex: http://www.codeplex.com/Project/ProjectDirectory.aspx?ProjectSearchText=validation.

Most of the time, these types of libraries end up being custom written and stay internal to the company that uses them.

There is support coming in .NET 4.0 to provide mechanisms to do this using Code Contracts, which are based on Spec#.

I recently wrote a post about guard classes (having not found any information either): http://ajdotnet.wordpress.com/2009/08/01/posting-guards-guard-classes-explained/

I also published a respective Guard class implementation (feel free to use this code as is, or to adjust it to your needs): ajdotnet.wordpress.com/guard-class/

With regard to the relationship between Guard classes and Code Contract in .NET 4.0 (successor to Spec#), have a look at the following post: www.leading-edge-dev.de/?p=438

(sorry for the fragmented links, the site only permitted one link...)

HIH, AJ.NET

Install the netfx-guard nuget package. You get also code snippets notnull and notempty, and it performs as fast as your manual checks

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