public void ShouldCacheIEnumerableParameter() { var realRepository = new UserRepository(); var repository = AspectFactory.Create <IUserRepository>(realRepository); Guid id1, id2; repository.Save(new User { Id = id1 = Guid.NewGuid(), Name = "User 1" }); repository.Save(new User { Id = id2 = Guid.NewGuid(), Name = "User 2" }); var l1 = repository.List(new[] { id1, id2 }); var l2 = repository.List(new[] { id1, id2 }); l2 = repository.List(new[] { id1, id2 }); var l3 = repository.ListParams(id1, id2); var l4 = repository.ListParams(id1, id2); l4 = repository.ListParams(id1, id2); Assert.AreEqual(1, realRepository.ListCount); Assert.AreEqual(1, realRepository.ListParamsCount); }
public void ShouldProxyParameterlessMethods() { var obj = new ParameterPassingTest(pInt: 99, pString: "teste"); var proxy = AspectFactory.Create <IParameterPassingTest>(obj); proxy.Test4Void(); int vInt = proxy.Test4Int(); string vString = proxy.Test4String(); Assert.AreEqual(obj.Test4Int(), vInt); Assert.AreEqual(obj.Test4String(), vString); }
/// <summary> /// If an resource was loaded (new or existing), the resource /// can be assigned to this view model /// </summary> private async Task AssignLoadedResource(ResourceModel resource) { IsBusy = true; try { // Load type from type tree var typeTreeModel = await ResourceServiceModel.GetTypeTree(); var flatTypeTree = typeTreeModel.DerivedTypes.Flatten(t => t.DerivedTypes).SingleOrDefault(t => t.Name == resource.Type); Type = new ResourceTypeViewModel(flatTypeTree); EditableObject = new ResourceViewModel(resource, Type); if (AspectUsage) { var typedAspects = Config.AspectConfigurations.FirstOrDefault(ac => ac.TypeName == resource.Type); List <AspectConfiguration> aspectConfigurations; if (typedAspects == null || typedAspects.Aspects.Count == 0) { aspectConfigurations = Config.DefaultAspects; } else { aspectConfigurations = typedAspects.Aspects; } var aspects = aspectConfigurations.Select(ca => (IResourceAspect)AspectFactory.Create(ca.PluginName)) .Where(a => a.IsRelevant(EditableObject)).ToArray(); var aspectLoadTask = new List <Task>(aspects.Select(aspect => aspect.Load(EditableObject))); await Task.WhenAll(aspectLoadTask); Aspects.Items.AddRange(aspects); } Logger.Log(LogLevel.Trace, "Loaded resource with id {0}.", EditableObject.Id); } catch (Exception e) { //TODO throw; } finally { IsBusy = false; } }
public void ShouldExecuteGlobalAspects() { MethodContext startContext = null; MethodContext endContext = null; TestAspectAttribute.InterceptStartCallback = (ctx) => startContext = ctx; TestAspectAttribute.InterceptEndCallback = (ctx) => endContext = ctx; AspectFactory.RegisterGlobalAspect(new TestAspectAttribute()); var proxy = AspectFactory.Create <IParameterPassingTest>(new ParameterPassingTest()); proxy.Test3(); Assert.IsNotNull(startContext, "InterceptStart was called."); Assert.IsNotNull(endContext, "InterceptEnd was called."); }
public void ShouldFilterException() { Exception filteredException = null; ExceptionFilterAspect.DefaultHandler += (ex) => filteredException = ex; var proxy = AspectFactory.Create <IExceptionFilterTest>(new ExceptionFilterTest()); try { proxy.RaiseException(); } catch (Exception ex) { Assert.AreEqual(ex, filteredException); } }
public void ShouldProxyRefParameters() { var obj = new ParameterPassingTest(pInt: 99, pString: "teste", pDateTime: DateTime.Now); var proxy = AspectFactory.Create <IParameterPassingTest>(obj); int vInt; string vString; DateTime vDateTime; proxy.Teste5(out vInt, out vString, out vDateTime); Assert.AreEqual(obj.PInt, vInt); Assert.AreEqual(obj.PString, vString); Assert.AreEqual(obj.PDateTime, vDateTime); }
public void ShouldImplementCacheAspect() { var realRepository = new UserRepository(); var repository = AspectFactory.Create <IUserRepository>(realRepository); var user = new User { Id = Guid.NewGuid(), Name = "User 1" }; repository.Save(user); var u1 = repository.GetById(user.Id); var u2 = repository.GetById(user.Id); var u3 = repository.GetById(user.Id); Assert.AreEqual(1, realRepository.GetByIdCount); }
/// <inheritdoc /> public async Task Load(long productId) { Execute.OnUIThread(() => IsBusy = true); try { var model = await ProductServiceModel.GetProductDetails(productId); EditableObject = new ProductViewModel(model); if (AspectUsage) { var typedAspects = Config.AspectConfigurations.FirstOrDefault(ac => ac.TypeName == model.Type); List <AspectConfiguration> aspectConfigurations; if (typedAspects == null || typedAspects.Aspects.Count == 0) { aspectConfigurations = Config.DefaultAspects; } else { aspectConfigurations = typedAspects.Aspects; } // Load aspects var aspects = aspectConfigurations.Select(ca => (IProductAspect)AspectFactory.Create(ca.PluginName)) .Where(a => a.IsRelevant(EditableObject)).ToArray(); var aspectLoadTasks = new List <Task>(aspects.Select(aspect => aspect.Load(EditableObject))); await Task.WhenAll(aspectLoadTasks); Aspects.Items.AddRange(aspects); } } catch { //TODO throw; } finally { IsBusy = false; } }
public void MultipleAspectShouldIntercept() { MethodContext startContext1, startContext2, endContext1, endContext2; startContext1 = startContext2 = endContext1 = endContext2 = null; TestAspectAttribute.InterceptStartCallback = (ctx) => startContext1 = ctx; TestAspectAttribute.InterceptEndCallback = (ctx) => endContext1 = ctx; TestAspect2Attribute.InterceptStartCallback = (ctx) => startContext2 = ctx; TestAspect2Attribute.InterceptEndCallback = (ctx) => endContext2 = ctx; var obj = new ParameterPassingTest(); var proxy = AspectFactory.Create <IParameterPassingTest>(obj); proxy.Test2("test", 99); Assert.IsNotNull(startContext1, "InterceptStart1 was called."); Assert.IsNotNull(endContext1, "InterceptEnd1 was called."); Assert.IsNotNull(startContext2, "InterceptStart2 was called."); Assert.IsNotNull(endContext2, "InterceptEnd2 was called."); }
public void ParametersShouldBePassedProperly() { MethodContext startContext = null; MethodContext endContext = null; TestAspectAttribute.InterceptStartCallback = (ctx) => startContext = ctx; TestAspectAttribute.InterceptEndCallback = (ctx) => endContext = ctx; var obj = new ParameterPassingTest(5, "test", DateTime.Now.Subtract(TimeSpan.FromMinutes(9999)), long.MaxValue, true, Enumerable.Range(10, 5).ToArray()); var proxy = AspectFactory.Create <IParameterPassingTest>(obj); bool ok = proxy.Test(obj.PInt, obj.PString, obj.PDateTime, obj.PLong, obj.PBool, obj.PArray); Assert.IsTrue(ok, "Parameters was ok on real object."); Assert.IsNotNull(startContext, "InterceptStart was called."); Assert.IsNotNull(endContext, "InterceptEnd was called."); var parameters = new object[] { obj.PInt, obj.PString, obj.PDateTime, obj.PLong, obj.PBool, obj.PArray }; bool okInterceptParameters = startContext.Parameters.Select(i => i.Value).SequenceEqual(parameters); Assert.IsTrue(okInterceptParameters, "Parameters received in intercept method was ok on aspect."); }