autofac / Autofac.Extras.AggregateService

Dynamic aggregate service implementation generation for Autofac IoC
MIT License
4 stars 7 forks source link

Open Generic Support #5

Closed hrfarrokh closed 4 years ago

hrfarrokh commented 6 years ago

Does This Supported?

`

public class PermissionBusiness : BusinessBaseEx<Permission, IPermissionRepository>, IPermissionBusiness
{

    public PermissionBusiness(IBusinessService<IPermissionRepository> aggregateService) : base(aggregateService)
    {
    }

}

builder.RegisterAggregateService(typeof(IBusinessService<>));
builder.RegisterAggregateService<IInfrastructureService>();

public interface IInfrastructureService
{
    IMapper Mapper { get; set; }
    IEventBus EventBus { get; set; }
    ICacheService CacheService { get; set; }
}

public interface IBusinessService<TRepository> : IInfrastructureService
{
    TRepository Repository { get; set; }
}

`

tillig commented 6 years ago

What have you tried? Have you looked at the unit tests?

hrfarrokh commented 6 years ago

OpenGenericAggregateServiceTest.zip

Yes,I Do. But i want the aggregate service interface itself to be generic, I've attached a sample project to show what i want to do. Please download and just run it. Thank you so much. Sincerely.

tillig commented 6 years ago

Unfortunately the two project maintainers for Autofac have a minimum amount of time to spread across answering questions here, answering questions on StackOverflow, and coding on core Autofac and the 20+ extension libraries. It's best if we can help guide folks to answer their own questions where possible (which is why we try to get people to ask questions on StackOverflow, where there's a larger community, rather than in the issues list).

I don't see any unit tests showing aggregate services themselves being generic but I don't see anything off hand in the code that would stop it.

If you can put together a failing unit test and post it in here, awesome. If you're really going to need someone to download and look at a test project, it will be a while before that happens. Again, sorry.

hrfarrokh commented 6 years ago

Hi again I know you masters are busy so i apologize but this is the code

` class Program { static void Main(string[] args) { var serviceCollection = new ServiceCollection(); serviceCollection.AddLogging();

        var containerBuilder = new ContainerBuilder();

        containerBuilder.Populate(serviceCollection);

        containerBuilder.RegisterType<TestRepository>().As<IRepository>();
        containerBuilder.RegisterType<TestBusiness>().As<IBusiness>();
        containerBuilder.RegisterType<EventBus>().As<IEventBus>();
        containerBuilder.RegisterType<CacheService>().As<ICacheService>();

        containerBuilder.RegisterAggregateService(typeof(IBusinessService<>));
        containerBuilder.RegisterAggregateService<IInfrastructureService>();

        var container = containerBuilder.Build();
        var serviceProvider = new AutofacServiceProvider(container);

        // Works Fine
        Console.WriteLine(container.Resolve<IRepository>().ToString());
        // Console.WriteLine => OpenGenericAggregateServiceTest.TestRepository

        // Works Fine
        Console.WriteLine(container.Resolve<IInfrastructureService>().ToString());
        // Console.WriteLine => Castle.Proxies.IInfrastructureServiceProxy

        Console.WriteLine(container.Resolve<IInfrastructureService>().EventBus.Dummy.ToString());
        // Console.WriteLine => Hi I'm The EventBus

        // This is what i want,But it doesnt work
        // This is what i want,But it doesnt work
        // This is what i want,But it doesnt work
        // This is what i want,But it doesnt work
        // This is what i want,But it doesnt work
        Console.WriteLine(container.Resolve<IBusiness>().ToString());

        // THIS IS THE ERROR THAT GENERATE
        /*  
         Autofac.Core.DependencyResolutionException
        HResult=0x80131500
        Message=An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = TestBusiness (ReflectionActivator), 
        Services = [OpenGenericAggregateServiceTest.IBusiness], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---> None of the 
        constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'OpenGenericAggregateServiceTest.TestBusiness' can be invoked with the available 
        services and parameters:
        Cannot resolve parameter 'OpenGenericAggregateServiceTest.IBusinessService`1[OpenGenericAggregateServiceTest.IRepository] service' of constructor 
        'Void .ctor(OpenGenericAggregateServiceTest.IBusinessService`1[OpenGenericAggregateServiceTest.IRepository])'. (See inner exception for details.)
        Source=Autofac
        StackTrace:
        at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
        at Autofac.Core.Resolving.InstanceLookup.Execute()
        at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
        at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
        at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
        at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
        at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)
        at OpenGenericAggregateServiceTest.Program.Main(String[] args) in C:\Users\hrfar\source\repos\OpenGenericAggregateServiceTest\OpenGenericAggregateServiceTest\Program.cs:line 50

        Inner Exception 1:
        DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 
        'OpenGenericAggregateServiceTest.TestBusiness' can be invoked with the available services and parameters:
        Cannot resolve parameter 'OpenGenericAggregateServiceTest.IBusinessService`1[OpenGenericAggregateServiceTest.IRepository] service' of constructor 
        'Void .ctor(OpenGenericAggregateServiceTest.IBusinessService`1[OpenGenericAggregateServiceTest.IRepository])'.

         */

        Console.ReadLine();
    }
}

public interface IInfrastructureService
{
    ILogger<IInfrastructureService> Logger { get; set; }
    IEventBus EventBus { get; set; }
    ICacheService CacheService { get; set; }
}

public interface IBusinessService<TRepository> : IInfrastructureService
    where TRepository : class, IRepository
{
    TRepository Repository { get; set; }
}

public class TestBusiness : IBusiness
{
    public TestBusiness(IBusinessService<IRepository> service)
    {
        Service = service;
    }

    public IBusinessService<IRepository> Service { get; }
}

public interface IBusiness
{
}

public interface IRepository
{
}

public class TestRepository : IRepository
{
}

public interface ICacheService
{
    int MyProperty { get; set; }
}

public class CacheService : ICacheService
{
    public int MyProperty { get; set; } = 100;
}

public interface IEventBus
{
    string Dummy { get; set; }
}

public class EventBus : IEventBus
{
    public string Dummy { get; set; } = "Hi I'm The EventBus ";
}`

this is the CC as you are the owner as you said i will ask this in the Stack Sincerely

hrfarrokh commented 6 years ago

This is a question exactly what i want but unfortunately isn't answered

dewelloper commented 4 years ago

it needs new dependencies exactly what this did it! in Asp.net Core 3.1 i think it will be a demolition if they do nothing interesting

tillig commented 4 years ago

It doesn't look like there's been any real motion on this issue in two years. Given no action and no PR, I'm going to close the issue. If someone wants this feature, we'd be happy to accept a pull request.

tillig commented 3 years ago

This will be released shortly as v6.1.0.

AgentFire commented 2 years ago

Yeah, I'd like to have the feature, it seems really useful.

Edit: wow, you actually implemented it. Well done!