English 中文(简体)
合同链条电话?
原标题:Code Contracts chaining calls?

基本情况是一米,看2种不同情况:

同一类别的方法:

public class MyClass
{
    public Bar GetDefaultBar(Foo foo)
    {
        Contract.Requires(foo != null);

        return GetSpecificBar(foo, String.Empty);
    }

    public Bar GetSpecificBar(Foo foo, string name)
    {
        Contract.Requires(foo != null);
        Contract.Requires(name != null);

        ...
    }
}

不同类别的方法:

public class MyClass
{
    private MyBarProvider { get; set; }

    public Bar GetDefaultBar(Foo foo)
    {
        Contract.Requires(foo != null);

        return BarProvider.GetSpecificBar(foo, String.Empty);
    }

    //Object-Invariant ensures that MyBarProvider is never null...
}

public class MyBarProvider
{
    public Bar GetSpecificBar(Foo foo, string name)
    {
        Contract.Requires(foo != null);
        Contract.Requires(name != null);

        ...
    }
}

我不禁要问,是否有必要为这两种情况订立重复合同? 我猜测,在第一个例子(在同一类别之内)中,也许有办法避免这种情况,但在第二个例子中(不同类别)。 而且,我是否应当避免重复,或者是否应该这样做?

最佳回答

我假定,你重复谈论的是:Contract.Requires(foo != 无);

在这两种情况下,你们都需要在这两种方法中都这样做。 如果你没有把电话列入电话,你就会在电话站获得“不要求”的服务,因为电话方法的打电话者可能会失去价值。 如果你没有将合同列入被征召者,被征召者的合同就包含非票要求,合同分析员将认为 f可能无效。 这是因为另一个电话站可以自由通过,无任何价值。

http://www.ohchr.org。

As Ɖiamond Ǥeeze Ʀ 指出,你可以使用,将重复性合同电话改为另一种方法。 该属性不属于4.0.NET框架的一部分,但你可以界定这一框架。 详情见Terje Sandstrom s blog 员额,内容是协调编码合同,不与No.Cop检查:

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签