【问题标题】:Change autofac code on ninject在ninject上更改autofac代码
【发布时间】:2015-04-24 13:05:02
【问题描述】:

这是项目中的 Autofac 代码

https://github.com/MarlabsInc/SocialGoal

如何在 Ninject 上更改此代码?

我想使用来自社会目标项目的存储库,但我更喜欢使用 ninject 而不是 autofac。

public static class Bootstrapper
{
    public static void Run()
    {
        SetAutofacContainer();
        //Configure AutoMapper
        AutoMapperConfiguration.Configure();
    }
    private static void SetAutofacContainer()
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
        builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();
        builder.RegisterAssemblyTypes(typeof(FocusRepository).Assembly)
        .Where(t => t.Name.EndsWith("Repository"))
        .AsImplementedInterfaces().InstancePerHttpRequest();
        builder.RegisterAssemblyTypes(typeof(GoalService).Assembly)
       .Where(t => t.Name.EndsWith("Service"))
       .AsImplementedInterfaces().InstancePerHttpRequest();

        builder.RegisterAssemblyTypes(typeof(DefaultFormsAuthentication).Assembly)
     .Where(t => t.Name.EndsWith("Authentication"))
     .AsImplementedInterfaces().InstancePerHttpRequest();

        builder.Register(c => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>( new SocialGoalEntities())))
            .As<UserManager<ApplicationUser>>().InstancePerHttpRequest();

        builder.RegisterFilterProvider();
        IContainer container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));            
    }
}

【问题讨论】:

    标签: c# model-view-controller ninject repository-pattern autofac


    【解决方案1】:

    InstancePerHttpRequest 在ninject 中通过将InRequestScope() 附加到绑定来完成,如下所示:

    kernel.Bind<IFoo>().To<UnitOfWork>().InRequestScope();
    

    要使InRequestScope 可用,您需要适当的Ninject.Web.Mvc* nuget 包,请参阅here。 stackoverflow 上已经有几篇文章涵盖了这一点(如何为 asp.net / MVC 设置 ninject)。


    RegisterAssemblyTypes 之类的操作是使用 Ninject.Extensions.Conventions 完成的。示例:

    builder.RegisterAssemblyTypes(typeof(FocusRepository).Assembly)
           .Where(t => t.Name.EndsWith("Repository"))
           .AsImplementedInterfaces().InstancePerHttpRequest();
    

    完成如下:

    kernel.Bind(x => x.From(typeof(FocusRepository).Assembly)
          .IncludingNonePublicTypes() // only required if the classes aren't `public`
          .SelectAllClasses()
          .EndingWith("Repository")
          .BindAllInterfaces()
          .Configure(b => b.InRequestScope()));
    

    【讨论】:

    • 它有效,但仅当我使用服务中的 GetAll 方法时。当我使用删除或创建时它不起作用。这个存储库是 autofac 存储库,也许这是个问题。这个存储库看起来像下面的链接stackoverflow.com/questions/12390665/…
    • 在使用kernel.GetAll&lt;&gt; 时究竟是什么不工作或只是工作?存储库类的设计不是问题,它们不是特定于 autofac 的,您可以将它们与 Ninject、Autofac、simpleinjector... 一起使用。但是绑定(尤其是泛型)的工作方式有点不同。
    • 我认为在show what you've implemented and what exactly isn't working 处发布一个新问题是个好主意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 2012-12-11
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多