Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you should do is create an empty membership provider that does nothing more than call into the MVC Dependency Resolver to fetch the real <code>Membership Provider</code>. By doing this you achieve three things:</p> <ul> <li>This implementation has a default constructor and can be configured in the web.config.</li> <li>The implementation is a MVC infrastructure component and is decoupled from the actual membership provider that contains the implementation (that you probably want to test).</li> <li>This infrastructure component has no dependency on Unity (or any other container), but just on MVC3 which lowers the coupling of the container itself.</li> </ul> <p>The implementation might look like this:</p> <pre><code>public class DependencyResolverMembershipProvider : MembershipProvider { private static MembershipProvider Provider { get { var provider = DependencyResolver.Current.GetService&lt;MembershipProvider&gt;(); if (provider == null) { throw new InvalidOperationException( "Make sure the MembershipProvider is registered."); } return provider; } } public override string ApplicationName { get { return Provider.ApplicationName; } set { Provider.ApplicationName = value; } } public override bool ChangePassword(string username, string oldPassword, string newPassword) { return Provider.ChangePassword(username, oldPassword, newPassword); } public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) { return Provider.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer); } public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { return Provider.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status); } // Implement all other methods here } </code></pre> <p>This is still a lot of work, since the <code>MembershipProvider</code> has a lot of methods. There are two ways you can make this easier:</p> <ul> <li>Use a framework such as <a href="https://github.com/jgauffin/griffin.mvccontrib" rel="nofollow">Griffin.MvcContrib</a>, which already do such a thing for you and do it even in a more pleasant way, since it separates the responsibilities that are inside these classes and expose a few new interfaces that you can implement for this functionality.</li> <li>Prevent any part of the system of depending on the static <code>Membership</code> class, but always inject a <code>MembershipProvider</code> into those classes or even better: don't use the <code>MembershipProvider</code> but simply inject the interfaces that are probably behind that <code>MembershipProvider</code> (such as <code>IUserRepository</code> and <code>IPasswordManager</code>).</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload