Base solution for your next web application
Open Closed

Best Practice to register interface #6423


User avatar
0
andmattia created

I have a multiple instance of IReportData so I register it on PreInitialize of my module

IocManager.IocContainer.Register(
                Component.For<IReportData, Expense>().ImplementedBy<Expense>(),
                Component.For<IReportData, Invoice>().ImplementedBy<Invoice>()
                );

In this way I need to register every new IReportData, how can I do it automatically via IocManager.IocContainer?


9 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @andmattia

    You can do it similar to https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/Dependency/BasicConventionalRegistrar.cs#L15

    There are other samples in ABP's source code.

  • User Avatar
    0
    andmattia created

    hi @ismcagdas

    I try to flow your suggestion but my interface are not registered. Last week I update to lastest ABP 4.2 and this week I try it again.

  • User Avatar
    0
    aaron created
    Support Team

    Show code.

  • User Avatar
    0
    ismcagdas created
    Support Team

    @andmattia

    Any news ?

  • User Avatar
    0
    andmattia created

    Hi @ismcagdas

    I can't test this week beacuse I don't have time to do it. I plan to do next week and I give you an update ASAP.

  • User Avatar
    0
    ismcagdas created
    Support Team

    @andmattia

    Thank you for your feedback. We can continue according to your response.

  • User Avatar
    0
    andmattia created

    @ismcagdas

    I try to implement the example that has suggested but it doesn't work, but I check on Windsor docs at Select() and I see the correct implementation.

    So the problem is related to the name because to registrer class with different name than the interface I use this code

    public class ReportConventionalRegistrar : IConventionalDependencyRegistrar
        {
            public void RegisterAssembly(IConventionalRegistrationContext context)
            {
    
                context.IocManager.IocContainer.Register(
                    Classes.FromAssembly(context.Assembly)
                        .BasedOn<IReportData>()
                        .If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
                        .WithService.Self()
                        .WithService.Base()
                );
            }
        }
    
  • User Avatar
    0
    musa.demir created

    Did you add your registerer to IocManager? see : https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f4b34279b743319d922a02a2246a1af9b7255886/src/Abp/AbpKernelModule.cs#L45

  • User Avatar
    0
    andmattia created

    @demirmusa

    Yes I do it on my base module and works fine.