public OnElementLoadedEvent( [Import(AllowDefault = true)] IPatternManager patternManager, [Import(AllowDefault = true)] IInstanceBase currentElement) { this.currentElement = currentElement; this.patternManager = patternManager; this.sourceEvent = WeakObservable.FromEvent <EventArgs>( handler => this.ElementLoaded += handler, handler => this.ElementLoaded -= handler); if (patternManager != null) { this.patternManager.IsOpenChanged += (sender, args) => this.OnOpenChanged(); if (this.patternManager.IsOpen) { this.storeEvent = WeakObservable.FromEvent <ValueEventArgs <IInstanceBase> >( handler => this.patternManager.Store.ElementLoaded += handler, handler => this.patternManager.Store.ElementLoaded -= handler); this.storeEventSubscription = this.storeEvent.Subscribe(this.OnStoreElementLoaded); } } }
private static bool IsTargetInterfaceDefinition(IInstanceBase target, Guid definitionId) { // Are we creating an extension pattern? var guestProduct = target as IProduct; if (guestProduct != null && guestProduct.Info != null) { // See if the guest pattern implements an extension point within the host pattern var hostProduct = guestProduct.Product; if (hostProduct != null && hostProduct != guestProduct && hostProduct.Info != null) { var extensions = hostProduct.Info.FindAllDescendants <IExtensionPointInfo>(); if (extensions.Any()) { var extensionDefinitionId = extensions.FirstOrDefault(rex => guestProduct.Info.ProvidedExtensionPoints.Any(pex => pex.ExtensionPointId == rex.RequiredExtensionPointId)); if (extensionDefinitionId != null) { if (extensionDefinitionId.Id == definitionId) { return(true); } } } } } // Did we ask for correct interface? return(definitionId == target.DefinitionId); }
public void Initialize() { this.manager = new Mock<IPatternManager>(); this.store = new Mock<IProductState>(); this.current = Mock.Of<IInstanceBase>(); this.manager.Setup(x => x.Store).Returns(this.store.Object); this.publisher = new OnElementDeletedEvent(this.manager.Object, this.current); }
/// <summary> /// Attempts to retrieve (from cache or by creating and caching it the first time) /// the typed interface layer proxy defined by the runtime instance, without knowing /// its type. This is used in the add rules to initialize the interface layers. /// </summary> public static IToolkitInterface GetInterfaceLayer(this IInstanceBase instance) { Guard.NotNull(() => instance, instance); if (instance.Info == null) { return(null); } var key = new ToolkitInterfaceLayerCacheKey(instance, instance.DefinitionId); var layer = default(object); var canCache = instance.Root != null && instance.Root.ProductState != null && instance.Root.ProductState.PropertyBag != null; if (canCache && instance.Root.ProductState.PropertyBag.TryGetValue(key, out layer)) { return((IToolkitInterface)layer); } var interfaceService = default(IToolkitInterfaceService); if (!canCache || (interfaceService = instance.Root.ProductState.TryGetService <IToolkitInterfaceService>()) == null) { tracer.Warn(Resources.ToolkitInterfaceLayer_ServiceUnavailable); return(null); } var definitionId = Guid.Empty; var proxyType = interfaceService.AllInterfaces .Where(export => !string.IsNullOrEmpty(export.Metadata.DefinitionId) && Guid.TryParse(export.Metadata.DefinitionId, out definitionId)) .Where(export => export.Metadata.ExtensionId == instance.Info.GetRoot().ExtensionId&& definitionId == instance.DefinitionId) .Select(export => export.Metadata.ProxyType) .FirstOrDefault(); if (proxyType != null) { try { layer = Activator.CreateInstance(proxyType, instance); if (layer != null && canCache) { instance.Root.ProductState.PropertyBag[key] = layer; } return(layer as IToolkitInterface); } catch (System.Reflection.TargetInvocationException tie) { tracer.Error(tie.InnerException, Resources.ToolkitInterfaceLayer_TraceFailedInstantiation, proxyType); } } return(null); }
public void Initialize() { this.manager = new Mock <IPatternManager>(); this.store = new Mock <IProductState>(); this.current = Mock.Of <IInstanceBase>(); this.manager.Setup(x => x.Store).Returns(this.store.Object); this.publisher = new OnElementInstantiatedEvent(this.manager.Object, this.current); }
private IEnumerable <IInstanceBase> RecursiveElementParent(IInstanceBase parent) { if (parent == null) { return(Enumerable.Empty <IInstanceBase>()); } return(new[] { parent }.Concat(RecursiveElementParent(parent.Parent))); }
private void AddAncestorElementsEntries(IInstanceBase element, string parentPath = null) { Guard.NotNull(() => element, element); var parentElement = element.Parent; string elementPath = ParentNotation; if (!string.IsNullOrEmpty(parentPath)) { elementPath = GetPropertyKey(parentPath, elementPath); } var parentProduct = parentElement as IProduct; if (parentProduct != null) { AddSelfEntries(parentProduct, elementPath); return; } else { var parentView = parentElement as IView; if (parentView != null) { AddSelfEntries(parentView, elementPath); AddDecendantElementsEntries(parentView, elementPath, element); AddAncestorElementsEntries(parentView, elementPath); } else { var parentAbstractElement = parentElement as IAbstractElement; if (parentAbstractElement != null) { AddSelfEntries(parentAbstractElement, elementPath); AddDecendantElementsEntries(parentAbstractElement, elementPath, element); AddAncestorElementsEntries(parentAbstractElement, elementPath); } else { throw new NotImplementedException( string.Format(CultureInfo.CurrentCulture, Resources.ProductElementDictionaryConverter_ErrorElementNotSupportAncestors, element)); } } } }
public void Initialize() { this.id = Guid.NewGuid(); this.provider = new RuntimeElementUriProvider(); var serviceProvider = new Mock<IServiceProvider>(); var namedElement = new Mock<IInstanceBase>(); var productStore = new Mock<IProductState>(); var manager = new Mock<IPatternManager>(); namedElement.SetupGet(n => n.Id).Returns(this.id); productStore.Setup(p => p.FindAll<IInstanceBase>()).Returns(new IInstanceBase[] { namedElement.Object }); manager.SetupGet(m => m.Store).Returns(productStore.Object); serviceProvider.Setup(sp => sp.GetService(typeof(IPatternManager))).Returns(manager.Object); this.element = namedElement.Object; this.provider.ServiceProvider = serviceProvider.Object; }
public void Initialize() { this.id = Guid.NewGuid(); this.provider = new RuntimeElementUriProvider(); var serviceProvider = new Mock <IServiceProvider>(); var namedElement = new Mock <IInstanceBase>(); var productStore = new Mock <IProductState>(); var manager = new Mock <IPatternManager>(); namedElement.SetupGet(n => n.Id).Returns(this.id); productStore.Setup(p => p.FindAll <IInstanceBase>()).Returns(new IInstanceBase[] { namedElement.Object }); manager.SetupGet(m => m.Store).Returns(productStore.Object); serviceProvider.Setup(sp => sp.GetService(typeof(IPatternManager))).Returns(manager.Object); this.element = namedElement.Object; this.provider.ServiceProvider = serviceProvider.Object; }
/// <summary> /// Retrieves a cached interface layer proxy or creates a new one and caches it /// the first time. Infers the toolkit from the toolkit interface attribute /// on the <typeparamref name="TInterface"/>. /// </summary> private static TInterface GetInterfaceLayer <TInterface, TProxied>(IInstanceBase target) where TInterface : class { Func <TInterface> toolkit = () => { var interfaceType = typeof(TInterface); var interfaceAttr = GetToolkitInterfaceAttributeOrThrow(interfaceType); if (!IsTargetInterfaceDefinition(target, new Guid(interfaceAttr.DefinitionId))) { return(default(TInterface)); } var proxyType = interfaceAttr.ProxyType; ThrowIfProxyDoesNotImplementInterface(proxyType, interfaceType); var constructor = GetProxyConstructorOrThrow(proxyType, typeof(TProxied)); return((TInterface)constructor.Invoke(new object[] { target })); }; return(GetInterfaceLayer(target, toolkit)); }
public void WhenPropertyInstantiated_ThenRaisesInstantiatedEvent() { var store = new DslTestStore <ProductStateStoreDomainModel>(); IProductState productState = null; IProduct product = null; store.TransactionManager.DoWithinTransaction(() => { productState = store.ElementFactory.CreateElement <ProductState>(); product = productState.CreateProduct(); }); IInstanceBase instantiated = null; productState.ElementInstantiated += (sender, args) => instantiated = args.Value; store.TransactionManager.DoWithinTransaction(() => { product.CreateProperty(); }); Assert.NotNull(instantiated); }
/// <summary> /// Retrieves a cached interface layer proxy or creates a new one using /// the toolkit specified and caches it the first time. /// </summary> private static TInterface GetInterfaceLayer <TInterface>(IInstanceBase element, Func <TInterface> toolkit) where TInterface : class { var typed = element as TInterface; if (typed != null) { return(typed); } // We'll never be able to cache in this case. if (element.Root == null || element.Root.ProductState == null || element.Root.ProductState.PropertyBag == null) { return(toolkit()); } var layer = default(object); var key = new ToolkitInterfaceLayerCacheKey(element, element.DefinitionId); if (element.Root.ProductState.PropertyBag.TryGetValue(key, out layer)) { return(layer as TInterface); } else { var result = toolkit(); if (result != default(TInterface)) { element.Root.ProductState.PropertyBag[key] = result; } return(result); } }
public void WhenProductInstantiatedWhileSerializing_ThenDoesNotRaiseInstantiatedEvent() { var store = new DslTestStore <ProductStateStoreDomainModel>(); store.Store.PropertyBag[ProductState.IsSerializingKey] = true; IProductState productState = null; store.TransactionManager.DoWithinTransaction(() => { productState = store.ElementFactory.CreateElement <ProductState>(); }); IInstanceBase instantiated = null; IProduct product = null; productState.ElementInstantiated += (sender, args) => instantiated = args.Value; store.TransactionManager.DoWithinTransaction(() => { product = productState.CreateProduct(); }); Assert.Null(instantiated); }
/// <summary> /// Gets a strong typed interface layer for a pattern. /// </summary> public static TInterface As <TInterface>(this IInstanceBase instance) { Guard.NotNull(() => instance, instance); if (resolvingInstanceBaseAs.Value.GetValueOrDefault()) { throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, Resources.ToolkitInterfaceLayer_InstanceTypeDoesNotHaveInterfaceLayer, instance.GetType())); } dynamic dynInstance = instance; resolvingInstanceBaseAs.Value = true; try { return((TInterface)ToolkitInterfaceLayer.As <TInterface>(dynInstance)); } finally { resolvingInstanceBaseAs.Value = null; } }
private static bool IsRootProduct(IInstanceBase current) { return current is IProduct && current.Parent == null; }
private IEnumerable<IInstanceBase> RecursiveElementParent(IInstanceBase parent) { if (parent == null) { return Enumerable.Empty<IInstanceBase>(); } return new[] { parent }.Concat(RecursiveElementParent(parent.Parent)); }
private static bool IsRootProduct(IInstanceBase current) { return(current is IProduct && current.Parent == null); }
private void AddDecendantElementsEntries(IElementContainer container, string containerPath = null, IInstanceBase originatingChild = null) { Guard.NotNull(() => container, container); // Maybe one or more instances of different element types (filter the originatingchild instance) to prevent infinite recursion back down to a descendant element. foreach (var childElementGroup in container.Elements.Where(e => e != originatingChild).GroupBy(e => e.DefinitionName)) { var groupIndex = 0; foreach (var childElement in childElementGroup) { string elementPath = GetPluralizedElementPath(childElement, groupIndex); if (!string.IsNullOrEmpty(containerPath)) { elementPath = GetPropertyKey(containerPath, elementPath); } AddSelfEntries(childElement, elementPath); AddDecendantElementsEntries(childElement, elementPath); groupIndex++; } } // Maybe one or more instances of different extension types (filter the originatingchild instance) to prevent infinite recursion. foreach (var childExtensionGroup in container.Extensions.Where(e => e != originatingChild).GroupBy(e => e.DefinitionName)) { var groupIndex = 0; foreach (var childExtension in childExtensionGroup) { var extensionPoint = container.Info.ExtensionPoints .FirstOrDefault(ep => childExtension.Info.ProvidedExtensionPoints.FirstOrDefault(x => x.ExtensionPointId == ep.RequiredExtensionPointId) != null); if (extensionPoint != null) { string elementPath = GetPluralizedExtensionPath(extensionPoint, groupIndex); if (!string.IsNullOrEmpty(containerPath)) { elementPath = GetPropertyKey(containerPath, elementPath); } AddSelfEntries(childExtension, elementPath); groupIndex++; } } } }
public ToolkitInterfaceLayerCacheKey(IInstanceBase runtimeElement, Guid definitionId) { this.Element = runtimeElement; this.DefinitionId = definitionId; }
/// <summary> /// Traverses from the specified parent to all the children. /// </summary> /// <param name="parent">The parent to traverse.</param> public static IEnumerable <IInstanceBase> Traverse(this IInstanceBase parent) { return(Traverse(new[] { parent })); }