来自特定程序集的Unity DI自动注册 [英] Unity DI auto-registration from specific Assembly

查看:68
本文介绍了来自特定程序集的Unity DI自动注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用按惯例注册Unity"功能.我不知道如何从特定的程序集/项目文件中以存储库"结尾的文件进行注册.

  container.RegisterTypes(AllClasses.FromAssembliesInBasePath(),WithMappings.FromMatchingInterface,WithName.Default,WithLifetime.ContainerControlled); 

我从MSDN博客(

如果您在单个项目下拥有所有存储库,则无需扫描所有程序集.这应该在项目 CManager.Repository

下注册所有存储库.

 公共静态类UnityConfig{公共静态无效RegisterComponents(){var container = new UnityContainer();var repositoryAssembly = AppDomain.CurrentDomain.GetAssemblies().First(a => a.FullName =="CManager.Repository,Version = X.X.X.X,Culture = neutral,PublicKeyToken = null");container.RegisterTypes(repositoryAssembly.GetTypes(),WithMappings.FromMatchingInterface,WithName.Default,WithLifetime.ContainerControlled);container.RegisterType< ApplicationDbContext>(新的PerResolveLifetimeManager());//.......注册其他需要的东西DependencyResolver.SetResolver(new UnityDependencyResolver(container));}} 

我们必须在应用程序启动上注册统一组件,该组件通常位于 Global.asax.cs 文件中.因此,将 UnityConfig.cs 文件放在启动项目的 App_Start 文件夹下.

 公共类MvcApplication:HttpApplication{受保护的void Application_Start(){UnityConfig.RegisterComponents();AreaRegistration.RegisterAllAreas();FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);RouteConfig.RegisterRoutes(RouteTable.Routes);BundleConfig.RegisterBundles(BundleTable.Bundles);}} 

并确保将其他项目与启动项目链接.

I'm trying to use the Unity Registration by Convention feature. I can't figure out how to register from specific assembly/project files with the file ending "Repository".

container.RegisterTypes(
       AllClasses.FromAssembliesInBasePath(),
       WithMappings.FromMatchingInterface,
       WithName.Default,
       WithLifetime.ContainerControlled);

I've found only this example from a MSDN blog ( https://blogs.msdn.microsoft.com/agile/2013/03/12/unity-configuration-registration-by-convention/ ) post and as I understand it, this will search through all projects/assemblies and will look for default naming convention files Class and IClass.

I have a project in my solution named CManager.Repository with repository files ending with *Repository. They are auto-registered.

Any hint or help?

解决方案

For auto-registration of unity it's better to separate the Interfacesand Repositories as folder in the repository project with proper naming convention.

If you have all repositories under single project then it's not needed to scan for all assemblies. This should register all repositories under project CManager.Repository

  public static class UnityConfig
   {
     public static void RegisterComponents()
      {
        var container = new UnityContainer();
        var repositoryAssembly = AppDomain.CurrentDomain.GetAssemblies()
            .First(a => a.FullName == "CManager.Repository, Version=X.X.X.X, Culture=neutral, PublicKeyToken=null"); 

        container.RegisterTypes(repositoryAssembly.GetTypes(),
            WithMappings.FromMatchingInterface,
            WithName.Default,
            WithLifetime.ContainerControlled);      

        container.RegisterType<ApplicationDbContext>(new PerResolveLifetimeManager());
       // ................ register other things is needed
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
     }
  } 

we have to register unity components on application startup, traditionally inside Global.asax.cs file. So put UnityConfig.cs file on startup project under App_Start folder.

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        UnityConfig.RegisterComponents();
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);            
    }      
}

and make sure other projects are linked up with startup project.

这篇关于来自特定程序集的Unity DI自动注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆