C# 仅从指定的命名空间解析依赖项

C# 仅从指定的命名空间解析依赖项,c#,unity-container,conventions,C#,Unity Container,Conventions,我可以用这个语句自动注册实现接口的所有类型 IUnityContainer container = new UnityContainer(); container.RegisterTypes( AllClasses.FromAssembliesInBasePath(), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.Transient); ICustomer result =

我可以用这个语句自动注册实现接口的所有类型

IUnityContainer container = new UnityContainer();

container.RegisterTypes(
    AllClasses.FromAssembliesInBasePath(),
    WithMappings.FromMatchingInterface,
    WithName.Default,
    WithLifetime.Transient);
ICustomer result = container.Resolve<ICustomer>();
IUnityContainer container=newunitycontainer();
container.RegisterTypes(
AllClasses.FromAssembliesInBasePath(),
使用Mappings.FromMatchingInterface,
WithName.Default,
使用寿命(瞬时);
icCustomer结果=container.Resolve();
如何为接口和实现指定名称空间


i、 e:只有
框架中的接口。RepositoryInterfaces
应按
框架中的类型进行解析。RepositoryImplementations

尝试按命名空间过滤类型

IUnityContainer container=newunitycontainer();
container.RegisterTypes(
AllClass.FromAssembliesInBasePath()。其中(
t=>t.Namespace.StartsWith(“Framework.RepositoryImplementations”)||
t、 Namespace.StartsWith(“Framework.RepositoryInterfaces”),
使用Mappings.FromMatchingInterface,
WithName.Default,
使用寿命(瞬时);
icCustomer结果=container.Resolve();
您可以使用:

公共类名称空间注册约定:注册约定
{
私有只读IEnumerable\u typesToResolve;
私有只读字符串_namespaceprofixforinterfaces;
私有只读字符串_namespaceprofixforimplements;
公共命名空间注册约定(IEnumerable typesToResolve、字符串NamespacePrefixForInterface、字符串NamespacePrefixForImplements)
{
_typesToResolve=typesToResolve;
_namespacePrefixForInterfaces=namespacePrefixForInterfaces;
_NamespacePrefixForImplements=NamespacePrefixForImplements;
}
公共重写IEnumerable GetTypes()
{
//还添加了摘要。如果愿意,您只能筛选接口。
return\u typesToResolve.Where(t=>
((t.IsInterface | | t.isastract)和&t.Namespace.StartsWith(_namespacepprefixforinterfaces))||
(!t.IsInterface&&!t.isastract&&t.Namespace.StartsWith(_namespacepprefixforimplements));
}
公共重写函数GetFromTypes()
{
返回mappings.FromMatchingInterface;
}
公共重写Func GetName()
{
返回WithName.Default;
}
公共覆盖函数GetLifetimeManager()
{
使用寿命返回。短暂;
}
公共重写函数GetInjectionMembers()
{
返回null;
}
}
并通过以下方式使用:

container.RegisterTypes(new NamespaceRegistrationConvention(AllClasses.FromAssembliesInBasePath(), "Framework.RepositoryInterfaces", "Framework.RepositoryImplementations");
ICustomer result = container.Resolve<ICustomer>();
container.RegisterTypes(新的NamespaceRegistrationConvention(AllClass.FromAssembliesInBasePath(),“Framework.RepositoryInterfaces”,“Framework.repositoryImplements”);
icCustomer结果=container.Resolve();
public class NamespaceRegistrationConvention : RegistrationConvention
{
    private readonly IEnumerable<Type> _typesToResolve;
    private readonly string _namespacePrefixForInterfaces;
    private readonly string _namespacePrefixForImplementations;

    public NamespaceRegistrationConvention(IEnumerable<Type> typesToResolve, string namespacePrefixForInterfaces, string namespacePrefixForImplementations)
    {
        _typesToResolve = typesToResolve;
        _namespacePrefixForInterfaces = namespacePrefixForInterfaces;
        _namespacePrefixForImplementations = namespacePrefixForImplementations;
    }

    public override IEnumerable<Type> GetTypes()
    {
        // Added the abstract as well. You can filter only interfaces if you wish.
        return _typesToResolve.Where(t =>
            ((t.IsInterface || t.IsAbstract) && t.Namespace.StartsWith(_namespacePrefixForInterfaces)) ||
            (!t.IsInterface && !t.IsAbstract && t.Namespace.StartsWith(_namespacePrefixForImplementations)));
    }

    public override Func<Type, IEnumerable<Type>> GetFromTypes()
    {
        return WithMappings.FromMatchingInterface;
    }

    public override Func<Type, string> GetName()
    {
        return WithName.Default;
    }

    public override Func<Type, LifetimeManager> GetLifetimeManager()
    {
        return WithLifetime.Transient;
    }

    public override Func<Type, IEnumerable<InjectionMember>> GetInjectionMembers()
    {
        return null;
    }
}
container.RegisterTypes(new NamespaceRegistrationConvention(AllClasses.FromAssembliesInBasePath(), "Framework.RepositoryInterfaces", "Framework.RepositoryImplementations");
ICustomer result = container.Resolve<ICustomer>();