简体   繁体   English

此C#代码有什么作用?

[英]What does this C# code do?

    public class AccountMembershipService : IMembershipService
    {
        private readonly MembershipProvider _provider;

        public AccountMembershipService()
            : this(null)
        {
        }

I took this bit of code from the AccountModels.cs class automatically created with the MVC3 project. 我从MVC3项目自动创建的AccountModels.cs类中获取了这段代码。

Can you explain what the 'this(null)' bit is doing? 您能解释一下“ this(null)”位在做什么吗?

It will call the single-argument constructor for AccountMembershipService , passing a null as the argument, before processing the body of the constructor you list. 在处理您列出的构造函数的主体之前,它将调用AccountMembershipService的单参数构造函数,并传递一个null作为参数。

From MSDN : MSDN

A constructor can invoke another constructor in the same object by using the this keyword. 一个构造函数可以使用this关键字在同一对象中调用另一个构造函数。 Like base, this can be used with or without parameters, and any parameters in the constructor are available as parameters to this, or as part of an expression. 像base一样,可以在有或没有参数的情况下使用它,并且构造函数中的任何参数都可以用作此参数或作为表达式的一部分。

More detail in section 17.10.1 (Constructor initializers) of the C# spec . 有关详细信息,请参见C#规范的 17.10.1节(构造函数初始化器)。

There is most likely another constructor in your class that looks like this: 您的类中最有可能是另一个构造函数,如下所示:

public AccountMembershipService(MembershipProvider provider)
{
    _provider = provider;
}

Your code calls this constructor first and passes null as the argument for provider , then executes the original constructor. 您的代码首先调用此构造函数,并将null作为provider的参数传递,然后执行原始的构造函数。

它使用null参数调用其他构造函数。

The above answers are answering the question asked, but to go a bit further: 上面的答案正在回答所问的问题,但要进一步说明:

This is one technique we use for inversion of control and making unit testing possible. 这是我们用于控制反转和使单元测试成为可能的一种技术。

Here are both constructors 这都是构造函数

public AccountMembershipService()
    : this(null)
{
}

public AccountMembershipService(MembershipProvider provider)
{
    _provider = provider ?? Membership.Provider;
}

The first constructor with :this(null) calls your 2nd constructor, passing null to the parameter provider. 具有:this(null)的第一个构造函数调用您的第二个构造函数,将null传递给参数提供程序。

One reason for doing this is to avoid duplication of logic. 这样做的原因之一是避免逻辑重复。 Suppose you did: 假设您做了:

public AccountMembershipService()
{
    _provider = Membership.Provider;
}

public AccountMembershipService(MembershipProvider provider)
{
    _provider = provider;
}

While perfectly reasonable, if you change the name of _provider, or perhaps add some other initialization code, you'd have to modify it in 2 places. 虽然完全合理,但是如果您更改_provider的名称,或者添加其他初始化代码,则必须在2个地方进行修改。 By calling :this(null), now all your work just happens in one place. 通过调用:this(null),现在您所有的工作都只发生在一个地方。

The reason we have 2 constructors is, by calling the default constructor, the static instance Membership.Provider gets used. 我们有2个构造函数的原因是,通过调用默认构造函数,将使用静态实例Membership.Provider。 Unfortunately it would be very difficult to unit test, because now we have dependencies on the membership providers, on the database, on having valid data, etc. 不幸的是,很难进行单元测试,因为现在我们依赖于成员资格提供者,数据库,具有有效数据等。

Instead, by creating the 2nd constructor, we can now pass in a mock MembershipProvider. 相反,通过创建第二个构造函数,我们现在可以传递一个模拟MembershipProvider。 This is an entirely different topic though, so if you're interested in how that works, feel free to ask another question. 但是,这是一个完全不同的主题,因此,如果您对它的工作方式感兴趣,请随时问另一个问题。

It calls another constructor in your class and passes null as a parameter. 它在您的类中调用另一个构造函数,并传递null作为参数。

You can also write : base(...) to explicitly call a constructor in your base class. 您还可以编写: base(...)在基类中显式调用构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM