AOP for C # dotnet core 2.0, access method parameter value before running the method body

This is my method, I'm trying to check componentToSave(or the parameters of the access method parameter) and throw an exception, even if the body of the method even works.

public Component SaveComponent(Component componentToSave) {
    ...
}
  • I tried using PostSharp, but it is not free, and there were other libraries that rely on AutoFac as IoC, but in my current setup I use the built-in dependency injection of the built-in mesh network.

  • I tried NConcern, and it relies on CNeptuneand CNeptunerelies on the .exefile itself for linking after compilation, and I am currently using Linux for development and production, so I can’t use it, even I tried testing with it on windows, but I couldn’t get it work with dotnet core.

  • I tried this approach (i.e., ActionFilterand ServiceFilter), but I only got it if it [ServiceFilter(typeof(LoggingActionFilter))]doesn't control any other method (i.e. SaveComponent).

  • I tried to use RealProxy, but apparently it is not supported in the dotnet core.

I just got lost, maybe I'm already complicating the problem, but there must be a way. Any help would be greatly appreciated.

+2
source share
1

CQS, SimpleInjector. , , (SOLID). , , , . , . .

Edit

, !:)

. CQS ( ) ( ). , IQueryHandler ICommandHandler, . , .

public interface ISaveComponent
{
    Component SaveComponent(Component componentToSave);
}

public class SaveComponent : ISaveComponent
{
    public Component SaveComponent(Component componentToSave)
    {
        // Do your work here
    }
}

public class SaveComponentValidation : ISaveComponent
{
    private readonly ISaveComponent _saveComponent;

    public SaveComponentValidation(ISaveComponent saveCompnent)
    {
        _saveComponent = saveCompnent;
    }

    public Component SaveComponent(Component componentToSave)
    {
        // Do Validation here
        return _saveComponent.SaveComponent(componentToSave);
    }
}

SimpleInjector (IoC/DI) , :

 container.RegisterDecorator(typeof(ISaveComponent), typeof(SaveComponentValidation));

:

public class Program
{
    public static void Main()
    {
        ISaveComponent handler = new SaveComponentValidation(new SaveComponent());
        handler.SaveComponent(new Component());
    }
}
0

Source: https://habr.com/ru/post/1691794/


All Articles