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