/// <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; } IList <IAdvisor> advisors = ResolveInterceptorNames(); foreach (IAdvisor advisor in advisors) { if (advisor is IIntroductionAdvisor) { proxyFactory.AddIntroduction((IIntroductionAdvisor)advisor); } else { proxyFactory.AddAdvisor(advisor); } } return(proxyFactory); }
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()); }
public void TestAutomaticInterfaceRecognitionInDelegate() { IIntroductionAdvisor ia = new DefaultIntroductionAdvisor(new Test(EXPECTED_TIMESTAMP)); TestObject target = new TestObject(); ProxyFactory pf = new ProxyFactory(target); pf.AddIntroduction(0, ia); ITimeStamped ts = (ITimeStamped)pf.GetProxy(); Assert.IsTrue(ts.TimeStamp == EXPECTED_TIMESTAMP); ((ITest)ts).Foo(); int age = ((ITestObject)ts).Age; }
public void TestIntroductionInterceptorWithDelegation() { TestObject raw = new TestObject(); Assert.IsTrue(!(raw is ITimeStamped)); ProxyFactory factory = new ProxyFactory(raw); ITimeStampedIntroduction ts = MockRepository.GenerateMock <ITimeStampedIntroduction>(); ts.Stub(x => x.TimeStamp).Return(EXPECTED_TIMESTAMP); DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ts); factory.AddIntroduction(advisor); ITimeStamped tsp = (ITimeStamped)factory.GetProxy(); Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP); }
public void TestIntroductionInterceptorWithDelegation() { TestObject raw = new TestObject(); Assert.IsTrue(!(raw is ITimeStamped)); ProxyFactory factory = new ProxyFactory(raw); ITimeStampedIntroduction ts = A.Fake <ITimeStampedIntroduction>(); A.CallTo(() => ts.TimeStamp).Returns(EXPECTED_TIMESTAMP); DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ts); factory.AddIntroduction(advisor); ITimeStamped tsp = (ITimeStamped)factory.GetProxy(); Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP); }
/// <summary> /// Create an AOP proxy for the given object. /// </summary> /// <param name="targetType">Type of the object.</param> /// <param name="targetName">The name of the object.</param> /// <param name="specificInterceptors">The set of interceptors that is specific to this /// object (may be empty but not null)</param> /// <param name="targetSource">The target source for the proxy, already pre-configured to access the object.</param> /// <returns>The AOP Proxy for the object.</returns> protected virtual object CreateProxy(Type targetType, string targetName, object[] specificInterceptors, ITargetSource targetSource) { ProxyFactory proxyFactory = CreateProxyFactory(); // copy our properties (proxyTargetClass) inherited from ProxyConfig proxyFactory.CopyFrom(this); object target = targetSource.GetTarget(); if (!ProxyTargetType) { // Must allow for introductions; can't just set interfaces to // the target's interfaces only. Type[] targetInterfaceTypes = AopUtils.GetAllInterfacesFromType(targetType); foreach (Type interfaceType in targetInterfaceTypes) { proxyFactory.AddInterface(interfaceType); } } IAdvisor[] advisors = BuildAdvisors(targetName, specificInterceptors); foreach (IAdvisor advisor in advisors) { if (advisor is IIntroductionAdvisor) { proxyFactory.AddIntroduction((IIntroductionAdvisor)advisor); } else { proxyFactory.AddAdvisor(advisor); } } proxyFactory.TargetSource = targetSource; CustomizeProxyFactory(proxyFactory); proxyFactory.IsFrozen = freezeProxy; return(proxyFactory.GetProxy()); }
public void TestIntroductionInterceptorWithInterfaceHierarchy() { TestObject raw = new TestObject(); Assert.IsTrue(!(raw is ISubTimeStamped)); ProxyFactory factory = new ProxyFactory(raw); ISubTimeStampedIntroduction ts = MockRepository.GenerateMock <ISubTimeStampedIntroduction>(); ts.Stub(x => x.TimeStamp).Return(EXPECTED_TIMESTAMP); DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ts); // we must add introduction, not an advisor factory.AddIntroduction(advisor); object proxy = factory.GetProxy(); ISubTimeStamped tsp = (ISubTimeStamped)proxy; Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP); }
public void TestIntroductionInterceptorWithSuperInterface() { TestObject raw = new TestObject(); Assert.IsTrue(!(raw is ITimeStamped)); ProxyFactory factory = new ProxyFactory(raw); ISubTimeStamped ts = MockRepository.GenerateMock <ISubTimeStampedIntroduction>(); ts.Stub(x => x.TimeStamp).Return(EXPECTED_TIMESTAMP); factory.AddIntroduction(0, new DefaultIntroductionAdvisor( (ISubTimeStampedIntroduction)ts, typeof(ITimeStamped)) ); ITimeStamped tsp = (ITimeStamped)factory.GetProxy(); Assert.IsTrue(!(tsp is ISubTimeStamped)); Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP); }
public void TestIntroductionInterceptorWithSuperInterface() { TestObject raw = new TestObject(); Assert.IsTrue(!(raw is ITimeStamped)); ProxyFactory factory = new ProxyFactory(raw); ISubTimeStampedIntroduction ts = A.Fake <ISubTimeStampedIntroduction>(); A.CallTo(() => ts.TimeStamp).Returns(EXPECTED_TIMESTAMP); factory.AddIntroduction(0, new DefaultIntroductionAdvisor( ts, typeof(ITimeStamped)) ); ITimeStamped tsp = (ITimeStamped)factory.GetProxy(); Assert.IsTrue(!(tsp is ISubTimeStamped)); Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP); }
public void testIntroductionInterceptorWithDelegation() { TestObject raw = new TestObject(); Assert.IsTrue(!(raw is ITimeStamped)); ProxyFactory factory = new ProxyFactory(raw); IDynamicMock tsControl = new DynamicMock(typeof(ITimeStampedIntroduction)); ITimeStampedIntroduction ts = (ITimeStampedIntroduction)tsControl.Object; tsControl.ExpectAndReturn("TimeStamp", EXPECTED_TIMESTAMP); DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ts); factory.AddIntroduction(advisor); ITimeStamped tsp = (ITimeStamped)factory.GetProxy(); Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP); tsControl.Verify(); }
public void DoesNotCacheWithDifferentInterfaces() { ProxyFactory advisedSupport = new ProxyFactory(new TestObject()); CreateAopProxy(advisedSupport); advisedSupport = new ProxyFactory(new TestObject()); advisedSupport.AddInterface(typeof(IPerson)); CreateAopProxy(advisedSupport); AssertAopProxyTypeCacheCount(2); // Same with Introductions advisedSupport = new ProxyFactory(new TestObject()); TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); ti.TimeStamp = new DateTime(666L); IIntroductionAdvisor introduction = new DefaultIntroductionAdvisor(ti, typeof(ITimeStamped)); advisedSupport.AddIntroduction(introduction); CreateAopProxy(advisedSupport); AssertAopProxyTypeCacheCount(3); }
public void testIntroductionInterceptorWithInterfaceHierarchy() { TestObject raw = new TestObject(); Assert.IsTrue(!(raw is ISubTimeStamped)); ProxyFactory factory = new ProxyFactory(raw); IDynamicMock tsControl = new DynamicMock(typeof(ISubTimeStampedIntroduction)); ISubTimeStampedIntroduction ts = (ISubTimeStampedIntroduction)tsControl.Object; tsControl.ExpectAndReturn("TimeStamp", EXPECTED_TIMESTAMP); DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ts); // we must add introduction, not an advisor factory.AddIntroduction(advisor); object proxy = factory.GetProxy(); ISubTimeStamped tsp = (ISubTimeStamped)proxy; Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP); tsControl.Verify(); }
/// <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; } IList<IAdvisor> advisors = ResolveInterceptorNames(); foreach (IAdvisor advisor in advisors) { if (advisor is IIntroductionAdvisor) { proxyFactory.AddIntroduction((IIntroductionAdvisor)advisor); } else { proxyFactory.AddAdvisor(advisor); } } return proxyFactory; }
public void DoesNotCacheWithDifferentInterfaces() { ProxyFactory advisedSupport = new ProxyFactory(new TestObject()); CreateAopProxy(advisedSupport); advisedSupport = new ProxyFactory(new TestObject()); advisedSupport.AddInterface(typeof(IPerson)); CreateAopProxy(advisedSupport); AssertAopProxyTypeCacheCount(2); // Same with Introductions advisedSupport = new ProxyFactory(new TestObject()); TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); ti.TimeStamp = new DateTime(666L); IIntroductionAdvisor introduction = new DefaultIntroductionAdvisor(ti, typeof(ITimeStamped)); advisedSupport.AddIntroduction(introduction); CreateAopProxy(advisedSupport); AssertAopProxyTypeCacheCount(3); }