ArxOne / MrAdvice

.NET aspect weaver (build task under NuGet package)
MIT License
311 stars 45 forks source link

Dotnet core DI and logging #175

Closed heralight closed 2 years ago

heralight commented 3 years ago

Hi!

Great work with MrAdvice!

I would like to use it in dotnet core 5 aspnet project to simplify serilog (logger) code, simplify some EF query through attributes, ...etc. The problem that I can't actually solve is how to access to dotnet core logger, ef context, ...etc. Do I need to do it through some reflection at startup and get the instances from some properties, what is your advice or perhaps have already some code to do that please?

Thank you !

Alexandre

picrap commented 3 years ago

What is my advice using MrAdvice, mmmh? šŸ˜ Iā€™m not sure I understand your question: do you want to inject aspect to dotnet code or to yours? The first is not possible (MrAdvice works at post-build step, so it needs you to do the build), the second is simply achieved by applying the advice (itā€™s an attribute youā€™ll write) at the requested point (method, class, even whole assembly). Does this help?

dlalex83 commented 2 years ago

Hi there,

Great work indeed!

I think he's trying to understand how to inject dependencies (e.g. a logger class) into an aspect.
Considering the aspect works at compile time, that should not be possible, I guess. I noticed that on PostSharp's aspects is possible to add a runtime initialization which can be used to then obtain and use dependencies.

Is there something similar available in MrAdvice?

Thanks

picrap commented 2 years ago

What do you mean by ā€œruntime initializationā€? There are aspects in MrAdvice that are related to reflection, such as IMethodInfoAdvice and IPropertyInfoAdvice, they are invoked once per reflection element. How would you imagine the feature you want?

dlalex83 commented 2 years ago

I've bumped into this post and noticed the RuntimeInitialize method exposed by PostSharp. In the above question the guy that posted is manually creating instances of the required dependency at runtime initalization and the 1st answer shows a better pattern to achieve the requirement. Either way, it's all possible because of the RuntimeInitialization method in the aspect.

picrap commented 2 years ago

Hi everyone. I like this discussion, so feel free to join discussion #179 šŸ˜‰

picrap commented 2 years ago

Join #179!