0
votes

I'm working on a larger scale MVC App and have been trying to use DI with Ninject. I have the following projects:

Core - Class Library (contains a custom membership provider) Domain - Class Library (contains interfaces and EF to access data such as IUserRepository) WebUI - MVC 3 Project

I have bound IUserRepository to UserRepository in the Ninject startup within the App_Start container of my WebUI.

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);
        return kernel;
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IUserRepository>().To<UserRepository>();
    }        
}

However I want to use the UserRepository in my Customer Membership Provider with the Core Class Library...

public class MyMembershipProvider : MembershipProvider
{

    private IUserRepository userRepository;

    public MyMembershipProvider (IUserRepository repository)
    {
        this.userRepository = repository;
    }

    public override string ApplicationName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

// Rest of methods....

However this fails as my Ninject start is within the WebUI project and presumably knows nothing about the class library. Is there a way to get it to inject the dependencies for those class libraries also?

Or should I be looking at a different way to code those and their dependencies?

1
I believe this has already been answered: stackoverflow.com/a/7924087/335545Bern

1 Answers

0
votes

am not sure you should have MembershipProvider or roleProvider with a constructor argument since the web.config when u register your custom provider it will need to call a non argument constructor, or when you try to call for example :

     public MembershipCreateStatus CreateUser(string userName, string password, string email)
    {
        MembershipCreateStatus status;
        var memberProvider = (MyMembershipProvider)Membership.Provider;

        memberProvider.CreateUser(userName, password, email, null, null, true, null, out status);
        return status;
    }

it will try to construct a new MyMembershipProvider with empty constructor, instead use [inject] property