2010-04-09 31 views
2

我刚刚尝试开始使用Ninject 2和ASP.NET MVC 2.我按照本教程http://www.craftyfella.com/2010/02/creating-aspnet-mvc-2-controller.html创建了一个包含Ninject的Controller Factory并绑定第一个抽象的具体实现。现在我想从另一个程序集(我的具体SQL存储库所在的位置)加载一个存储库类型,我不能让它工作。这里是我的代码:Ninject 2 + ASP.NET MVC 2来自外部程序集的绑定类型

的Global.asax.cs

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterRoutes(RouteTable.Routes); 


     ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory()); 
    } 

器厂:

public class Kernelhelper 
{ 
    public static IKernel GetTheKernel() 
    { 
     IKernel kernel = new StandardKernel(); 
     kernel.Load(System.Reflection.Assembly.Load("MyAssembly")); 
     return kernel; 
    } 
} 

public class MyControllerFactory : DefaultControllerFactory 
{ 
    private IKernel kernel = Kernelhelper.GetTheKernel(); 


    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) 
    { 

     return controllerType == null ? null : (IController)kernel.Get(controllerType); 
    } 
} 

在 “MyAssembly程序” 有一个模块:

public class ExampleConfigModule : NinjectModule 
{ 
    public override void Load() 
    { 
     Bind<Domain.CommunityUserRepository>().To<SQLCommunityUserRepository>(); 
    } 

} 

现在,当我刚在我的入口点上使用MockRepository对象时,它工作得很好,需要存储库的控制器工作正常。 kernel.Load(System.Reflection.Assembly.Load(“MyAssembly”));也做它的工作和注册模块,但只要我呼吁需要存储库的控制器我从Ninject得到一个ActivationException:

没有匹配的绑定可用,并且类型不可自行绑定。 激活路径:依赖CommunityUserRepository的 2)注入型的AccountController 1)要求的AccountController

的构造函数的参数_rep

谁能给我从外部组件绑定类型最佳实践范例(这确实是一个重要的方面依赖注入)?谢谢!

+0

愚蠢的我,具体实现的构造函数需要一个连接字符串。现在我必须找出如何告诉Ninject ... – hoetz 2010-04-10 08:20:14

回答

0

好吧,我做了一些重构,现在我运行了。我将发布我的Global.asax的代码,因为这就是发生的一切。我使用的是最新的Ninject 2建立与最新Ninject.Web.Mvc建立MVC 2

public class MvcApplication : NinjectHttpApplication 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      "Default",            // Route name 
      "{controller}/{action}/{id}",       // URL with parameters 
      new { controller = "Home", action = "Index", id = "" } // Parameter defaults 
     ); 

    } 

    protected override void OnApplicationStarted() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterRoutes(RouteTable.Routes); 

     RegisterAllControllersIn(System.Reflection.Assembly.GetExecutingAssembly()); 
    } 


    protected override Ninject.IKernel CreateKernel() 
    { 

     var kernel = new StandardKernel(); 
     kernel.Load(new ExampleConfigModule()); 
     return kernel; 
    } 
} 

public class ExampleConfigModule : Ninject.Modules.NinjectModule 
{ 
    public override void Load() 
    { 
     string connectionString = 
      ConfigurationManager.ConnectionStrings 
      ["ConnectionString1"].ConnectionString; 
     string communityUserRepTypeName = 
      ConfigurationManager.AppSettings 
      ["CommunityUserRepositoryType"]; 
     var communityUserRepositoryType = 
      Type.GetType(communityUserRepTypeName, true); 
     Bind<Domain.CommunityUserRepository>().To(communityUserRepositoryType).WithConstructorArgument("conString",connectionString); 
    } 

} 

正如你可以看到我摆脱了我的ControllerFactory的,从NinjectHttpApplication继承和负载&结合外部组件'输入模块。现在可能有更好的方法,而不需要在配置文件中将类型指定为字符串,也许你可以在外部程序集中声明模块,并让Ninject从那里自动加载它,但我仍然需要将连接字符串传递给具体实现的构造函数。也许有人有这个想法,但现在这工作正常。