How can I use spring.net for methods?

I sent a message to the spring.net forum, but also hoped I could get some values:

I am considering some recommendations on how I could fulfill the following requirement.

First, some background - I use spring.NET to achieve an intuitive IOC injection into my asp.net C # multi tier web application. Injection is achieved using the spring.net xml configuration file with all my development, achieved through development against interfaces and injection into a class implemented in the interface where necessary. All of this works great for me. My knowledge will be an intermediate level, I believe.

I ran into a problem and tried to find a solution for it.

To take, for example, I have a class with several methods where they are all extracted from the database, the data is cached for a certain period of time.

I want to have the freedom to introduce caching characteristics to each method, for example. so that he lives in a cache, etc. Therefore, instead of entering the class where it is needed, I also want to be able to enter values ​​into methods.

I could go the way of creating class properties for each method, but this becomes useless or introduces parameters into my class. My preference is to control each method with spring injection.

So, any ideas on how this can be achieved, as I expect it to be something others may have come across.

One of the possible ideas that I came up with is to somehow add an attribute to each of the methods via spring with an attribute having properties with values ​​that are required in the method, for example, cache duration, etc. Is this a valid solution? If so, can someone help me in setting these up.

Or, if someone has other ideas, it would be great.

+4
source share
2 answers

If you take the bbaia approach, this might look like an example below. This is a little simplified, but you can apply it to your situation. Suppose we have an IDoWorkForSomeTime interface with a single time parameter:

 public interface IDoWorkForSomeTime { void Work(int time); } 

It is implemented using TimeBoundWorker and AdvisedTimeBoundWorker :

 public class TimeBoundWorker : IDoWorkForSomeTime { public void Work(int time) { Console.WriteLine("Working for {0} hours", time); } } public class AdvisedTimeBoundWorker : IDoWorkForSomeTime { /* *** Note The Attribute *** */ [ChangeParameter] public void Work(int time) { Console.WriteLine("Working for {0} hours", time); } } 

Then we can configure the AOP proxy to change the time parameter at runtime, so when we run the following program:

 class Program { static void Main(string[] args) { IApplicationContext ctx = new XmlApplicationContext("objects.xml"); var worker = (IDoWorkForSomeTime)ctx.GetObject("plainWorker"); var advisedWorker = (IDoWorkForSomeTime)ctx.GetObject("advisedWorker"); worker.Work(4); advisedWorker.Work(4); } } 

It outputs:

 Working for 4 hours Working for 8 hours 

So, although I call it the value 4, advisedWorker uses the value 8, which I configured in my spring configuration file. The spring container sees the [ChangeParameter] attribute and knows from my configuration that it should use the following interceptor method:

 public class ChangeParamInterceptor : IMethodInterceptor { public int NewValue { get; set; } // set in spring cofig public object Invoke(IMethodInvocation invocation) { invocation.Arguments[0] = NewValue; // change the argument object rval = invocation.Proceed(); return rval; } } 

This requires the spring configuration in objects.xml :

 <?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <object id="plainWorker" type="Examples.Aop.Shared.TimeBoundWorker, Examples.Aop.Shared" singleton="true"> </object> <object id="advisedWorker" type="Examples.Aop.Shared.AdvisedTimeBoundWorker, Examples.Aop.Shared" singleton="true"> </object> <!-- AOP configuration: --> <object id="changeParamAdvice" type="Examples.Aop.Shared.ChangeParamInterceptor, Examples.Aop.Shared"> <!-- AH! there the 8 --> <property name="NewValue" value="8" /> </object> <object id="attributePointcut" type="Spring.Aop.Support.AttributeMatchMethodPointcut, Spring.Aop"> <property name="Attribute" value="Examples.Aop.Shared.ChangeParameterAttribute, Examples.Aop.Shared" /> </object> <object id="changeParamAspect" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop"> <property name="Pointcut" ref="attributePointcut" /> <property name="Advice" ref="changeParamAdvice"/> </object> <object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator, Spring.Aop" /> </objects> 

You have many more options for setting up AOP and applying tips like ChangeParamInterceptor . Read more in the Spring .NET Documentation on AOP .

+6
source

You can try using Spring.NET AOP . You can change the parameters of the method at runtime.

0
source

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


All Articles