IEnumerable <Registration> GetRegistrationsFor(Type type, IInstanceStore tempInstanceStore) { var registrations = new List <Registration>(); // use temp instance store first if (tempInstanceStore != null) { lock (tempInstanceStore.Mutex) { if (tempInstanceStore.ContainsRegistrationsFor(type)) { registrations.AddRange(tempInstanceStore.GetRegistrationsFor(type)); } } } // TODO: send to bottom? lock (this.transientInstanceStore.Mutex) registrations.AddRange(this.transientInstanceStore.GetRegistrationsFor(type)); lock (this.singletonInstanceStore.Mutex) registrations.AddRange(this.singletonInstanceStore.GetRegistrationsFor(type)); lock (this.httpContextOrExecutionContextLocalStore.Mutex) registrations.AddRange(this.httpContextOrExecutionContextLocalStore.GetRegistrationsFor(type)); if (registrations.Any(r => r.InjectionBehaviour == InjectionBehaviour.Override)) { return(registrations.Where(r => r.InjectionBehaviour == InjectionBehaviour.Override).ToArray()); } return(registrations); }
/// <summary> /// Trys to get an instance from the instance store, creating it if it doesnt exist /// </summary> /// <param name="requestType">The requested type</param> /// <param name="instanceStore"></param> /// <param name="tempInstanceStore"></param> /// <param name="buildStack"></param> /// <returns></returns> IEnumerable GetOrCreateInstances(Type requestType, IInstanceStore instanceStore, IInstanceStore tempInstanceStore, Stack <Type> buildStack) { var typesToCreate = this.GetTypesToCreate(requestType, buildStack); var instances = new List <Tuple <Registration, object> >(); if (tempInstanceStore != null && tempInstanceStore.ContainsInstancesFor(requestType)) { instances.AddRange(tempInstanceStore.GetInstances(requestType).Cast <Tuple <Registration, object> >()); } else if (instanceStore.ContainsInstancesFor(requestType)) { instances.AddRange(instanceStore.GetInstances(requestType).Cast <Tuple <Registration, object> >()); } foreach (var registration in typesToCreate) { if (!instances.Any(i => i != null && i.Item1 == registration)) { var newinstance = this.GetInstance(registration, tempInstanceStore, new Stack <Type>(buildStack.Reverse())); instanceStore.Insert(registration, requestType, newinstance); instances.Add(new Tuple <Registration, object>(registration, newinstance)); } } return(instances.Select(i => i.Item2).ToArray()); }
public RetrieveResourceService( IInstanceStore instanceStore, IFileStore blobDataStore, ITranscoder transcoder, IFrameHandler frameHandler, IRetrieveTransferSyntaxHandler retrieveTransferSyntaxHandler, IDicomRequestContextAccessor dicomRequestContextAccessor, ILogger <RetrieveResourceService> logger) { EnsureArg.IsNotNull(instanceStore, nameof(instanceStore)); EnsureArg.IsNotNull(blobDataStore, nameof(blobDataStore)); EnsureArg.IsNotNull(transcoder, nameof(transcoder)); EnsureArg.IsNotNull(frameHandler, nameof(frameHandler)); EnsureArg.IsNotNull(retrieveTransferSyntaxHandler, nameof(retrieveTransferSyntaxHandler)); EnsureArg.IsNotNull(dicomRequestContextAccessor, nameof(dicomRequestContextAccessor)); EnsureArg.IsNotNull(logger, nameof(logger)); _instanceStore = instanceStore; _blobDataStore = blobDataStore; _transcoder = transcoder; _frameHandler = frameHandler; _retrieveTransferSyntaxHandler = retrieveTransferSyntaxHandler; _dicomRequestContextAccessor = dicomRequestContextAccessor; _logger = logger; }
public RetrieveMetadataServiceTests(DataStoreTestsFixture storagefixture) { _instanceStore = Substitute.For <IInstanceStore>(); _metadataStore = storagefixture.MetadataStore; _eTagGenerator = Substitute.For <IETagGenerator>(); _retrieveMetadataService = new RetrieveMetadataService(_instanceStore, _metadataStore, _eTagGenerator); }
public RetrieveResourceService( IInstanceStore instanceStore, IFileStore blobDataStore, ITranscoder transcoder, IFrameHandler frameHandler, IRetrieveTransferSyntaxHandler retrieveTransferSyntaxHandler, RecyclableMemoryStreamManager recyclableMemoryStreamManager, ILogger <RetrieveResourceService> logger) { EnsureArg.IsNotNull(instanceStore, nameof(instanceStore)); EnsureArg.IsNotNull(blobDataStore, nameof(blobDataStore)); EnsureArg.IsNotNull(transcoder, nameof(transcoder)); EnsureArg.IsNotNull(frameHandler, nameof(frameHandler)); EnsureArg.IsNotNull(retrieveTransferSyntaxHandler, nameof(retrieveTransferSyntaxHandler)); EnsureArg.IsNotNull(recyclableMemoryStreamManager, nameof(recyclableMemoryStreamManager)); EnsureArg.IsNotNull(logger, nameof(logger)); _instanceStore = instanceStore; _blobDataStore = blobDataStore; _transcoder = transcoder; _frameHandler = frameHandler; _retrieveTransferSyntaxHandler = retrieveTransferSyntaxHandler; _recyclableMemoryStreamManager = recyclableMemoryStreamManager; _logger = logger; }
public RetrieveMetadataServiceTests() { _instanceStore = Substitute.For <IInstanceStore>(); _metadataStore = Substitute.For <IMetadataStore>(); _eTagGenerator = Substitute.For <IETagGenerator>(); _retrieveMetadataService = new RetrieveMetadataService(_instanceStore, _metadataStore, _eTagGenerator); }
public Container() { this.singletonInstanceStore = new SingletonInstanceStore(); this.httpContextOrExecutionContextLocalStore = new HttpContextOrExecutionContextLocalInstanceStore(); this.transientInstanceStore = new TransientInstanceStore(); this.Inject <IContainer>(this); }
public InstanceStoreTests(SqlDataStoreTestsFixture fixture) { _instanceStore = EnsureArg.IsNotNull(fixture?.InstanceStore, nameof(fixture.InstanceStore)); _indexDataStore = EnsureArg.IsNotNull(fixture?.IndexDataStore, nameof(fixture.IndexDataStore)); _extendedQueryTagStore = EnsureArg.IsNotNull(fixture?.ExtendedQueryTagStore, nameof(fixture.ExtendedQueryTagStore)); _indexDataStoreTestHelper = EnsureArg.IsNotNull(fixture?.IndexDataStoreTestHelper, nameof(fixture.IndexDataStoreTestHelper)); _extendedQueryTagStoreTestHelper = EnsureArg.IsNotNull(fixture?.ExtendedQueryTagStoreTestHelper, nameof(fixture.ExtendedQueryTagStoreTestHelper)); _partitionStore = EnsureArg.IsNotNull(fixture?.PartitionStore, nameof(fixture.PartitionStore)); }
public static async Task <IEnumerable <VersionedInstanceIdentifier> > GetInstancesToRetrieve( this IInstanceStore instanceStore, ResourceType resourceType, int partitionKey, string studyInstanceUid, string seriesInstanceUid, string sopInstanceUid, CancellationToken cancellationToken) { EnsureArg.IsNotNull(instanceStore, nameof(instanceStore)); IEnumerable <VersionedInstanceIdentifier> instancesToRetrieve = Enumerable.Empty <VersionedInstanceIdentifier>(); switch (resourceType) { case ResourceType.Frames: case ResourceType.Instance: instancesToRetrieve = await instanceStore.GetInstanceIdentifierAsync( partitionKey, studyInstanceUid, seriesInstanceUid, sopInstanceUid, cancellationToken); break; case ResourceType.Series: instancesToRetrieve = await instanceStore.GetInstanceIdentifiersInSeriesAsync( partitionKey, studyInstanceUid, seriesInstanceUid, cancellationToken); break; case ResourceType.Study: instancesToRetrieve = await instanceStore.GetInstanceIdentifiersInStudyAsync( partitionKey, studyInstanceUid, cancellationToken); break; default: Debug.Fail($"Unknown retrieve transaction type: {resourceType}", nameof(resourceType)); break; } if (!instancesToRetrieve.Any()) { ThrowNotFoundException(resourceType); } return(instancesToRetrieve); }
public Container() { this.registeredTypes = new Dictionary<Type, IList<Registration>>(); this.singletonInstanceStore = new SingletonInstanceStore(); this.httpContextOrThreadLocalStore = new HttpContextOrThreadLocalInstanceStore(); this.httpContextOrExecutionContextLocalStore = new HttpContextOrExecutionContextLocalInstanceStore(); this.mutex = new object(); this.Inject<IContainer>(this); }
public RetrieveMetadataServiceTests() { _instanceStore = Substitute.For <IInstanceStore>(); _metadataStore = Substitute.For <IMetadataStore>(); _eTagGenerator = Substitute.For <IETagGenerator>(); _dicomRequestContextAccessor = Substitute.For <IDicomRequestContextAccessor>(); _dicomRequestContextAccessor.RequestContext.DataPartitionEntry = new PartitionEntry(DefaultPartition.Key, DefaultPartition.Name); _retrieveMetadataService = new RetrieveMetadataService(_instanceStore, _metadataStore, _eTagGenerator, _dicomRequestContextAccessor); }
internal Container(Container container) { this.singletonInstanceStore = container.singletonInstanceStore.Clone(); this.httpContextOrExecutionContextLocalStore = container.httpContextOrExecutionContextLocalStore.Clone(); this.transientInstanceStore = container.transientInstanceStore.Clone(); // remove old container this.RemoveAllRegistrationsAndInstancesOf <IContainer>(); // the contain can resolve itself); this.Inject <IContainer>(this); }
public RetrieveResourceServiceTests() { _instanceStore = Substitute.For <IInstanceStore>(); _fileStore = Substitute.For <IFileStore>(); _retrieveTranscoder = Substitute.For <ITranscoder>(); _dicomFrameHandler = Substitute.For <IFrameHandler>(); _retrieveTransferSyntaxHandler = new RetrieveTransferSyntaxHandler(NullLogger <RetrieveTransferSyntaxHandler> .Instance); _logger = NullLogger <RetrieveResourceService> .Instance; _recyclableMemoryStreamManager = new RecyclableMemoryStreamManager(); _retrieveResourceService = new RetrieveResourceService( _instanceStore, _fileStore, _retrieveTranscoder, _dicomFrameHandler, _retrieveTransferSyntaxHandler, _logger); }
public RetrieveMetadataServiceTests(DataStoreTestsFixture storagefixture) { EnsureArg.IsNotNull(storagefixture, nameof(storagefixture)); _instanceStore = Substitute.For <IInstanceStore>(); _metadataStore = storagefixture.MetadataStore; _eTagGenerator = Substitute.For <IETagGenerator>(); _dicomRequestContextAccessor = Substitute.For <IDicomRequestContextAccessor>(); _dicomRequestContextAccessor.RequestContext.DataPartitionEntry = new PartitionEntry(DefaultPartition.Key, DefaultPartition.Name); _retrieveMetadataService = new RetrieveMetadataService(_instanceStore, _metadataStore, _eTagGenerator, _dicomRequestContextAccessor); }
public RetrieveResourceServiceTests(DataStoreTestsFixture blobStorageFixture, SqlDataStoreTestsFixture sqlIndexStorageFixture) { _indexDataStore = sqlIndexStorageFixture.IndexDataStore; _instanceStore = sqlIndexStorageFixture.InstanceStore; _fileStore = blobStorageFixture.FileStore; _retrieveTranscoder = Substitute.For <ITranscoder>(); _frameHandler = Substitute.For <IFrameHandler>(); _retrieveTransferSyntaxHandler = new RetrieveTransferSyntaxHandler(NullLogger <RetrieveTransferSyntaxHandler> .Instance); _recyclableMemoryStreamManager = blobStorageFixture.RecyclableMemoryStreamManager; _retrieveResourceService = new RetrieveResourceService( _instanceStore, _fileStore, _retrieveTranscoder, _frameHandler, _retrieveTransferSyntaxHandler, blobStorageFixture.RecyclableMemoryStreamManager, NullLogger <RetrieveResourceService> .Instance); }
public ReindexDurableFunction( IExtendedQueryTagStore extendedQueryTagStore, IInstanceStore instanceStore, IInstanceReindexer instanceReindexer, ISchemaVersionResolver schemaVersionResolver, IOptions <QueryTagIndexingOptions> configOptions) { _extendedQueryTagStore = EnsureArg.IsNotNull(extendedQueryTagStore, nameof(extendedQueryTagStore)); _instanceStore = EnsureArg.IsNotNull(instanceStore, nameof(instanceStore)); _instanceReindexer = EnsureArg.IsNotNull(instanceReindexer, nameof(instanceReindexer)); _schemaVersionResolver = EnsureArg.IsNotNull(schemaVersionResolver, nameof(schemaVersionResolver)); _options = EnsureArg.IsNotNull(configOptions?.Value, nameof(configOptions)); }
public RetrieveMetadataService( IInstanceStore instanceStore, IMetadataStore metadataStore, IETagGenerator eTagGenerator) { EnsureArg.IsNotNull(instanceStore, nameof(instanceStore)); EnsureArg.IsNotNull(metadataStore, nameof(metadataStore)); EnsureArg.IsNotNull(eTagGenerator, nameof(eTagGenerator)); _instanceStore = instanceStore; _metadataStore = metadataStore; _eTagGenerator = eTagGenerator; }
object GetInstance(Registration registration, IInstanceStore tempInstanceStore, Stack <Type> buildStack) { if (buildStack.Contains(registration.ConcreteType)) { throw new ContainerException("Cyclic dependency detected when trying to construct `" + registration.ConcreteType.AssemblyQualifiedName + "`", buildStack); } buildStack.Push(registration.ConcreteType); var constructor = registration.Ctor ?? (container => { var constructors = registration.ConcreteType.GetConstructors(); var ctorsWithParams = constructors.Select(c => new { ctor = c, parameters = c.GetParameters() }); var orderedEnumerable = ctorsWithParams.OrderBy(x => x.parameters.Length); foreach (var ctor in orderedEnumerable) { var parameterInfos = ctor.parameters.Select(p => p.ParameterType); this.CheckDependencies(registration.ConcreteType, parameterInfos, registration.Lifecycle, tempInstanceStore, buildStack); var parameters = new object[ctor.parameters.Length]; for (var i = 0; i < ctor.parameters.Length; i++) { var newBuildStack = new Stack <Type>(buildStack.Reverse()); if (ctor.parameters[i].ParameterType.IsGenericType && ctor.parameters[i].ParameterType.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { var genericArgument = ctor.parameters[i].ParameterType.GetGenericArguments()[0]; parameters[i] = this.ResolveAll(genericArgument, newBuildStack); } else { parameters[i] = this.Resolve(ctor.parameters[i].ParameterType, tempInstanceStore, newBuildStack); } } try { return(ctor.ctor.Invoke(parameters)); } catch (Exception e) { throw new ContainerException("Cannot create type `" + ctor.ctor.DeclaringType.FullName + "`", buildStack, e); } } throw new ContainerException("Unable to construct `" + registration.ConcreteType.AssemblyQualifiedName + "`", buildStack); }); return(constructor(this)); }
public RetrieveResourceServiceTests() { _instanceStore = Substitute.For <IInstanceStore>(); _fileStore = Substitute.For <IFileStore>(); _retrieveTranscoder = Substitute.For <ITranscoder>(); _dicomFrameHandler = Substitute.For <IFrameHandler>(); _retrieveTransferSyntaxHandler = new RetrieveTransferSyntaxHandler(NullLogger <RetrieveTransferSyntaxHandler> .Instance); _logger = NullLogger <RetrieveResourceService> .Instance; _recyclableMemoryStreamManager = new RecyclableMemoryStreamManager(); _dicomRequestContextAccessor = Substitute.For <IDicomRequestContextAccessor>(); _dicomRequestContextAccessor.RequestContext.DataPartitionEntry = new PartitionEntry(DefaultPartition.Key, DefaultPartition.Name); _retrieveResourceService = new RetrieveResourceService( _instanceStore, _fileStore, _retrieveTranscoder, _dicomFrameHandler, _retrieveTransferSyntaxHandler, _dicomRequestContextAccessor, _logger); }
public TenantEngine(ITenantConfiguration configuration, IInstanceStore instanceStore) { if (configuration == null) { throw new ArgumentNullException("configuration"); } if (instanceStore == null) { throw new ArgumentNullException("instanceStore"); } this.configuration = configuration; this.runningInstances = instanceStore; }
public RetrieveMetadataService( IInstanceStore instanceStore, IMetadataStore metadataStore, IETagGenerator eTagGenerator, IDicomRequestContextAccessor contextAccessor) { EnsureArg.IsNotNull(instanceStore, nameof(instanceStore)); EnsureArg.IsNotNull(metadataStore, nameof(metadataStore)); EnsureArg.IsNotNull(eTagGenerator, nameof(eTagGenerator)); EnsureArg.IsNotNull(contextAccessor, nameof(contextAccessor)); _instanceStore = instanceStore; _metadataStore = metadataStore; _eTagGenerator = eTagGenerator; _contextAccessor = contextAccessor; }
internal Container(Container container) { //todo: replace lists also this.registeredTypes = new Dictionary<Type, IList<Registration>>(container.registeredTypes); this.singletonInstanceStore = new SingletonInstanceStore(container.singletonInstanceStore); this.httpContextOrThreadLocalStore = new HttpContextOrThreadLocalInstanceStore(container.httpContextOrThreadLocalStore); this.httpContextOrExecutionContextLocalStore = new HttpContextOrExecutionContextLocalInstanceStore(container.httpContextOrExecutionContextLocalStore); this.mutex = new object(); // remove old container this.RemoveAllRegistrationsAndInstancesOf<IContainer>(); // the contain can resolve itself); this.Inject<IContainer>(this); }
/// <summary> /// Trys to get an instance from the registered lifecycle store, creating it if it dosent exist /// </summary> /// <param name="type"></param> /// <param name="lifecycle"></param> /// <param name="tempInstanceStore"></param> /// <param name="buildStack"></param> /// <returns></returns> IEnumerable GetOrCreateInstances(Type type, Lifecycle lifecycle, IInstanceStore tempInstanceStore, Stack <Type> buildStack) { switch (lifecycle) { case Lifecycle.Singleton: lock (this.singletonInstanceStore.Mutex) return(this.GetOrCreateInstances(type, this.singletonInstanceStore, tempInstanceStore, buildStack)); case Lifecycle.HttpContextOrExecutionContextLocal: lock (this.httpContextOrExecutionContextLocalStore.Mutex) return(this.GetOrCreateInstances(type, this.httpContextOrExecutionContextLocalStore, tempInstanceStore, buildStack)); default: var typesToCreate = this.GetTypesToCreate(type, buildStack); return(typesToCreate.Select(typeToCreate => this.GetInstance(typeToCreate, tempInstanceStore, buildStack)).ToArray()); } }
public ReindexDurableFunctionTests() { _extendedQueryTagStore = Substitute.For <IExtendedQueryTagStore>(); _instanceStore = Substitute.For <IInstanceStore>(); _instanceReindexer = Substitute.For <IInstanceReindexer>(); _schemaVersionResolver = Substitute.For <ISchemaVersionResolver>(); _options = new QueryTagIndexingOptions { ActivityRetryOptions = new RetryOptions(TimeSpan.FromSeconds(5), 10), }; _reindexDurableFunction = new ReindexDurableFunction( _extendedQueryTagStore, _instanceStore, _instanceReindexer, _schemaVersionResolver, Options.Create(_options)); }
public RetrieveResourceServiceTests(DataStoreTestsFixture blobStorageFixture, SqlDataStoreTestsFixture sqlIndexStorageFixture) { EnsureArg.IsNotNull(sqlIndexStorageFixture, nameof(sqlIndexStorageFixture)); EnsureArg.IsNotNull(blobStorageFixture, nameof(blobStorageFixture)); _instanceStore = sqlIndexStorageFixture.InstanceStore; _indexDataStore = sqlIndexStorageFixture.IndexDataStore; _fileStore = blobStorageFixture.FileStore; _retrieveTranscoder = Substitute.For <ITranscoder>(); _frameHandler = Substitute.For <IFrameHandler>(); _dicomRequestContextAccessor = Substitute.For <IDicomRequestContextAccessor>(); _dicomRequestContextAccessor.RequestContext.DataPartitionEntry = new PartitionEntry(DefaultPartition.Key, DefaultPartition.Name); _retrieveTransferSyntaxHandler = new RetrieveTransferSyntaxHandler(NullLogger <RetrieveTransferSyntaxHandler> .Instance); _recyclableMemoryStreamManager = blobStorageFixture.RecyclableMemoryStreamManager; _retrieveResourceService = new RetrieveResourceService( _instanceStore, _fileStore, _retrieveTranscoder, _frameHandler, _retrieveTransferSyntaxHandler, _dicomRequestContextAccessor, NullLogger <RetrieveResourceService> .Instance); }
public HttpContextOrExecutionContextLocalInstanceStore(IInstanceStore instanceStore) : this() { if (!(instanceStore is HttpContextOrExecutionContextLocalInstanceStore)) throw new ArgumentException("executionContextInstanceStore is not a `" + typeof(HttpContextOrExecutionContextLocalInstanceStore).FullName + "`"); var executionContextInstanceStore = (HttpContextOrExecutionContextLocalInstanceStore)instanceStore; if (HttpContext.Current != null) { // todo: replace ILists with new lists HttpContext.Current.Items["__NanoIoC_InstanceStore_" + this.id] = new Dictionary<Type, IList<Tuple<Registration, object>>>(executionContextInstanceStore.Store); HttpContext.Current.Items["__NanoIoC_InjectedRegistrations_" + this.id] = new Dictionary<Type, IList<Registration>>(executionContextInstanceStore.InjectedRegistrations); } else { this.registrationStore.Value = new Dictionary<Type, IList<Tuple<Registration, object>>>(executionContextInstanceStore.Store); this.injectedRegistrations.Value = new Dictionary<Type, IList<Registration>>(executionContextInstanceStore.InjectedRegistrations); } }
public SaasKitEngine(SaasKitConfiguration configuration, IInstanceStore instanceStore) { if (configuration == null) { throw new ArgumentNullException("configuration"); } if (instanceStore == null) { throw new ArgumentNullException("instanceStore"); } if (!configuration.IsValid) { throw new InvalidOperationException("Configuration is invalid."); } this.configuration = configuration; this.runningInstances = instanceStore; }
public HttpContextOrThreadLocalInstanceStore(IInstanceStore instanceStore) : this() { // meh if(!(instanceStore is HttpContextOrThreadLocalInstanceStore)) throw new ArgumentException("httpContextOrThreadLocalStore is not a `" + typeof(HttpContextOrThreadLocalInstanceStore).FullName + "`"); var httpContextOrThreadLocalInstanceStore = instanceStore as HttpContextOrThreadLocalInstanceStore; if(HttpContext.Current != null) { // todo: replace ILists with new lists HttpContext.Current.Items["__NanoIoC_InstanceStore_" + this.id] = new Dictionary<Type, IList<Tuple<Registration, object>>>(HttpContext.Current.Items["__NanoIoC_InstanceStore_" + httpContextOrThreadLocalInstanceStore.id] as IDictionary<Type, IList<Tuple<Registration, object>>>); HttpContext.Current.Items["__NanoIoC_InjectedRegistrations_" + this.id] = new Dictionary<Type, IList<Registration>>(HttpContext.Current.Items["__NanoIoC_InjectedRegistrations_" + httpContextOrThreadLocalInstanceStore.id] as IDictionary<Type, IList<Registration>>); } else { this.threadStore.Value = new Dictionary<Type, IList<Tuple<Registration, object>>>(httpContextOrThreadLocalInstanceStore.threadStore.Value); this.injectedRegistrations.Value = new Dictionary<Type, IList<Registration>>(httpContextOrThreadLocalInstanceStore.injectedRegistrations.Value); } }
object Resolve(Type type, IInstanceStore tempInstanceStore, Stack <Type> buildStack) { if (tempInstanceStore != null && tempInstanceStore.ContainsInstancesFor(type)) { return(tempInstanceStore.GetInstances(type).Cast <Tuple <Registration, object> >().First().Item2); } var registrations = this.GetRegistrationsFor(type, null).ToList(); if (registrations.Count > 1) { throw new ContainerException("Cannot return single instance for type `" + type.AssemblyQualifiedName + "`, There are multiple instances stored.", buildStack); } if (registrations.Count == 1) { return(this.GetOrCreateInstances(type, registrations[0].Lifecycle, tempInstanceStore, buildStack).First()); } var typesToCreate = this.GetTypesToCreate(type, buildStack); return(this.GetInstance(typesToCreate.First(), tempInstanceStore, buildStack)); }
public FilePersistenceProvider(Guid id, string fileName) : base(id) { m_InstanceStore = new FileInstanceStore <Guid, object>(fileName); }
bool CanCreateDependency(Type dependeeType, Type requestedType, Lifecycle lifecycle, IInstanceStore tempInstanceStore, bool allowMultiple, Stack<Type> buildStack) { var registrations = this.GetRegistrationsFor(requestedType, tempInstanceStore).ToList(); if (registrations.Any()) { if (!allowMultiple && registrations.Count > 1) throw new ContainerException("Cannot create dependency `" + requestedType.AssemblyQualifiedName + "`, there are multiple concrete types registered for it.", buildStack); if (registrations[0].Lifecycle < lifecycle) throw new ContainerException("Cannot create dependency `" + requestedType.AssemblyQualifiedName + "`. It's lifecycle (" + registrations[0].Lifecycle + ") is shorter than the dependee's `" + dependeeType.AssemblyQualifiedName + "` (" + lifecycle + ")", buildStack); return true; } if (requestedType.IsGenericType) { var genericTypeDefinition = requestedType.GetGenericTypeDefinition(); return this.HasRegistrationsFor(genericTypeDefinition); } return false; }
void CheckDependencies(Type dependeeType, IEnumerable<Type> parameters, Lifecycle lifecycle, IInstanceStore tempInstanceStore, Stack<Type> buildStack) { parameters.All(p => { if (CanCreateDependency(dependeeType, p, lifecycle, tempInstanceStore, false, buildStack)) return true; if (p.IsGenericType && p.GetGenericTypeDefinition() == typeof (IEnumerable<>)) return true; if (!p.IsAbstract && !p.IsInterface) return true; throw new ContainerException("Cannot create dependency `" + p.AssemblyQualifiedName + "` of dependee `" + dependeeType.AssemblyQualifiedName + "`", buildStack); }); }
public MemoryProvider(Guid id, IInstanceStore <Guid, object> instanceStore) : base(id) { m_InstanceStore = instanceStore; }
public MemoryProvider(Guid id,IInstanceStore<Guid,object> instanceStore) : base(id) { m_InstanceStore = instanceStore; }
/// <summary> /// Trys to get an instance from the instance store, creating it if it doesnt exist /// </summary> /// <param name="requestType">The requested type</param> /// <param name="instanceStore"></param> /// <param name="tempInstanceStore"></param> /// <param name="buildStack"></param> /// <returns></returns> IEnumerable GetOrCreateInstances(Type requestType, IInstanceStore instanceStore, IInstanceStore tempInstanceStore, Stack<Type> buildStack) { var typesToCreate = GetTypesToCreate(requestType, buildStack); var instances = new List<Tuple<Registration, object>>(); if (tempInstanceStore != null && tempInstanceStore.ContainsInstancesFor(requestType)) instances.AddRange(tempInstanceStore.GetInstances(requestType).Cast<Tuple<Registration, object>>()); else if (instanceStore.ContainsInstancesFor(requestType)) instances.AddRange(instanceStore.GetInstances(requestType).Cast<Tuple<Registration, object>>()); foreach (var registration in typesToCreate) { if(!instances.Any(i => i != null && i.Item1 == registration)) { var newinstance = this.GetInstance(registration, tempInstanceStore, new Stack<Type>(buildStack.Reverse())); instanceStore.Insert(registration, requestType, newinstance); instances.Add(new Tuple<Registration, object>(registration, newinstance)); } } return instances.Select(i => i.Item2).ToArray(); }
IEnumerable<Registration> GetRegistrationsFor(Type type, IInstanceStore tempInstanceStore) { lock (this.mutex) { var registrations = new List<Registration>(); // use temp instance store first if (tempInstanceStore != null && tempInstanceStore.InjectedRegistrations.ContainsKey(type)) registrations.AddRange(tempInstanceStore.InjectedRegistrations[type]); // only add registrations if there are no override injections if ((!this.singletonInstanceStore.InjectedRegistrations.ContainsKey(type) || this.singletonInstanceStore.InjectedRegistrations[type].All(r => r.InjectionBehaviour != InjectionBehaviour.Override)) && (!this.httpContextOrThreadLocalStore.InjectedRegistrations.ContainsKey(type) || this.httpContextOrThreadLocalStore.InjectedRegistrations[type].All(r => r.InjectionBehaviour != InjectionBehaviour.Override)) && (!this.httpContextOrExecutionContextLocalStore.InjectedRegistrations.ContainsKey(type) || this.httpContextOrExecutionContextLocalStore.InjectedRegistrations[type].All(r => r.InjectionBehaviour != InjectionBehaviour.Override))) { if (this.registeredTypes.ContainsKey(type)) registrations.AddRange(this.registeredTypes[type]); } // add singleton injections if (this.singletonInstanceStore.InjectedRegistrations.ContainsKey(type)) { // if there are any overrides, return only them var overrideInjections = this.singletonInstanceStore.InjectedRegistrations[type].Where(r => r.InjectionBehaviour == InjectionBehaviour.Override).ToArray(); if(overrideInjections.Any()) registrations.AddRange(overrideInjections); else registrations.AddRange(this.singletonInstanceStore.InjectedRegistrations[type]); } // add httpcontextthreadlocal injections if (this.httpContextOrThreadLocalStore.InjectedRegistrations.ContainsKey(type)) { // if there are any overrides, return only them var overrideInjections = this.httpContextOrThreadLocalStore.InjectedRegistrations[type].Where(r => r.InjectionBehaviour == InjectionBehaviour.Override).ToArray(); if (overrideInjections.Any()) registrations.AddRange(overrideInjections); else registrations.AddRange(this.httpContextOrThreadLocalStore.InjectedRegistrations[type]); } // add httpcontextthreadlocal injections if (this.httpContextOrExecutionContextLocalStore.InjectedRegistrations.ContainsKey(type)) { // if there are any overrides, return only them var overrideInjections = this.httpContextOrExecutionContextLocalStore.InjectedRegistrations[type].Where(r => r.InjectionBehaviour == InjectionBehaviour.Override).ToArray(); if (overrideInjections.Any()) registrations.AddRange(overrideInjections); else registrations.AddRange(this.httpContextOrExecutionContextLocalStore.InjectedRegistrations[type]); } return registrations; } }
public TransactionalMemoryProvider(Guid id) : base(id) { m_InstanceStore = new TransactionalMemoryStore <Guid, object>(); }
object GetInstance(Registration registration, IInstanceStore tempInstanceStore, Stack<Type> buildStack) { if (buildStack.Contains(registration.ConcreteType)) throw new ContainerException("Cyclic dependency detected when trying to construct `" + registration.ConcreteType.AssemblyQualifiedName + "`", buildStack); buildStack.Push(registration.ConcreteType); var constructor = registration.Ctor ?? (container => { var constructors = registration.ConcreteType.GetConstructors(); var ctorsWithParams = constructors.Select(c => new {ctor = c, parameters = c.GetParameters()}); var orderedEnumerable = ctorsWithParams.OrderBy(x => x.parameters.Length); foreach (var ctor in orderedEnumerable) { var parameterInfos = ctor.parameters.Select(p => p.ParameterType); this.CheckDependencies(registration.ConcreteType, parameterInfos, registration.Lifecycle, tempInstanceStore, buildStack); var parameters = new object[ctor.parameters.Length]; for (var i = 0; i < ctor.parameters.Length; i++) { var newBuildStack = new Stack<Type>(buildStack.Reverse()); if (ctor.parameters[i].ParameterType.IsGenericType && ctor.parameters[i].ParameterType.GetGenericTypeDefinition() == typeof (IEnumerable<>)) { var genericArgument = ctor.parameters[i].ParameterType.GetGenericArguments()[0]; parameters[i] = this.ResolveAll(genericArgument, newBuildStack); } else { parameters[i] = this.Resolve(ctor.parameters[i].ParameterType, tempInstanceStore, newBuildStack); } } try { return ctor.ctor.Invoke(parameters); } catch(Exception e) { throw new ContainerException("Cannot create type `" + ctor.ctor.DeclaringType.FullName + "`", buildStack, e); } } throw new ContainerException("Unable to construct `" + registration.ConcreteType.AssemblyQualifiedName + "`", buildStack); }); return constructor(this); }
public FilePersistenceProvider(Guid id,string fileName) : base(id) { m_InstanceStore = new FileInstanceStore<Guid,object>(fileName); }
object Resolve(Type type, IInstanceStore tempInstanceStore, Stack<Type> buildStack) { if (tempInstanceStore != null && tempInstanceStore.ContainsInstancesFor(type)) return tempInstanceStore.GetInstances(type).Cast<Tuple<Registration, object>>().First().Item2; lock (this.mutex) { // dont pass tempinstancestore in here, the If above handles it var registrations = this.GetRegistrationsFor(type, tempInstanceStore).ToList(); if(registrations.Count > 1) throw new ContainerException("Cannot return single instance for type `" + type.AssemblyQualifiedName + "`, There are multiple instances stored.", buildStack); if (registrations.Count == 1) return this.GetOrCreateInstances(type, registrations[0].Lifecycle, tempInstanceStore, buildStack).First(); var typesToCreate = GetTypesToCreate(type, buildStack); return this.GetInstance(typesToCreate.First(), tempInstanceStore, buildStack); } }
/// <summary> /// Trys to get an instance from the registered lifecycle store, creating it if it dosent exist /// </summary> /// <param name="type"></param> /// <param name="lifecycle"></param> /// <param name="tempInstanceStore"></param> /// <param name="buildStack"></param> /// <returns></returns> IEnumerable GetOrCreateInstances(Type type, Lifecycle lifecycle, IInstanceStore tempInstanceStore, Stack<Type> buildStack) { switch (lifecycle) { case Lifecycle.Singleton: return this.GetOrCreateInstances(type, this.singletonInstanceStore, tempInstanceStore, buildStack); case Lifecycle.HttpContextOrThreadLocal: return this.GetOrCreateInstances(type, this.httpContextOrThreadLocalStore, tempInstanceStore, buildStack); case Lifecycle.HttpContextOrExecutionContextLocal: return this.GetOrCreateInstances(type, this.httpContextOrExecutionContextLocalStore, tempInstanceStore, buildStack); default: var typesToCreate = GetTypesToCreate(type, buildStack); return typesToCreate.Select(typeToCreate => this.GetInstance(typeToCreate, tempInstanceStore, buildStack)).ToArray(); } }
public SingletonInstanceStore(IInstanceStore store) { // todo: replace ILists with new lists this.instanceStore = new Dictionary<Type, IList<Tuple<Registration, object>>>(store.Store); this.injectedRegistrations = new Dictionary<Type, IList<Registration>>(store.InjectedRegistrations); }