Skip to content

Decorator for classes , permit to know when method start and ends

License

Notifications You must be signed in to change notification settings

ignatandrei/RSCG_Decorator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RSCG_Decorator

Decorator for classes , permit to know when method start and ends

Usage

Add reference to RSCG_Decorator and RSCG_DecoratorCommon

<ItemGroup>
	
	  <PackageReference Include="RSCG_Decorator" Version="7.2023.930.2116" OutputItemType="Analyzer" ReferenceOutputAssembly="false"  />
	  <PackageReference Include="RSCG_DecoratorCommon" Version="7.2023.930.2116" />
</ItemGroup>
<PropertyGroup>
	<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
	<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
	

For any class that you want to intercept methods, implement interface IDecoratorMethodV1

See example at the RSCG_DecoratorTestConsole

public partial class Person : IDecoratorMethodV1
{
    public void EndMethod(MethodRecognizer recognizer)
    {
        logger.LogInformation("end "+recognizer.UniqueId);
    }

   
    public void ExceptionMethod(Exception ex, MethodRecognizer recognizer)
    {
        logger.LogError(ex, "exception on " + recognizer.UniqueId+ " Value Parameters:" + recognizer.ValueTypeParametersString); 
    }


    public void StartMethod(MethodRecognizer recognizer)
    {
        logger.LogInformation("start " + recognizer.UniqueId + " Value Parameters:"+recognizer.ValueTypeParametersString);
   }
}

Usage with ASP.NET Core - or any DI framework

Let's take Person with interface IPerson. You have the following:

serviceCollection 
          .AddTransient<IPerson, Person>();

You can add this as the latest

serviceCollection = serviceCollection
     .AddTransient<Person, Person>()
     .AddTransient<IPerson, Person_Decorator>();
     

And when asking for IPerson , the last wins:

var data = serviceProvider.GetRequiredService<IPerson>();
//obtaining Person_Decorator because is the last one

More Roslyn Source Code Generators

You can find more RSCG with examples at Roslyn Source Code Generators

About

Decorator for classes , permit to know when method start and ends

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published