开发者

What does this C# code do?

开发者 https://www.devze.com 2023-02-16 09:20 出处:网络
public class AccountMembershipService : IMembershipService { private readonly MembershipProvider _provider;
    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.

Can you explain what the 'this(null)' bit is doing?


It will call the single-argument constructor for AccountMembershipService, passing a null as the argument, before processing the body of the constructor you list.

From MSDN:

A constructor can invoke another constructor in the same object by using the this keyword. 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.

More detail in section 17.10.1 (Constructor initializers) of the C# spec.


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.


It calls a different constructor with a null argument.


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.

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. By calling :this(null), now all your work just happens in one place.

The reason we have 2 constructors is, by calling the default constructor, the static instance Membership.Provider gets used. 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. 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.

You can also write : base(...) to explicitly call a constructor in your base class.

0

精彩评论

暂无评论...
验证码 换一张
取 消