0

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?

4

1 回答 1

0

我不确定您是否应该使用带有构造函数参数的 MembershipProvider 或 roleProvider,因为当您注册自定义提供程序时,web.config 将需要调用非参数构造函数,或者当您尝试调用例如:

     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;
    }

它将尝试使用空构造函数构造一个新的 MyMembershipProvider,而不是使用 [inject] 属性

于 2013-04-19T19:10:50.940 回答