Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open Generic Support #5

Closed
hrfarrokh opened this issue May 15, 2018 · 9 comments
Closed

Open Generic Support #5

hrfarrokh opened this issue May 15, 2018 · 9 comments
Labels

Comments

@hrfarrokh
Copy link

hrfarrokh commented May 15, 2018

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; }
}

`

@hrfarrokh hrfarrokh changed the title Hi Open Generic Support May 15, 2018
@tillig
Copy link
Member

tillig commented May 15, 2018

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

@hrfarrokh
Copy link
Author

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
Copy link
Member

tillig commented May 15, 2018

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
Copy link
Author

hrfarrokh commented May 15, 2018

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 hrfarrokh reopened this May 15, 2018
@hrfarrokh
Copy link
Author

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

@dewelloper
Copy link

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
Copy link
Member

tillig commented Oct 5, 2020

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 tillig closed this as completed Oct 5, 2020
StijnVA pushed a commit to StijnVA/Autofac.Extras.AggregateService that referenced this issue Aug 24, 2021
@tillig
Copy link
Member

tillig commented Aug 24, 2021

This will be released shortly as v6.1.0.

@AgentFire
Copy link

AgentFire commented Apr 4, 2022

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

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants