public void Remove(ContractIdentity identity) { if (_contracts.ContainsKey(identity)) { _contracts.Remove(identity); } }
public void OnComponentCreated(ContractIdentity identity, IComponentFactory componentFactory, Type componentTargetType, ref object componentInstance, object originalInstance) { var match = false; if (IncludedContracts != null) { match |= IncludedContracts.Match(identity.Type); } if (!match) { return; } if (!identity.Type.IsInterface) { throw new ArgumentException( "Can not create wrapper for non-interface type contracts. " + "The transaction composition listener should be configured such that " + "the non-interface type contracts do not match the wrapping criteria."); } var interceptor = new TracingInterceptor(identity.Type.Name); var callHanlder = new InterceptingAdapterEmittedTypeHanlder(componentInstance, interceptor); componentInstance = ClassEmitter.EmitInterfaceInstance(callHanlder, identity.Type); }
public void OnComponentComposed(ContractIdentity identity, IEnumerable <InitializationPointSpecification> initializationPoints, IEnumerable <object> initializationPointResults, Type componentTargetType, object componentInstance, object originalInstance) { // Ignore. // There's nothing to do after a component is composed. }
public void OnComponentCreated(ContractIdentity identity, IComponentFactory componentFactory, Type componentTargetType, ref object componentInstance, object originalInstance) { if (componentInstance is SampleComponentOne) { componentInstance = new SampleComponentTwo(); } }
public virtual object GetComponent(Type contract, string name) { if (contract.ContainsGenericParameters) { throw new CompositionException("Requested contract type " + contract.Name + " contains open generic parameters. Can not construct a concrete type."); } var identity = new ContractIdentity(contract, name); var factories = _repository.FindFactories(identity); using (var enumerator = factories?.GetEnumerator()) { if (enumerator == null) { return(null); } while (enumerator.MoveNext()) { var result = enumerator.Current?.GetComponentInstance(identity, _compositionListeners.Values); if (result != null) { return(result); } } } return(null); }
public void Add(ContractIdentity identity, IComponentFactory factory) { if (!_contracts.ContainsKey(identity)) { _contracts.Add(identity, new List <IComponentFactory>()); } _contracts[identity].Add(factory); }
public void OnComponentRetrieved(ContractIdentity identity, IComponentFactory componentFactory, Type componentTargetType, ref object componentInstance, object originalInstance) { RetrievedIdentity = identity; RetrievedComponentFactory = componentFactory; RetrievedComponentTargetType = componentTargetType; RetrievedComponentInstance = componentInstance; RetrievedOriginalInstance = originalInstance; }
public virtual void Unregister(ContractIdentity identity) { if (identity == null) { throw new ArgumentNullException(); } _repository.Remove(identity); }
public virtual IEnumerable <object> GetAllComponents(Type contract, string name) { var identity = new ContractIdentity(contract, name); var factories = _repository.FindFactories(identity); return(factories .Select(f => f.GetComponentInstance(identity, _compositionListeners.Values)) .Where(result => result != null)); }
private void NotifyComposed(object componentInstance, object originalComponentInstance, List <object> initializationPointResults, ContractIdentity contract, IEnumerable <ICompositionListener> listenerChain) { foreach (var compositionListener in listenerChain) { compositionListener.OnComponentComposed(contract, _initializationPoints, initializationPointResults, _targetType, componentInstance, originalComponentInstance); } }
public object GetComponentInstance(ContractIdentity contract, IEnumerable <ICompositionListener> listenerChain) { var result = new DefaultCalculator(); result.Adder = _composer.GetComponent <IAdder>(); result.Multiplier = _composer.GetComponent <IMultiplier>(); result.Divider = _composer.GetComponent <IDivider>(); return(result); }
public object GetComponentInstance(ContractIdentity contract, IEnumerable <ICompositionListener> listenerChain) { // Check if the factory is initialized if (_composer == null) { throw new InvalidOperationException( "LocalComponentFactory should be initialized before calling GetComponentInstance method."); } if (_componentCache == null) { // If the component is not cached at all, then unique instances should // be created for each call, then locking does not help and just // degrades performance. So, create without locking. var newComponent = CreateComponent(contract, listenerChain); return(NotifyRetrieved(newComponent.ComponentInstance, newComponent.OriginalComponentInstance, contract, listenerChain)); } // Check if the component is cached, and ready to deliver var componentCacheEntry = _componentCache.GetFromCache(contract); if (componentCacheEntry != null) { return(NotifyRetrieved(componentCacheEntry.ComponentInstance, componentCacheEntry.OriginalComponentInstance, contract, listenerChain)); } // If the component is cached, then lock the component instance // to avoid creation of more than one cached components per config // when called in concurrent manner lock (_componentCache.SynchronizationObject) { // Double-check the initialization to avoid rendezvouz componentCacheEntry = _componentCache.GetFromCache(contract); if (componentCacheEntry != null) { return(NotifyRetrieved(componentCacheEntry.ComponentInstance, componentCacheEntry.OriginalComponentInstance, contract, listenerChain)); } componentCacheEntry = CreateComponent(contract, listenerChain); return(NotifyRetrieved(componentCacheEntry.ComponentInstance, componentCacheEntry.OriginalComponentInstance, contract, listenerChain)); } }
private ComponentCacheEntry CreateComponent(ContractIdentity contract, IEnumerable <ICompositionListener> listenerChain) { // Prepare the constructor information for instantiating the object. var constructorArguments = PrepareConstructorArguments(); var targetConstructor = FindTargetConstructor(constructorArguments); // Save the original component instance reference, so that // we can apply initialization points to it later, as the // composition listeners may change the reference to a // wrapped one. object originalComponentInstance = targetConstructor.Invoke(constructorArguments.ToArray()); // After constructing the component object, first process // all composition listeners so that if the reference should // change, it changes before setting it to the cache. // Otherwise, in circular dependency scenarios, dependent // components may get unwrapped component where the component // is wrapped by composition listeners. object componentInstance = NotifyCreated(originalComponentInstance, contract, listenerChain); // Store the cache, so that if there is a circular dependency, // applying initialization points is not blocked and chained // recursively. var result = new ComponentCacheEntry { ComponentInstance = componentInstance, OriginalComponentInstance = originalComponentInstance }; _componentCache?.PutInCache(contract, result); // Complete the object initialization by applying the initialization // points. They should be applied to the original component instance, // as the reference may have been changed by composition listeners to // an instance that does not have the original configuration points. var initializationPointResults = ApplyInitializationPoints(originalComponentInstance); // Inform all composition listeners of the newly composed // component instance by calling OnComponentComposed method. NotifyComposed(componentInstance, originalComponentInstance, initializationPointResults, contract, listenerChain); // The composition is now finished for the component instance. // See if an [OnCompositionComplete] method is specified, call it. // This should be called on the original component instance // for the same reason stated above. InvokeCompositionNotifications(componentInstance); return(result); }
public void OnComponentComposed(ContractIdentity identity, IEnumerable <InitializationPointSpecification> initializationPoints, IEnumerable <object> initializationPointResults, Type componentTargetType, object componentInstance, object originalInstance) { ComposedIdentity = identity; ComposedInitializationPoints = initializationPoints; ComposedInitializationPointResults = initializationPointResults; ComposedComponentTargetType = componentTargetType; ComposedComponentInstance = componentInstance; ComposedOriginalInstance = originalInstance; }
public ComponentCacheEntry GetFromCache(ContractIdentity contract) { var contractString = GetContractString(contract); if (HttpContext.Current.Session[contractString] != null) { return(HttpContext.Current.Session[contractString] as ComponentCacheEntry); } return(null); }
private object NotifyCreated(object originalComponentInstance, ContractIdentity contract, IEnumerable <ICompositionListener> listenerChain) { var componentInstance = originalComponentInstance; foreach (var compositionListener in listenerChain) { compositionListener.OnComponentCreated(contract, this, _targetType, ref componentInstance, originalComponentInstance); } return(componentInstance); }
public void OnComponentCreated(ContractIdentity identity, IComponentFactory componentFactory, Type componentTargetType, ref object componentInstance, object originalInstance) { Console.WriteLine("LISTENER - BigNumberFilterListener.OnComponentCreated({0})", identity == null ? "<null>" : identity.Type.Name); Console.WriteLine(" - Wrapping component for filtering big numbers."); if ((identity != null) && (identity.Type.IsInterface)) { var handler = new InterceptingAdapterEmittedTypeHanlder(componentInstance, new BigNumberFilterInterceptor()); var wrappedComponent = ClassEmitter.EmitInterfaceInstance(handler, identity.Type); componentInstance = wrappedComponent; } }
public void OnComponentCreated(ContractIdentity identity, IComponentFactory componentFactory, Type componentTargetType, ref object componentInstance, object originalInstance) { Console.WriteLine("LISTENER - OnComponentCreated: {0}", identity.Type.Name); Console.WriteLine(" - Wrapping component for logging."); if (identity.Type.IsInterface) { var handler = new InterceptingAdapterEmittedTypeHanlder(componentInstance, new LogToConsoleInterceptor()); var wrappedComponent = ClassEmitter.EmitInterfaceInstance(handler, identity.Type); componentInstance = wrappedComponent; } }
public ComponentCacheEntry GetFromCache(ContractIdentity contract) { if (HttpContext.Current == null) { return(null); } if (HttpContext.Current.Items.Contains(contract)) { return(HttpContext.Current.Items[contract] as ComponentCacheEntry); } return(null); }
public IEnumerable <IComponentFactory> FindFactories(ContractIdentity identity) { _contracts.TryGetValue(identity, out var closedResults); if (!identity.Type.IsGenericType) { return(closedResults ?? Enumerable.Empty <IComponentFactory>()); } var genericContractType = identity.Type.GetGenericTypeDefinition(); var genericIdentity = new ContractIdentity(genericContractType, identity.Name); if (_contracts.TryGetValue(genericIdentity, out var genericResults)) { return(closedResults?.Concat(genericResults) ?? genericResults); } return(closedResults ?? Enumerable.Empty <IComponentFactory>()); }
public ComponentCacheEntry GetFromCache(ContractIdentity contract) { var context = OwinRequestScopeContext.Current; if (context == null) { return(null); } var cache = GetCacheDictionary(context); if (cache.ContainsKey(contract)) { return(cache[contract]); } return(null); }
private static void RunUnregister(CompositionCommandInfo info, XmlProcessingContext xmlProcessingContext) { if (!(info is UnregisterInfo unregisterInfo)) { throw new ArgumentException("Invalid runner input type: error in static setup."); } var contractType = SimpleTypeParserUtil.ParseType(unregisterInfo.ContractType, xmlProcessingContext); if (contractType == null) { xmlProcessingContext.ReportError($"Type '{unregisterInfo.ContractType}' could not be loaded."); return; } var contractIdentity = new ContractIdentity(contractType, unregisterInfo.ContractName); xmlProcessingContext.ComponentContext.Unregister(contractIdentity); }
private static string GetIdentityString(ContractIdentity contract) { if (contract == null) { return(AppKeyPrefix + "<NullContract>"); } if (contract.Type == null) { return(AppKeyPrefix + "<NullType>"); } if (contract.Name == null) { return(AppKeyPrefix + contract.Type.FullName); } return(AppKeyPrefix + contract.Type.FullName + "," + contract.Name); }
public object GetComponentInstance(ContractIdentity contract, IEnumerable <ICompositionListener> listenerChain) { var channelFactoryType = Type.GetType( string.Format( "System.ServiceModel.ChannelFactory`1[[{0}]], System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", contract.Type.AssemblyQualifiedName)); var channelFactory = channelFactoryType.GetConstructor(Type.EmptyTypes).Invoke(new object[0]); var propertyInfo = channelFactoryType.GetProperty("Endpoint"); var endpoint = propertyInfo.GetValue(channelFactory, null); endpoint.GetType().GetProperty("Binding").SetValue(endpoint, _binding, null); var result = channelFactoryType.GetMethod("CreateChannel", new[] { typeof(EndpointAddress) }).Invoke(channelFactory, new object[] { _address }); if (_knownTypes != null) { foreach (var od in ((ServiceEndpoint)endpoint).Contract.Operations) { if (od.Behaviors.Contains(typeof(CustomOperationBehavior))) { od.Behaviors.Find <CustomOperationBehavior>().KnownTypes = _knownTypes; } } } var originalComponentObject = result; foreach (var compositionListener in listenerChain) { compositionListener.OnComponentCreated(contract, this, contract.Type, ref result, originalComponentObject); } return(result); }
public void OnComponentComposed(ContractIdentity identity, IEnumerable <InitializationPointSpecification> initializationPoints, IEnumerable <object> initializationPointResults, Type componentTargetType, object componentInstance, object originalInstance) { if (IncludedContracts != null) { if (!IncludedContracts.Match(identity.Type)) { return; } } if (IncludedComponents != null) { if (!IncludedComponents.Match(componentTargetType)) { return; } } OnComponentComposedCount++; }
public void OnComponentRetrieved(ContractIdentity identity, IComponentFactory componentFactory, Type componentTargetType, ref object componentInstance, object originalInstance) { if (IncludedContracts != null) { if (!IncludedContracts.Match(identity.Type)) { return; } } if (IncludedComponents != null) { if (!IncludedComponents.Match(componentTargetType)) { return; } } OnComponentRetrievedCount++; }
public object GetComponentInstance(ContractIdentity contract, IEnumerable <ICompositionListener> listenerChain) { var requestedClosedContractType = contract.Type; if (!requestedClosedContractType.IsGenericType) { throw new CompositionException("Requested contract " + requestedClosedContractType.Name + " is not a generic type."); } if (requestedClosedContractType.ContainsGenericParameters) { throw new CompositionException("Requested contract " + requestedClosedContractType.Name + " is not fully constructed and closed."); } var requestedGenericContractType = contract.Type.GetGenericTypeDefinition(); if (!_contractTypes.ContainsKey(requestedGenericContractType)) { throw new CompositionException("The requested generic contract type definition " + requestedGenericContractType.Name + " is not among the supported contracts for this factory."); } var originalGenericContractType = _contractTypes[requestedGenericContractType]; var closedTargetType = CloseGenericType(_targetType, originalGenericContractType, requestedClosedContractType); var subFactory = _subFactories.GetOrAdd(closedTargetType, type => { var newSubFactory = new LocalComponentFactory(type); newSubFactory.InitializationPoints.AddRange(_initializationPoints); newSubFactory.Initialize(_composer); return(newSubFactory); }); return(subFactory.GetComponentInstance(contract, listenerChain)); }
public object GetComponentInstance(ContractIdentity contract, IEnumerable <ICompositionListener> listenerChain) { return(_componentInstance); }
public void PutInCache(ContractIdentity contract, ComponentCacheEntry entry) { _cacheContent[contract] = entry; }
public ComponentCacheEntry GetFromCache(ContractIdentity contract) { return(_cacheContent.ContainsKey(contract) ? _cacheContent[contract] : null); }