internal object GetInstance(MappingKey key) { Lazy <object> createScopedInstance; if (scopedMappings.TryGetValue(key, out createScopedInstance)) { return(createScopedInstance.Value); } else { if (String.IsNullOrEmpty(key.InstanceName)) { var scopedKey = new MappingKey(key.Type, Scope.GetCurrentScope().ScopeType); if (scopedMappings.TryGetValue(scopedKey, out createScopedInstance)) { return(createScopedInstance.Value); } else { // If we do cannot create an instance there is a possibility the requested key to be registered in a parent scope. // If that is the case the instance is created within the correct scope without switching the current scope. Scope scopeContainingMappingKey; MappingKey keyToResolveFromFoundScope; if (ScopeHolder.TryFindWithinParents(scopedKey, ScopeHolder.CurrentScope, out scopeContainingMappingKey, out keyToResolveFromFoundScope)) { return(scopeContainingMappingKey.GetInstance(keyToResolveFromFoundScope)); } } } } return(null); }
private void Guard_AlreadyRegistered(MappingKey key) { if (singletonMappings.ContainsKey(key) || transientMappings.ContainsKey(key) || scopedMappings.ContainsKey(key)) { const string errorMessageFormat = "The requested mapping already exists - {0}"; throw new InvalidOperationException(string.Format(errorMessageFormat, key.ToTraceString())); } }
/// <summary> /// Check if a particular type/instance name has been registered with the container /// </summary> /// <param name="type">Type to check registration for</param> /// <param name="instanceName">Instance name (optional)</param> /// <returns><c>true</c>if the type/instance name has been registered /// with the container; otherwise <c>false</c></returns> public bool IsRegistered(Type type, string instanceName = null) { if (type == null) { throw new ArgumentNullException("type"); } var key = new MappingKey(type, instanceName); return(transientMappings.ContainsKey(key) || singletonMappings.ContainsKey(key) || scopedMappings.ContainsKey(key)); }
/// <summary> /// Resolve an instance of the requested type from the container. /// </summary> /// <param name="type">Requested type</param> /// <param name="instanceName">Instance name (optional)</param> /// <returns>The retrieved object</returns> public object Resolve(Type type, string instanceName = null) { var key = new MappingKey(type, instanceName); var instance = GetInstance(key); if (instance != null) { return(instance); } else { const string errorMessageFormat = "Could not find mapping for type '{0}'"; throw new InvalidOperationException(string.Format(errorMessageFormat, type.FullName)); } }
private void AddMapping(Type type, MappingKey key) { List <MappingKey> mapsForCurrentType; if (mappings.TryGetValue(type, out mapsForCurrentType)) { mapsForCurrentType.Add(key); } else { mappings.Add(type, new List <MappingKey>() { key }); } }
public void RegisterScoped(Type type, Func <object> createInstanceDelegate, string scopeType = null) { if (type == null) { throw new ArgumentNullException("type"); } if (createInstanceDelegate == null) { throw new ArgumentNullException("createInstanceDelegate"); } var key = new MappingKey(type, scopeType); Guard_AlreadyRegistered(key); scopedMappings.Add(key, createInstanceDelegate); }
/// <summary> /// Register a type with singleton life style /// </summary> /// <param name="type">Type that will be requested</param> /// <param name="createInstanceDelegate">A delegate that will be used to /// create an instance of the requested object</param> /// <param name="instanceName">Instance name (optional)</param> public void RegisterSingleton(Type type, Func <object> createInstanceDelegate, string instanceName = null) { if (type == null) { throw new ArgumentNullException("type"); } if (createInstanceDelegate == null) { throw new ArgumentNullException("createInstanceDelegate"); } var key = new MappingKey(type, instanceName); Guard_AlreadyRegistered(key); singletonMappings.Add(key, new Lazy <object>(createInstanceDelegate, true)); AddMapping(type, key); }
/// <summary> /// Register a type with transient life style /// </summary> /// <param name="type">Type that will be requested</param> /// <param name="createInstanceDelegate">A delegate that will be used to /// create an instance of the requested object</param> /// <param name="instanceName">Instance name (optional)</param> public void RegisterTransient(Type type, Func <object> createInstanceDelegate, string instanceName = null) { if (type == null) { throw new ArgumentNullException("type"); } if (createInstanceDelegate == null) { throw new ArgumentNullException("createInstanceDelegate"); } var key = new MappingKey(type, instanceName); Guard_AlreadyRegistered(key); transientMappings.Add(key, createInstanceDelegate); AddMapping(type, key); }
private object GetInstance(MappingKey key) { object instance = null; Lazy <object> createSingletonInstance; Func <object> createTransientInstance; if (Scope.IsInScope) { instance = Scope.GetCurrentScope().GetInstance(key); } if (instance == null && singletonMappings.TryGetValue(key, out createSingletonInstance)) { instance = createSingletonInstance.Value; } else if (instance == null && transientMappings.TryGetValue(key, out createTransientInstance)) { instance = createTransientInstance(); } return(instance); }
public static bool TryFindWithinParents(MappingKey key, Scope startingScope, out Scope scopeContainingMappingKey, out MappingKey keyToResolveFromFoundScope) { scopeContainingMappingKey = null; keyToResolveFromFoundScope = key; Scope scopeToTest = startingScope; while (!scopeToTest.IsRoot) { if (scopes.TryGetValue(scopeToTest.ParentScopeId, out scopeToTest)) { keyToResolveFromFoundScope = new MappingKey(key.Type, scopeToTest.ScopeType); if (scopeToTest.scopedMappings.ContainsKey(keyToResolveFromFoundScope)) { scopeContainingMappingKey = scopeToTest; return(true); } } else { break; } } return(false); }