postsharp / Metalama

Metalama is a Roslyn-based meta-programming framework. Use this repo to report bugs or ask questions.
164 stars 4 forks source link

IntroduceDependency causes non-null constructor warning #306

Closed hazzinator1 closed 3 weeks ago

hazzinator1 commented 1 month ago

I am using aspects to automatically do a UnitOfWork call at the end of some of my services

public class SaveChangesAttribute : OverrideMethodAspect
{
    [IntroduceDependency(IsRequired = true)]
    private readonly IUnitOfWork UnitOfWork;

    public override dynamic? OverrideMethod()
    {
        throw new Exception("SaveChanges cannot be applied to non-async methods");
    }

    public override async Task<dynamic?> OverrideAsyncMethod()
    {
        var result = await meta.Proceed();
        await this.UnitOfWork.SaveChanges();
        return result;
    }
}

However, after being applied to one of the methods like below:

public partial class DocumentActionsService
{
    ...

    public DocumentActionsService(...)
    {
        ...
    }

    [SaveChanges]
    public async Task CheckOutDocument(string documentUrl)
    {
        ...
    }
}

The constructor comes up with this warning: image

I can't find anyway of removing this warning, aside from disabling the nullable warning on this line, which doesn't feel like a good solution. I have tried IsRequired = true on the aspect, but that doesn't seem to affect anything. Any advice?

svick commented 1 month ago

Can you try this with Metalama 2024.2 preview? We made changes there exactly for this situation.

svick commented 3 weeks ago

I'm closing this issue, since it should be fixed in Metalama 2024.2 preview. If the problem remains, please comment here and we will reopen the issue.