Unity DI自定义汇编的自动注册
我正在尝试使用Unity Registration by Conventionfunction。 我无法弄清楚如何从特定的程序集/项目文件注册文件结尾“存储库”。
container.RegisterTypes( AllClasses.FromAssembliesInBasePath(), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.ContainerControlled);
我在MSDN博客( https://blogs.msdn.microsoft.com/agile/2013/03/12/unity-configuration-registration-by-convention/ )post中找到了这个例子,据我了解,这将搜索所有项目/程序集,并将查找默认命名约定文件Class和IClass。
我的解决方案中有一个名为CManager.Repository的项目,其存储库文件以* Repository结尾。 它们是自动注册的。
任何暗示或帮助?
对于统一的自动注册 ,最好将Interfaces
和Repositories
作为存储库项目中的文件夹与适当的命名约定分开。
如果您在单个项目下拥有所有存储库 ,则不需要扫描所有程序集。 这应该在项目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=XXXX, Culture=neutral, PublicKeyToken=null"); container.RegisterTypes(repositoryAssembly.GetTypes(), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.ContainerControlled); container.RegisterType(new PerResolveLifetimeManager()); // ................ register other things is needed DependencyResolver.SetResolver(new UnityDependencyResolver(container)); } }
我们必须在应用程序启动时注册unity组件,传统上在Global.asax.cs
文件中。 因此,将UnityConfig.cs
文件放在UnityConfig.cs
文件夹下的启动项目中。
public class MvcApplication : HttpApplication { protected void Application_Start() { UnityConfig.RegisterComponents(); AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } }
并确保其他项目与启动项目相关联 。
这对我来说是一个企业web-api,我已经工作了好几个月了。 它有一个n层架构,其数据层包含.cs文件,以’Repository’结尾。 虽然我同意另一篇文章的界面应该分开,但在我的web-api中,它们并没有以这种方式实现。
using System; using System.Web.Http; using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration; using Unity.WebApi; namespace Blah.Endpoints.App_Start { /// /// Specifies the Unity configuration for the main container. /// public static class UnityConfig { public static void RegisterComponents() { var container = new UnityContainer(); // register all your components with the container here // it is NOT necessary to register your controllers RegisterTypes(container); // eg container.RegisterType(); GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container); } #region Unity Container private static Lazy container = new Lazy (() => { var container = new UnityContainer(); RegisterTypes(container); return container; }); /// /// Gets the configured Unity container. /// public static IUnityContainer GetConfiguredContainer() { return container.Value; } #endregion /// Registers the type mappings with the Unity container. /// The unity container to configure. /// There is no need to register concrete types such as controllers or API controllers (unless you want to /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered. public static void RegisterTypes(IUnityContainer container) { container.RegisterTypes(AllClasses.FromLoadedAssemblies(), WithMappings.FromMatchingInterface, WithName.Default, null, //WithLifetime IF we want to change the lifetime aspect aka Singletons null,//GetMembers, false ); } } }
此设置通常适用于我的团队。 有时候,我们仍然会在没有明显原因的情 对于那些我们做这样的事情;
public class WcfServiceFactory : UnityServiceHostFactory { protected override void ConfigureContainer(IUnityContainer container) { //Attach hook for AOP attributes container.AddNewExtension(); // register all your components with the container here // container // .RegisterType() // .RegisterType(new HierarchicalLifetimeManager()); container.RegisterTypes(AllClasses.FromLoadedAssemblies(), WithMappings.FromMatchingInterface, WithName.Default, null, //WithLifetime IF we want to change the lifetime aspect aka Singletons GetMembers, false ); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType, SharedLoanDataRepository>(); container.RegisterType, SharedLoanDataRepository>(); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); } private IEnumerable GetMembers(Type type) { var list = new List (); list.Add(new InterceptionBehavior(type.ToString())); list.Add(new Interceptor(type.ToString())); return list; } }
我希望这有帮助
问候。