Can Spring.Net work like PostSharp?

A few months ago I discovered PostSharp, and for a while it was good.

But then the legal came back with a response saying that they did not like the license for the old versions. Then the department told me that the price of 2.0 was unacceptably high (for the number of places we need) ... I was extremely hidden, but not discouraged. I could not be the only such card.

I continued to search for a replacement, but most of them were either dead or poorly maintained (especially in the documentation department), for academic use, or all of the above (I look at you as Aspect.Net)

Then I discovered Spring.Net, and for a while it was good.

I read the documentation, and she continued to paint what seemed like a supernatural picture of AOP nirvana. I was no longer tied to attributes to mark where I needed code interception, but it can be configured in XML and changes to it do not require recompilation. Excellent.

Then I looked at the samples and saw the following in each use case:

// Create AOP proxy using Spring.NET IoC container. IApplicationContext ctx = ContextRegistry.GetContext(); ICommand command = (ICommand)ctx["myServiceCommand"]; command.Execute(); if (command.IsUndoCapable) { command.UnExecute(); } 

Why should the first two lines of code exist? It destroys everything. This means that I can’t just provide the user with a set of XML aspects and attributes or configurations that they can use by attaching the appropriate attributes to the corresponding methods / classes / etc or editing the mapping template in XML. They must change their programming logic to make this work!

Is there any way to make Spring.Net behave like PostSharp in this case? (that is, the user only needs to add XML attributes / configuration, and not edit the contents of any methods.

Alternatively, is there a decent and current alternative to PostSharp? I saw several questions like this on SO, but not one of them was really going to replace PostSharp, they just wanted to complement its functionality. I need a complete replacement.

+4
source share
2 answers

In short, yes, Spring.Net AOP can work the way you describe using XML-based configuration: you do not need to use these initial two lines of code, in fact, code-based configuration is not recommended. You can configure Spring.Net AOP only using the XML configuration, and this is really the recommended approach.

There are several steps for this:

  • Create your own tips: BeforeAdvice, AroundAdvice, AfterReturningAdvice and ThrowsAdvice are types of supported tips. AroundAdvice uses the AOPAlliance interface, the rest use Spring.AOP interfaces.
  • Define your pointcuts
  • Apply points and tips

Configuration example (generalized from a live configuration):

  <!-- START Spring.Net AOP --> <object id="beforeAdvice" type="MyBeforeAdvice, MyAOP"></object> <object id="beforeAdvisor" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop"> <property name="Advice" ref="beforeAdvice" /> </object> <object id="returnsAdvice" type="MyAfterReturningAdvice, MyAOP"></object> <object id="returnsAdvisor" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop"> <property name="Advice" ref="returnsAdvice" /> </object> <object id="throwsAdvice" type="MyThrowsAdvice, MyAOP"></object> <object id="throwsAdvisor" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop"> <property name="Advice" ref="throwsAdvice" /> </object> <!-- Advise objects --> <object type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop"> <property name="ObjectNames"> <list> <value>*Command</value> <value>...</value> </list> </property> <property name="InterceptorNames"> <list> <value>beforeAdvisor</value> <value>returnsAdvisor</value> <value>throwsAdvisor</value> </list> </property> </object> <!-- END Spring.Net AOP --> 

Weaving is performed at runtime and is fairly fast and non-intrusive.

Hope this is helpful

Andrew

+9
source

I think you are looking for a function to embed search methods .

You loaded the Spring.NET application context somewhere at the beginning. The code dependency on Spring.NET is minimal. The problem is that wherever you need a service (consultation), you explicitly depend on Spring.NET with ContextRegistry.GetContext () ..

You can get around this with replacing the method with the lookup method, for example:

Create an AbstractCommandFactory:

 namespace MyNamespace { public abstract class AbstractCommandFactory : ICommandFactory { public abstract ICommand getMyCommand(); } } 

Using Spring.NET, you can getMyCommand return a Spring.NET object:

 <objects> <object id="commandfactory" type="MyNamespace.AbstractCommandFactory, MyAssembly"> <lookup-method name="getMyCommand" object="commands.mycommand" /> </object> <object id="commands.mycommand" type=MyNamespace.MyCommandImplementation, MyAssembly" /> </objects> 

Now that you initialize the Spring.NET Application Context, download this factory command and pass the link. If you need an instance of MyCommandImplementation, you simply request it from the factory instance:

 public void doSomeWork() { // factory is an ICommandFactory // instantiated earlier using the explicit context.getObject("commandfactory") ICommand myCommand = this.factory.getMyCommand(); myCommand.Execute(); } 

Now your explicit dependency on Spring.NET is very minimal: only at boot time + instantiating your plants. The rest of your code stays clean.

Bonus points: you can more easily create ICommandFactory / ICommand mocks for unit code testing.

+2
source

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


All Articles