2

我只是想开始使用 Ninject 2 和 ASP.NET MVC 2。我已按照本教程http://www.craftyfella.com/2010/02/creating-aspnet-mvc-2-controller.html创建一个带有 Ninject 的控制器工厂,并将第一个抽象绑定到一个具体的实现。现在我想从另一个程序集(我的具体 SQL 存储库所在的位置)加载存储库类型,但我无法让它工作。这是我的代码:

全球.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:

没有匹配的绑定可用,并且该类型不可自绑定。激活路径:2)将依赖CommunityUserRepository注入到AccountController类型构造函数的参数_rep中1)对AccountController的请求

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

4

1 回答 1

0

好的,我做了一些重构,现在我让它运行起来了。我将发布我的 Global.asax 的代码,因为这就是一切发生的地方。我正在使用最新的 Ninject 2 Build 和最新的 Ninject.Web.Mvc Build for 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);
    }

}

如您所见,我摆脱了从 NinjectHttpApplication 继承的 ControllerFactory,并在模块中加载和绑定外部程序集的类型。现在可能有一种更好的方法,无需在配置文件中将类型指定为字符串,也许您可​​以在外部程序集中声明模块并让 Ninject 从那里自动加载它,但我仍然需要将连接字符串传递给具体实现的构造函数。也许有人对此有想法,但现在这很好用。

于 2010-04-10T09:17:13.630 回答