JasperFx / wolverine

Supercharged .NET server side development!
https://wolverinefx.net
MIT License
1.24k stars 135 forks source link

Wolverine codegen - variable name used twice in codegen #312

Closed afmuller closed 1 year ago

afmuller commented 1 year ago

In my project I have two handlers (in two different namespaces) for the same 'CompanyCreated' event.

However, the code generated by Wolverine declares/uses the 'companyCreatedHandler' variable name twice - and the code therefore does not compile.

WolverineFx version 0.9.16.

First handler:

using Ksb.CourierMgmtSystem.Application.Common.Interfaces;

namespace Ksb.CourierMgmtSystem.Application.Features.Companies.Commands;

public class CompanyCreatedHandler
{
    public Task Handle(CompanyCreated notification, CancellationToken cancellationToken, IIdentityService identityService)
    {
        //do some work
        return Task.CompletedTask;
    }

}

Second handler:

using Ksb.CourierMgmtSystem.Application.Common.Interfaces;

namespace Ksb.CourierMgmtSystem.BlazorServer.Handlers;

public class CompanyCreatedHandler
{
    public Task Handle(CompanyCreated notification, IIdentityService identityService)
    {
        //do some work
        return Task.CompletedTask;
    }
}

Generated code - 'companyCreatedHandler' variable name used twice:

// <auto-generated/>
#pragma warning disable
using Lamar;

namespace Internal.Generated.WolverineHandlers
{
    // START: CompanyCreatedHandler560193426
    public class CompanyCreatedHandler560193426 : Wolverine.Runtime.Handlers.MessageHandler
    {
        private readonly Lamar.IContainer _rootContainer;

        public CompanyCreatedHandler560193426(Lamar.IContainer rootContainer)
        {
            _rootContainer = rootContainer;
        }

        public override async System.Threading.Tasks.Task HandleAsync(Wolverine.Runtime.MessageContext context, System.Threading.CancellationToken cancellation)
        {
            await using var nestedContainer = (Lamar.IContainer)_rootContainer.GetNestedContainer();
            var companyCreatedHandler = nestedContainer.GetInstance<Ksb.CourierMgmtSystem.Application.Features.Companies.Commands.CompanyCreatedHandler>();
            var companyCreatedHandler = nestedContainer.GetInstance<Ksb.CourierMgmtSystem.BlazorServer.Handlers.CompanyCreatedHandler>();

            /*
            */
            var identityService = nestedContainer.GetInstance<Ksb.CourierMgmtSystem.Application.Common.Interfaces.IIdentityService>();
            var companyCreated = (Ksb.CourierMgmtSystem.Application.Features.Companies.Commands.CompanyCreated)context.Envelope.Message;
            await companyCreatedHandler.Handle(companyCreated, identityService).ConfigureAwait(false);
            await companyCreatedHandler.Handle(companyCreated, cancellation, identityService).ConfigureAwait(false);
        }

    }

    // END: CompanyCreatedHandler560193426

}
jeremydmiller commented 1 year ago

Got it. Quickie workaround is to make the handler methods static.

afmuller commented 1 year ago

Hi Jeremy Confirmed, it works perfectly with static methods, thanks.

jeremydmiller commented 1 year ago

So Wolverine happily disambiguates the handlers names already if it's building everything up itself, but that wasn't covering the case of needing to use a nested container to build things. I'm working on that use case too. Somewhere in the dependency tree you've got a service registration by lambda builder that's either Scoped or Transient? That's why Wolverine is having to use the nested container