Aspect Oriented Programming?

How can we apply attributes to a class function using AOP in C #?

UPDATE: I am a bit confused about what context should AOP be used in? Since we can use AOP for logging purposes, security (authentication), please suggest some other scenarios where we can take advantage of AOP

Can AOP be used to share data between different running threads in an application process?

+2


source to share


2 answers


C # doesn't have much support for AOP.

There is a PostSharp framework that you might like. You will probably get what you want, but it will never be as good as Java.



- Change

If you don't want you to use a Framework like PostSharp take a look at the ContextBoundObject class as well as (IIRC), DynamicProxy. But both of them require either a change in the inheritance chain or a change in the way objects are created.

+4


source


AOP is designed to cover what it calls end-to-end issues, that is, functionality that is required by many objects within the system, but which is not the root cause of these objects' problems. If such requirements are met by coding an end-to-end problem to objects of the entire system, we find ourselves in a messy, hard-to-find implementation, divided into many objects.

A classic example of an end-to-end problem is logging: logging is important to a real, living system and needs to be implemented throughout that system, but it's not really a problem of objects within that system: the system might need a sales tax calculation object to implement logging. but journaling is not an issue for the sales tax calculation object itself. AOP allows us to specify these system-wide (end-to-end) requirements separately for our core business logic and then weave them together at runtime or compile time.

AOP works by intercepting calls to methods on objects. The interception points are known as pointcuts, and the interception method is the recommended method, while the code that is recommended for the intercepted method is known as hint. I am only familiar with AOP through the Spring.Net AOP Framework, which allows you to specify and apply pointcuts and tips both through config files and programmatically. Spring.Net AOP has four types of recommendations: before, after, around, and throws, which are called when the recommended method is intercepted before the recommended method is called, after it is called, both before and after it is called, and when an exception is thrown accordingly. Whether applied by configuration or programmatically, the recommended method is unaware of Spring.Net AOP, or even what it was recommended.



Another use case for AOP is in transactions. If we try to implement this in code within objects, we end up with objects that need to know that they are acting within a transaction, which, in my opinion, is not a desirable design feature. AOP allows us to create and coordinate transactions externally with objects participating in a transaction, which can provide a much more stable, maintainable design.

The Spring.Net documentation is very good at explaining AOP in general and Spring.Net's implementation of AOP in particular, and has many examples. It's worth looking into even if you're not considering using the Spring.Net AOP framework.

+5


source







All Articles