Unity Container RegisterType <TFrom, TTo> Problems

I am trying to do automatic registration of repositories:

This works, but I don't like it, because in service level classes I will have to provide a specific one EntityRepository<T>for the designers instead of supplying an interfaceIRepository<T>

public static IContainer RegisterRepositories(
    this IContainer container, params Assembly[] assemblies)
{
    var repositories = (
        from assembly in assemblies
        from type in assembly.GetTypes()
        where typeof(ObjectContext).IsAssignableFrom(type)
        from property in type.GetProperties()
        let propertyType = property.PropertyType
        where propertyType.IsGenericType 
        where propertyType
            .GetGenericTypeDefinition() == typeof(ObjectSet<>)
        select new 
        {
            ObjectContextType = type, 
            DomainType = propertyType.GetGenericArguments()[0] 
        })
        .ToList();

    foreach (var repository in repositories)    
    {
        container.RegisterType(typeof(BaseObjectContext), 
            new PerExecutionContextLifetimeManager());

        var serviceType = typeof(IRepository<>)
            .MakeGenericType(repository.DomainType);

        var implementationType = typeof(EntityRepository<>)
            .MakeGenericType(repository.ObjectContextType);

        container.RegisterType(serviceType, implementationType, 
            new TransientLifetimeManager());
    };

    return container;
}

I want to automatically register my repositories with container.RegisterType<TFrom, TTo>(new TransientLifetimeManager()); I know where the problem is, because this registration method requires that you provide the real name of the interface and class, for example IRepository<Customer>and EntityRepository<Customer>. I can do this, but I will need to list the repositories one at a time, and I do not think that this is possible if you are dealing with a project of 100 domain objects. I want to do this automatically so that I can enter IRepository<T>into service level classes.

// Configure root container.Register types and life time 
// managers for unity builder process
public static IContainer RegisterRepositories(
    this IContainer container, params Assembly[] assemblies)
{
    var repositories = (
        from assembly in assemblies
        from type in assembly.GetTypes() 
        where typeof(ObjectContext).IsAssignableFrom(type)
        from property in type.GetProperties()
        let propertyType = property.PropertyType
        where propertyType.IsGenericType 
        where propertyType
            .GetGenericTypeDefinition() == typeof(ObjectSet<>)
        select new 
        {
            ObjectContextType = type, 
            DomainType = propertyType.GetGenericArguments()[0] 
        })
        .ToList()

    foreach (var repository in repositories)
    {
        container.RegisterType<BaseObjectContext>(
            new PerExecutionContextLifetimeManager(), 
            new InjectionConstructor());

        container.RegisterType<IRepository<repository.DomainType>,
             EntityRepository<repository.ObjectContextType>>(
            new TransientLifetimeManager());
    };

    return container;
}
+3
1

, , , , . , , IRepository<T> , Entity Framework, , .

IRepository<T> . , Entity Framework, (ObjectContext) . , ObjectContext, , , ObjectContext , , .

IRepository<T> , UnitOfWork : a IUnitOfWorkFactory. IUnitOfWorkFactory UnitOfWork ( ), UnitOfWork IRepository<T> SubmitChanges.

IRepository<T> GetRepository<T>() UnitOfWork, - , 100 . IUnitOfWork .

, UnitOfWork IRepository<T>, public IRepository<Customer> Customers { get; set; }, , , , , , . , , , .

, , , . (. LINQ), . , . , , , . , , .

, .

+1

All Articles