0

I found http://www.devproconnections.com/content1/catpath/database-development/topic/a-perfect-storm-linq-to-sql-dependency-injection-and-asp-net-providers/page/2 and had similar code as from the webpage:

public class CustomProvider : MembershipProvider, IMembershipProvider
{
    private IUserRepository _userRepo;

    // this .ctor is used via unit tests (as a seam)

    public CustomProvider(IUserRepository repo)
    {
        this._userRepo = repo;
    }

    // requisite parameter-less constructor:

    public CustomProvider()
    {
        // do NOTHING here
    }

    public override bool ValidateUser(string username, string password)
    {
        // HACK:
        IUserRepository repo = this._userRepo ??    ObjectFactory.GetInstance<IUserRepository>();
        SiteUser user = repo.GetUserByEmailAddress(username.ToLower());
        if (user == null)
            return false;

        if (!user.Active || !user.Verified)
            return false;

        if (user.PassPhrase.IsNullOrEmpty())
            return false;

        // do other verification... etc
    }
}

Except mine is a custom RoleProvider. Is calling the ObjectFactory.GetInstance an accepted way to inject dependencies into a RoleProvider? I tried to setup a property to inject the dependency, but I could not get that to work. I'm sure my StructureMap registry is wrong. But hard to find out the right way when the documentation is out of date.

So for an ASP.NET MVC3 app, is calling the ObjectFactory ok in a custom RoleProvider? Or should I attempt to inject to a property?

If a property, how? I have For<RoleProvider>().Use(ctx => Roles.Provider); currently. But I'm not sure id the Use should be an Add, nor am I sure on the syntax to inject a dependency into a property.

Still Need help I'm having an awful time trying to make miniprofiler not throw Null ref exceptions when I merely move the StructureMap ObjectFactory to a property for init. The goal is to allow roles to be cached. I get the same error as these questions mini-profiler nullreferenceexception Help Configure mvc mini profiler with Linq to Sql

I've updated to the latest MVCMiniProfiler and tried it's MVC package. Seems that profiling isn't enabled before the custom RoleProvider is init or the properties are initialized. If I set the field straight from the overridden GetRolesForUser method, everything is fine. If I make that field a backer to a public property, I get NULL exceptions in ProfiledDbCommand. Why?

1 Answer 1

2

The Microsoft "provider" pattern does not work well with dependency injection, because of its reliance on statics and singletons. If you have to use a Provider, just do the service location via ObjectFactory.GetInstance and move on.

1
  • Ya I have. Oddly moving the ObjectFactory.GetInstance to a property and checking for null, freaks out MvcMiniProfiler. Not sure why. But seems like it might have something to do with the order things are initialized, because it is a null exception. Nothing to do with StructureMap.
    – nportelli
    Mar 18, 2012 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.