मैं स्वचालित रूप से सभी प्रकार कि इस बयानहल निर्भरता
IUnityContainer container = new UnityContainer();
container.RegisterTypes(
AllClasses.FromAssembliesInBasePath(),
WithMappings.FromMatchingInterface,
WithName.Default,
WithLifetime.Transient);
ICustomer result = container.Resolve<ICustomer>();
साथ इंटरफेस को लागू मैं इंटरफेस और कार्यान्वयन के लिए एक नाम स्थान कैसे निर्दिष्ट कर सकते हैं रजिस्टर कर सकते हैं?
i.e: Framework.RepositoryInterfaces
में केवल इंटरफ़ेस Framework.RepositoryImplementations
में प्रकारों द्वारा हल किए जाने चाहिए।
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;
}
}
और के माध्यम से इसका इस्तेमाल: नाम स्थान से