문제

Null 인수 확인과 같은 가드 메소드를 제공하는 라이브러리 또는 소스 코드를 찾고 있습니다. 분명히 이것은 제작이 다소 간단하지만, 이미 .NET을위한 것이 있는지 궁금합니다. 기본적인 Google 검색은별로 공개되지 않았습니다.

도움이 되었습니까?

해결책

거기 있습니다 Cuttingedge.conditions. 페이지에서 사용 예 :

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;
}

도서관에 포장되지 않았지만 쉽게 가능할 수있는 또 다른 멋진 접근 방식은 Paint.net 블로그에서:

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];
}

나는 그것을 사용한다 내 프로젝트 그리고 당신은 거기에서 코드를 빌릴 수 있습니다.

다른 팁

Microsoft가 주어졌습니다 코드 계약 .NET 4.0과 함께 나오면 가능하면 대부분 호환되는 것을 찾으려고 노력할 것입니다. 이렇게하면 .NET 4.0 (결국)으로 업그레이드하면 마이그레이션이 더 쉬워집니다.

사용할 수있는 몇 가지 방법이 있습니다.

내가 가장 좋아하는 것은 측면 지향 프로그래밍을 사용하는 것입니다. PostSharp를 확인하십시오.

C#에 대한 확장자 인 Spec#을 볼 수도 있습니다.

4.0에는 완전한 기능을 갖춘 계약 라이브러리가 있습니다.

마지막으로, 내 대학은 매우 유용한 가드 라이브러리를 내놓았습니다.http://blueonionsoftware.com/blog.aspx?p=CA49CB62-7EA2-43C5-96AA-91D11774FB48

나는 상업적으로 구할 수있는 것을 모른다. Patterns & Practices Enterprise Library에는 이러한 유형의 코드에 대한 지원이 있습니다. CodePlex 에서이 작업을 수행하는 것으로 보이는 오픈 소스 프로젝트도 많이 있습니다. http://www.codeplex.com/project/projectdirectory.aspx?projectsearchtext=validation.

대부분의 경우, 이러한 유형의 라이브러리는 맞춤형 서면으로 작성되었으며이를 사용하는 회사 내부에 머물러 있습니다.

.NET 4.0에서 제공되는 지원이 있습니다. 코드 계약, 사양#을 기반으로합니다.

최근에 가드 클래스에 관한 게시물을 썼습니다 (정보도 찾지 못했습니다). http://ajdotnet.wordpress.com/2009/08/01/posting-guards-guard-classes-emplained/

또한 각 가드 클래스 구현을 게시했습니다 (이 코드를 그대로 사용하거나 필요에 맞게 조정하십시오) : ajdotnet.wordpress.com/guardclass/

.NET 4.0 (사양의 후속 자)의 가드 클래스와 코드 계약의 관계와 관련하여 다음 게시물을 살펴보십시오. www.leading-edge-dev.de/?p=438

(조각난 링크에 대해 죄송합니다. 사이트는 하나의 링크 만 허용했습니다 ...)

hih, aj.net

NetFX-Guard Nuget 패키지를 설치하십시오. 당신은 또한 코드 스 니펫을 얻지 못하고 무의미하고, 수동 점검만큼 빠르게 수행됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top