0
我正在開發一個更大規模的MVC應用程序,並一直試圖使用DI與Ninject。我有以下項目:Ninject DI帶類庫項目
核心 - 類庫(包含自定義成員資格提供) 域 - 類庫(包含接口和EF來訪問數據,如IUserRepository) Web用戶界面 - MVC 3項目
我有在我的WebUI的App_Start容器內的Ninject啓動中將IUserRepository綁定到UserRepository。
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>();
}
}
不過,我想用UserRepository與核心類庫我的客戶成員資格提供...
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....
然而,這未能按我的Ninject開始是在WebUI項目中大概什麼都不知道關於類庫。有沒有辦法讓它爲這些類庫注入依賴關係?
或者我應該看看不同的方式來編碼這些和他們的依賴?
我相信這已經回答了:http://stackoverflow.com/a/7924087/335545 – Bern