public CatalogCache(Type stubType, ICachedComposablePartCatalogSite cachedCatalogSite) { Assumes.NotNull(stubType); cachedCatalogSite = cachedCatalogSite ?? new EmptyCachedComposablePartCatalogSite(); this._stubType = stubType; this._cachedCatalogSite = cachedCatalogSite; // Get catalog index MethodInfo getIndexMethod = ComposablePartCatalogAssemblyCacheReader.GetCacheTypeMethod(this._stubType, CacheStructureConstants.CachingStubGetCatalogIndexMethodName, BindingFlags.Public | BindingFlags.Static); Func <IDictionary <string, IEnumerable <IntPtr> > > getIndex = (Func <IDictionary <string, IEnumerable <IntPtr> > >)Delegate.CreateDelegate(typeof(Func <IDictionary <string, IEnumerable <IntPtr> > >), getIndexMethod); this._catalogIndex = getIndex.Invoke(); // Get part factory MethodInfo getPartFactoryMethod = ComposablePartCatalogAssemblyCacheReader.GetCacheTypeMethod(this._stubType, CacheStructureConstants.CachingStubGetPartFactoryMethodName, BindingFlags.Static | BindingFlags.Public); this._getPartFactory = (Func <IntPtr, ComposablePartDefinition>)Delegate.CreateDelegate(typeof(Func <IntPtr, ComposablePartDefinition>), getPartFactoryMethod); // get fields FieldInfo importDefinitionFactoryField = ComposablePartCatalogAssemblyCacheReader.GetCacheTypeField(this._stubType, CacheStructureConstants.CachingStubImportDefinitionFactoryFieldName, BindingFlags.Public | BindingFlags.Static); FieldInfo exportDefinitionFactoryField = ComposablePartCatalogAssemblyCacheReader.GetCacheTypeField(this._stubType, CacheStructureConstants.CachingStubExportDefinitionFactoryFieldName, BindingFlags.Public | BindingFlags.Static); FieldInfo partDefinitionFactoryField = ComposablePartCatalogAssemblyCacheReader.GetCacheTypeField(this._stubType, CacheStructureConstants.CachingStubPartDefinitionFactoryFieldName, BindingFlags.Public | BindingFlags.Static); // initialize the cache fields with the factory methods importDefinitionFactoryField.SetValue(null, new Func <ComposablePartDefinition, IDictionary <string, object>, ImportDefinition>(this._cachedCatalogSite.CreateImportDefinitionFromCache)); exportDefinitionFactoryField.SetValue(null, new Func <ComposablePartDefinition, IDictionary <string, object>, ExportDefinition>(this._cachedCatalogSite.CreateExportDefinitionFromCache)); partDefinitionFactoryField.SetValue(null, new Func <IDictionary <string, object>, Func <ComposablePartDefinition, IEnumerable <ImportDefinition> >, Func <ComposablePartDefinition, IEnumerable <ExportDefinition> >, ComposablePartDefinition>(this.InternalCreatePartDefinitionFromCache)); }
public CatalogCache(Type stubType, ICachedComposablePartCatalogSite cachedCatalogSite) { Assumes.NotNull(stubType); cachedCatalogSite = cachedCatalogSite ?? new EmptyCachedComposablePartCatalogSite(); this._stubType = stubType; this._cachedCatalogSite = cachedCatalogSite; // Get catalog index MethodInfo getIndexMethod = ComposablePartCatalogAssemblyCacheReader.GetCacheTypeMethod(this._stubType, CacheStructureConstants.CachingStubGetCatalogIndexMethodName, BindingFlags.Public | BindingFlags.Static); Func<IDictionary<string, IEnumerable<IntPtr>>> getIndex = (Func<IDictionary<string, IEnumerable<IntPtr>>>)Delegate.CreateDelegate(typeof(Func<IDictionary<string, IEnumerable<IntPtr>>>), getIndexMethod); this._catalogIndex = getIndex.Invoke(); // Get part factory MethodInfo getPartFactoryMethod = ComposablePartCatalogAssemblyCacheReader.GetCacheTypeMethod(this._stubType, CacheStructureConstants.CachingStubGetPartFactoryMethodName, BindingFlags.Static | BindingFlags.Public); this._getPartFactory = (Func<IntPtr, ComposablePartDefinition>)Delegate.CreateDelegate(typeof(Func<IntPtr, ComposablePartDefinition>), getPartFactoryMethod); // get fields FieldInfo importDefinitionFactoryField = ComposablePartCatalogAssemblyCacheReader.GetCacheTypeField(this._stubType, CacheStructureConstants.CachingStubImportDefinitionFactoryFieldName, BindingFlags.Public | BindingFlags.Static); FieldInfo exportDefinitionFactoryField = ComposablePartCatalogAssemblyCacheReader.GetCacheTypeField(this._stubType, CacheStructureConstants.CachingStubExportDefinitionFactoryFieldName, BindingFlags.Public | BindingFlags.Static); FieldInfo partDefinitionFactoryField = ComposablePartCatalogAssemblyCacheReader.GetCacheTypeField(this._stubType, CacheStructureConstants.CachingStubPartDefinitionFactoryFieldName, BindingFlags.Public | BindingFlags.Static); // initialize the cache fields with the factory methods importDefinitionFactoryField.SetValue(null, new Func<ComposablePartDefinition, IDictionary<string, object>, ImportDefinition>(this._cachedCatalogSite.CreateImportDefinitionFromCache)); exportDefinitionFactoryField.SetValue(null, new Func<ComposablePartDefinition, IDictionary<string, object>, ExportDefinition>(this._cachedCatalogSite.CreateExportDefinitionFromCache)); partDefinitionFactoryField.SetValue(null, new Func<IDictionary<string, object>, Func<ComposablePartDefinition, IEnumerable<ImportDefinition>>, Func<ComposablePartDefinition, IEnumerable<ExportDefinition>>, ComposablePartDefinition>(this.InternalCreatePartDefinitionFromCache)); }
public AssemblyCacheGenerator(ModuleBuilder moduleBuilder, GenerationServices generationServices, ICachedComposablePartCatalogSite cachedCatalogSite, string catalogIdentifier) { Assumes.NotNull(moduleBuilder); Assumes.NotNull(generationServices); Assumes.NotNull(cachedCatalogSite); Assumes.NotNull(catalogIdentifier); this._moduleBuilder = moduleBuilder; this._generationServices = generationServices; this._catalogIdentifier = catalogIdentifier ?? string.Empty; this._catalogMetadata = new Dictionary <string, object>(); this._catalogIndex = new Dictionary <string, List <MethodInfo> >(); this._cachedCatalogSite = cachedCatalogSite; }
/// <summary> /// Writes part definitions and catalog metadata into the cache. /// </summary> /// <param name="catalogType">Catalog type.</param> /// <param name="partDefinitions">Parts definitions.</param> /// <param name="catalogMetadata">Catalog Metadata.</param> /// <param name="catalogSite">Catalog Site</param> /// <returns>Catalog cache token. This value can be cached and used to locate this cache wen reading from the cache.</returns> public object WriteCache(Type catalogType, IEnumerable<ComposablePartDefinition> partDefinitions, IDictionary<string, object> catalogMetadata, ICachedComposablePartCatalogSite catalogSite) { Requires.NotNull(catalogType, "catalogType"); ConstructorInfo constructor = catalogType.GetConstructor( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(ComposablePartCatalogCache) }, null); if (constructor == null) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Strings.CacheableCatalogMustHaveProperConstructor, catalogType.FullName, typeof(ComposablePartCatalogCache).FullName)); } catalogMetadata = (catalogMetadata != null) ? new Dictionary<string, object>(catalogMetadata) : new Dictionary<string, object>(); catalogMetadata[ComposablePartCatalogCacheReader.CatalogTypeMetadataKey] = catalogType; partDefinitions = partDefinitions ?? Enumerable.Empty<ComposablePartDefinition>(); return this.WriteCacheCore(partDefinitions, catalogMetadata, catalogSite); }
protected override object WriteCacheCore(IEnumerable<ComposablePartDefinition> partDefinitions, IDictionary<string, object> catalogMetadata, ICachedComposablePartCatalogSite catalogSite) { this.ThrowIfDisposed(); catalogSite = catalogSite ?? new EmptyCachedComposablePartCatalogSite(); CachingResult result = CachingResult.SucceededResult; int currentCatalogIdentifierCounter = this._currentCatalogIdentifierCouner++; string currentCatalogIdentifier = string.Format(CultureInfo.InvariantCulture, "{0}", currentCatalogIdentifierCounter); AssemblyCacheGenerator generator = new AssemblyCacheGenerator(this._moduleBuilder, this._generationServices, catalogSite, currentCatalogIdentifier); generator.BeginGeneration(); foreach (ComposablePartDefinition partDefinition in partDefinitions) { result = result.MergeErrors(generator.CachePartDefinition(partDefinition).Errors); } generator.CacheCatalogMetadata(catalogMetadata); CachingResult<Type> stubGenerationResult = generator.EndGeneration(); result = result.MergeErrors(stubGenerationResult.Errors); result.ThrowOnErrors(); return currentCatalogIdentifier; }
/// <summary> /// Writes part definitions and catalog metadata into the cache. /// </summary> /// <param name="partDefinitions">Parts definitions.</param> /// <param name="catalogMetadata">Catalog Metadata.</param> /// <param name="catalogSite">Catalog Site</param> /// <returns>Catalog cache token. This value can be cached and used to locate this cache wen reading from the cache.</returns> protected abstract object WriteCacheCore(IEnumerable<ComposablePartDefinition> partDefinitions, IDictionary<string, object> catalogMetadata, ICachedComposablePartCatalogSite catalogSite);
public override ComposablePartCatalog GetCacheCatalog(ICachedComposablePartCatalogSite catalogSite) { Requires.NotNull(catalogSite, "catalogSite"); return new CatalogCache(this._stubType, catalogSite); }
protected override object WriteCacheCore(IEnumerable <ComposablePartDefinition> partDefinitions, IDictionary <string, object> catalogMetadata, ICachedComposablePartCatalogSite catalogSite) { this.ThrowIfDisposed(); catalogSite = catalogSite ?? new EmptyCachedComposablePartCatalogSite(); CachingResult result = CachingResult.SucceededResult; int currentCatalogIdentifierCounter = this._currentCatalogIdentifierCouner++; string currentCatalogIdentifier = string.Format(CultureInfo.InvariantCulture, "{0}", currentCatalogIdentifierCounter); AssemblyCacheGenerator generator = new AssemblyCacheGenerator(this._moduleBuilder, this._generationServices, catalogSite, currentCatalogIdentifier); generator.BeginGeneration(); foreach (ComposablePartDefinition partDefinition in partDefinitions) { result = result.MergeErrors(generator.CachePartDefinition(partDefinition).Errors); } generator.CacheCatalogMetadata(catalogMetadata); CachingResult <Type> stubGenerationResult = generator.EndGeneration(); result = result.MergeErrors(stubGenerationResult.Errors); result.ThrowOnErrors(); return(currentCatalogIdentifier); }
/// <summary> /// Returns the catalog that represents the cache. Note, that this is not the originally cached catalog, bur rather a representation /// of the cached part definitions. /// </summary> /// <param name="catalogSite"></param> /// <returns></returns> public abstract ComposablePartCatalog GetCacheCatalog(ICachedComposablePartCatalogSite catalogSite);
public override ComposablePartCatalog GetCacheCatalog(ICachedComposablePartCatalogSite catalogSite) { Requires.NotNull(catalogSite, "catalogSite"); return(new CatalogCache(this._stubType, catalogSite)); }
/// <summary> /// Writes part definitions and catalog metadata into the cache. /// </summary> /// <param name="partDefinitions">Parts definitions.</param> /// <param name="catalogMetadata">Catalog Metadata.</param> /// <param name="catalogSite">Catalog Site</param> /// <returns>Catalog cache token. This value can be cached and used to locate this cache wen reading from the cache.</returns> protected abstract object WriteCacheCore(IEnumerable <ComposablePartDefinition> partDefinitions, IDictionary <string, object> catalogMetadata, ICachedComposablePartCatalogSite catalogSite);
/// <summary> /// Writes part definitions and catalog metadata into the cache. /// </summary> /// <param name="catalogType">Catalog type.</param> /// <param name="partDefinitions">Parts definitions.</param> /// <param name="catalogMetadata">Catalog Metadata.</param> /// <param name="catalogSite">Catalog Site</param> /// <returns>Catalog cache token. This value can be cached and used to locate this cache wen reading from the cache.</returns> public object WriteCache(Type catalogType, IEnumerable <ComposablePartDefinition> partDefinitions, IDictionary <string, object> catalogMetadata, ICachedComposablePartCatalogSite catalogSite) { Requires.NotNull(catalogType, "catalogType"); ConstructorInfo constructor = catalogType.GetConstructor( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(ComposablePartCatalogCache) }, null); if (constructor == null) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Strings.CacheableCatalogMustHaveProperConstructor, catalogType.FullName, typeof(ComposablePartCatalogCache).FullName)); } catalogMetadata = (catalogMetadata != null) ? new Dictionary <string, object>(catalogMetadata) : new Dictionary <string, object>(); catalogMetadata[ComposablePartCatalogCacheReader.CatalogTypeMetadataKey] = catalogType; partDefinitions = partDefinitions ?? Enumerable.Empty <ComposablePartDefinition>(); return(this.WriteCacheCore(partDefinitions, catalogMetadata, catalogSite)); }