I just tried to learn about PostSharp and honestly I think it's amazing.
But one thing that it is difficult for me how a pure dependency injection (not service locator) cannot be done in PostSharp aspects, perhaps in my understanding as a result of compile time weaving.
Came from PHP background, Symfony has JMSAopBundle which still allows dependency to be injected to it's Interceptor.
Does .Net have some libraries with same capability?
Or am I missing something with PostSharp?
I don't think you're missing anything here and the limitation is indeed the result of using compile time weaving.
Although I think compile time weaving tools have its place in software development, I feel that they are often overused. Often I see them being used to patch flaws in the application design. In the applications I build I apply generic interfaces to certain architectural concepts. For instance, I define:
ICommandHandler<TCommand>
interface for services that implement a certain use case; IQueryHandler<TQuery, TResult>
interface for services that execute a query; IRepository<TEntity>
interface as abstraction over repositories;IValidator<TCommand>
interface for components that execute message validation;This allows me to create a single generic decorator for such group of artifacts (for instance an TransactionCommandHandlerDecorator<TCommand>
that allows running each use case in its own transaction). The use of decorators has many advantages, such as:
A lot has been written about this kind of application design; here are a few articles I wrote myself:
UPDATE
Decorators are great, but what I like about AOP is it's concept of advice and join points. Is there a way to simulate the same capability with decorator? I could only think of reflection right now.
A Join Point is a "well defined location within a class where a concern is going to be attached". When you apply AOP using decorators, you will be 'limited' to join points that are on the method boundaries. If however you adhere to the SRP, OCP and ISP, you will have very thin interfaces (usually with a single method). When doing that, you will notice that there is hardly ever a reason for having a join point at any other place in your classes.
An Advice is a "concern which will potentially change the input and/or output of the targeted method". When working with decorators and a message-based design (the thing I'm promoting here), your Advice needs to change the message (or replace the complete message with altered values) or change the output value. Things aren't that much different than with code weaving—if you apply an Advice, there must be something in common between all code the Advice is applied to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With