本文整理汇总了C#中Spring.Aop.Framework.ProxyFactory.AddAdvisor方法的典型用法代码示例。如果您正苦于以下问题:C# ProxyFactory.AddAdvisor方法的具体用法?C# ProxyFactory.AddAdvisor怎么用?C# ProxyFactory.AddAdvisor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Spring.Aop.Framework.ProxyFactory
的用法示例。
在下文中一共展示了ProxyFactory.AddAdvisor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetProxy
private static Contact GetProxy()
{
Contact target = new Contact();
target.FirstName = "Aleksandar";
target.LastName = "Seovic";
ProxyFactory pf = new ProxyFactory(target);
pf.AddAdvisor(new ModificationAdvisor(target.GetType()));
pf.AddIntroduction(new IsModifiedAdvisor());
pf.ProxyTargetType = true;
return (Contact)pf.GetProxy();
}
示例2: Matches
public void Matches()
{
SerializablePerson target = new SerializablePerson();
target.SetAge(27);
ControlFlowPointcut cflow = new ControlFlowPointcut(typeof(One), "GetAge");
ProxyFactory factory = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
IPerson proxied = (IPerson) factory.GetProxy();
factory.AddAdvisor(new DefaultPointcutAdvisor(cflow, nop));
// not advised, not under One...
Assert.AreEqual(target.GetAge(), proxied.GetAge());
Assert.AreEqual(0, nop.Count, "Whoops, appear to be advising when not under One's cflow.");
// will be advised...
One one = new One();
Assert.AreEqual(27, one.GetAge(proxied));
Assert.AreEqual(1, nop.Count, "Not advising when under One's cflow (must be).");
// won't be advised...
Assert.AreEqual(target.GetAge(), new One().NoMatch(proxied));
Assert.AreEqual(1, nop.Count, "Whoops, appear to be advising when under One's cflow scope, BUT NOT under a target method's cflow scope.");
Assert.AreEqual(3, cflow.EvaluationCount, "Pointcut not invoked the correct number of times.");
}
示例3: RemoveAdvisorByReference
public void RemoveAdvisorByReference()
{
TestObject target = new TestObject();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
IAdvisor advisor = new DefaultPointcutAdvisor(cba);
pf.AddAdvice(nop);
pf.AddAdvisor(advisor);
ITestObject proxied = (ITestObject)pf.GetProxy();
proxied.Age = 5;
Assert.AreEqual(1, cba.GetCalls());
Assert.AreEqual(1, nop.Count);
Assert.IsFalse(pf.RemoveAdvisor(null));
Assert.IsTrue(pf.RemoveAdvisor(advisor));
Assert.AreEqual(5, proxied.Age);
Assert.AreEqual(1, cba.GetCalls());
Assert.AreEqual(2, nop.Count);
Assert.IsFalse(pf.RemoveAdvisor(new DefaultPointcutAdvisor(null)));
}
示例4: RemoveAdvisorByIndex
public void RemoveAdvisorByIndex()
{
TestObject target = new TestObject();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
IAdvisor advisor = new DefaultPointcutAdvisor(cba);
pf.AddAdvice(nop);
pf.AddAdvisor(advisor);
NopInterceptor nop2 = new NopInterceptor(2); // make instance unique (see SPRNET-847)
pf.AddAdvice(nop2);
ITestObject proxied = (ITestObject)pf.GetProxy();
proxied.Age = 5;
Assert.AreEqual(1, cba.GetCalls());
Assert.AreEqual(1, nop.Count);
Assert.AreEqual(1, nop2.Count);
// Removes counting before advisor
pf.RemoveAdvisor(1);
Assert.AreEqual(5, proxied.Age);
Assert.AreEqual(1, cba.GetCalls());
Assert.AreEqual(2, nop.Count);
Assert.AreEqual(2, nop2.Count);
// Removes Nop1
pf.RemoveAdvisor(0);
Assert.AreEqual(5, proxied.Age);
Assert.AreEqual(1, cba.GetCalls());
Assert.AreEqual(2, nop.Count);
Assert.AreEqual(3, nop2.Count);
// Check out of bounds
try
{
pf.RemoveAdvisor(-1);
Assert.Fail("Supposed to throw exception");
}
catch (AopConfigException)
{
// Ok
}
try
{
pf.RemoveAdvisor(2);
Assert.Fail("Supposed to throw exception");
}
catch (AopConfigException)
{
// Ok
}
Assert.AreEqual(5, proxied.Age);
Assert.AreEqual(4, nop2.Count);
}
示例5: NestedProxiesDontInvokeSameAdviceOrIntroductionTwice
public void NestedProxiesDontInvokeSameAdviceOrIntroductionTwice()
{
MultiProxyingTestClass testObj = new MultiProxyingTestClass();
ProxyFactory pf1 = new ProxyFactory();
pf1.Target = testObj;
NopInterceptor di = new NopInterceptor();
NopInterceptor diUnused = new NopInterceptor(1); // // make instance unique (see SPRNET-847)
TestCountingIntroduction countingMixin = new TestCountingIntroduction();
pf1.AddAdvice(diUnused);
pf1.AddAdvisor(new DefaultPointcutAdvisor(di));
pf1.AddIntroduction(new DefaultIntroductionAdvisor(countingMixin));
object innerProxy = pf1.GetProxy();
ProxyFactory pf2 = new ProxyFactory();
pf2.Target = innerProxy;
pf2.AddAdvice(diUnused);
pf2.AddAdvisor(new DefaultPointcutAdvisor(di));
pf2.AddIntroduction(new DefaultIntroductionAdvisor(countingMixin));
object outerProxy = pf2.GetProxy();
// any advice instance is invoked once only
string result = ((IMultiProxyingTestInterface)outerProxy).TestMethod("arg");
Assert.AreEqual(1, testObj.InvocationCounter);
Assert.AreEqual("arg|arg", result);
Assert.AreEqual(1, di.Count);
// any introduction instance is invoked once only
((ICountingIntroduction)outerProxy).Inc();
Assert.AreEqual(1, countingMixin.Counter);
}
示例6: IndexOfMethods
public void IndexOfMethods()
{
TestObject target = new TestObject();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
IAdvisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
IAdvised advised = (IAdvised)pf.GetProxy();
// Can use advised and ProxyFactory interchangeably
advised.AddAdvice(nop);
pf.AddAdvisor(advisor);
Assert.AreEqual(-1, pf.IndexOf((IInterceptor)null));
Assert.AreEqual(-1, pf.IndexOf(new NopInterceptor()));
Assert.AreEqual(0, pf.IndexOf(nop));
Assert.AreEqual(-1, advised.IndexOf((IAdvisor)null));
Assert.AreEqual(1, pf.IndexOf(advisor));
Assert.AreEqual(-1, advised.IndexOf(new DefaultPointcutAdvisor(null)));
}
示例7: CreateProxyFactoryWithoutTargetThenSetTarget
public void CreateProxyFactoryWithoutTargetThenSetTarget()
{
TestObject target = new TestObject();
target.Name = "Adam";
NopInterceptor nopInterceptor = new NopInterceptor();
CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
ProxyFactory pf = new ProxyFactory();
pf.Target = target;
pf.AddAdvice(nopInterceptor);
pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
object proxy = pf.GetProxy();
ITestObject to = (ITestObject)proxy;
Assert.AreEqual("Adam", to.Name);
Assert.AreEqual(1, countingBeforeAdvice.GetCalls());
}
示例8: CacheTest
public void CacheTest()
{
for (int i = 0; i < 2; i++)
{
TestObject target = new TestObject();
NopInterceptor nopInterceptor = new NopInterceptor();
CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
ProxyFactory pf = new ProxyFactory();
pf.Target = target;
pf.AddAdvice(nopInterceptor);
pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
object proxy = pf.GetProxy();
}
// fails when running in resharper/testdriven.net
// DynamicProxyManager.SaveAssembly();
}
示例9: CreateProxyFactory
/// <summary>
/// Create an AOP proxy for the given object.
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <param name="objectName">The name of the object.</param>
/// <returns>The AOP Proxy for the object.</returns>
protected virtual ProxyFactory CreateProxyFactory(Type objectType, string objectName)
{
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.ProxyTargetAttributes = this.ProxyTargetAttributes;
proxyFactory.TargetSource = new InheritanceBasedAopTargetSource(objectType);
if (!ProxyInterfaces)
{
proxyFactory.Interfaces = Type.EmptyTypes;
}
IAdvisor[] advisors = ResolveInterceptorNames();
foreach (IAdvisor advisor in advisors)
{
if (advisor is IIntroductionAdvisor)
{
proxyFactory.AddIntroduction((IIntroductionAdvisor)advisor);
}
else
{
proxyFactory.AddAdvisor(advisor);
}
}
return proxyFactory;
}
示例10: AddAndRemoveEventHandlerThroughInterceptor
public void AddAndRemoveEventHandlerThroughInterceptor()
{
TestObject target = new TestObject();
NopInterceptor nopInterceptor = new NopInterceptor();
CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
target.Name = "SOME-NAME";
ProxyFactory pf = new ProxyFactory(target);
pf.AddAdvice(nopInterceptor);
pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
object proxy = pf.GetProxy();
ITestObject to = proxy as ITestObject;
// add event handler through proxy
to.Click += new EventHandler(OnClick);
OnClickWasCalled = false;
to.OnClick();
Assert.IsTrue(OnClickWasCalled);
Assert.AreEqual(2, countingBeforeAdvice.GetCalls());
// remove event handler through proxy
to.Click -= new EventHandler(OnClick);
OnClickWasCalled = false;
to.OnClick();
Assert.IsFalse(OnClickWasCalled);
Assert.AreEqual(4, countingBeforeAdvice.GetCalls());
}
示例11: AddPersistenceExceptionTranslation
protected virtual void AddPersistenceExceptionTranslation(ProxyFactory pf, IPersistenceExceptionTranslator pet)
{
pf.AddAdvisor(new PersistenceExceptionTranslationAdvisor(pet, typeof(RepositoryAttribute)));
}
示例12: PostProcessAfterInitialization
/// <summary>
/// Add PersistenceExceptionTranslationAdvice to candidate object if it is a match.
/// Create AOP proxy if necessary or add advice to existing advice chain.
/// </summary>
/// <param name="instance">The new object instance.</param>
/// <param name="objectName">The name of the object.</param>
/// <returns>
/// The object instance to use, wrapped with either the original or a wrapped one.
/// </returns>
/// <exception cref="Spring.Objects.ObjectsException">
/// In case of errors.
/// </exception>
public object PostProcessAfterInitialization(object instance, string objectName)
{
IAdvised advised = instance as IAdvised;
Type targetType;
if (advised != null)
{
targetType = advised.TargetSource.TargetType;
} else
{
targetType = instance.GetType();
}
if (targetType == null)
{
// Can't do much here
return instance;
}
if (AopUtils.CanApply(this.persistenceExceptionTranslationAdvisor, targetType, ReflectionUtils.GetInterfaces(targetType)))
{
if (advised != null)
{
advised.AddAdvisor(this.persistenceExceptionTranslationAdvisor);
return instance;
}
else
{
ProxyFactory proxyFactory = new ProxyFactory(instance);
// copy our properties inherited from ProxyConfig
proxyFactory.CopyFrom(this);
proxyFactory.AddAdvisor(this.persistenceExceptionTranslationAdvisor);
return proxyFactory.GetProxy();
}
} else
{
return instance;
}
}
示例13: SelectiveApplication
public void SelectiveApplication()
{
SerializablePerson target = new SerializablePerson();
target.SetAge(27);
NopInterceptor nop = new NopInterceptor();
ControlFlowPointcut cflow = new ControlFlowPointcut(typeof (One));
IPointcut settersUnderOne = Pointcuts.Intersection(SetterPointcut.Instance, cflow);
ProxyFactory pf = new ProxyFactory(target);
IPerson proxied = (IPerson) pf.GetProxy();
pf.AddAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop));
// Not advised, not under One
target.SetAge(16);
Assert.AreEqual(0, nop.Count);
// Not advised; under One but not a setter
Assert.AreEqual(16, new One().GetAge(proxied));
Assert.AreEqual(0, nop.Count);
// Won't be advised
new One().Set(proxied);
Assert.AreEqual(1, nop.Count);
// We saved most evaluations
Assert.AreEqual(1, cflow.EvaluationCount);
}
示例14: ReplaceAdvisor
public void ReplaceAdvisor()
{
TestObject target = new TestObject();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba1 = new CountingBeforeAdvice();
CountingBeforeAdvice cba2 = new CountingBeforeAdvice();
IAdvisor advisor1 = new DefaultPointcutAdvisor(cba1);
IAdvisor advisor2 = new DefaultPointcutAdvisor(cba2);
pf.AddAdvisor(advisor1);
pf.AddAdvice(nop);
ITestObject proxied = (ITestObject)pf.GetProxy();
// Use the type cast feature
// Replace etc methods on advised should be same as on ProxyFactory
IAdvised advised = (IAdvised)proxied;
proxied.Age = 5;
Assert.AreEqual(1, cba1.GetCalls());
Assert.AreEqual(0, cba2.GetCalls());
Assert.AreEqual(1, nop.Count);
Assert.IsFalse(advised.ReplaceAdvisor(null, null));
Assert.IsFalse(advised.ReplaceAdvisor(null, advisor2));
Assert.IsFalse(advised.ReplaceAdvisor(advisor1, null));
Assert.IsTrue(advised.ReplaceAdvisor(advisor1, advisor2));
Assert.AreEqual(advisor2, pf.Advisors[0]);
Assert.AreEqual(5, proxied.Age);
Assert.AreEqual(1, cba1.GetCalls());
Assert.AreEqual(2, nop.Count);
Assert.AreEqual(1, cba2.GetCalls());
Assert.IsFalse(pf.ReplaceAdvisor(new DefaultPointcutAdvisor(null), advisor1));
}
示例15: AddAndRemoveEventHandlerThroughIntroduction
public void AddAndRemoveEventHandlerThroughIntroduction()
{
TestObject target = new TestObject();
DoubleClickableIntroduction dci = new DoubleClickableIntroduction();
DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(dci);
CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
target.Name = "SOME-NAME";
ProxyFactory pf = new ProxyFactory(target);
pf.AddIntroduction(advisor);
pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
object proxy = pf.GetProxy();
ITestObject to = proxy as ITestObject;
Assert.IsNotNull(to);
Assert.AreEqual("SOME-NAME", to.Name);
IDoubleClickable doubleClickable = proxy as IDoubleClickable;
// add event handler through introduction
doubleClickable.DoubleClick += new EventHandler(OnClick);
OnClickWasCalled = false;
doubleClickable.FireDoubleClickEvent();
Assert.IsTrue(OnClickWasCalled);
Assert.AreEqual(3, countingBeforeAdvice.GetCalls());
// remove event handler through introduction
doubleClickable.DoubleClick -= new EventHandler(OnClick);
OnClickWasCalled = false;
doubleClickable.FireDoubleClickEvent();
Assert.IsFalse(OnClickWasCalled);
Assert.AreEqual(5, countingBeforeAdvice.GetCalls());
}