private object GetOrCreateInstance(ConfiguratedType configuratedType) { if (configuratedType != null) { if (configuratedType.IsSingleton) { if (configuratedType.Instance == null) { lock (syncRoot) { if (configuratedType.Instance == null) { configuratedType.Instance = Create(configuratedType.ImplementationInterface); } } } return(configuratedType.Instance); } return(Create(configuratedType.ImplementationInterface)); } else { return(null); } }
private object RetrieveInst(ConfiguratedType registeredType) { if (registeredType != null) { if (registeredType.IsSingleton) { if (registeredType.GetInstance == null) { lock (obj) { if (registeredType.GetInstance == null) { registeredType.GetInstance = Create(registeredType.GetImplementationInterface); } } } return(registeredType.GetInstance); } object createdInst = Create(registeredType.GetImplementationInterface); return(createdInst); } throw new Exception("can't resolve this type"); }
private void RegisterType(Type tInterface, Type tImplementation, bool isSingleton = false) { if (!tImplementation.IsInterface && !tImplementation.IsAbstract && tInterface.IsAssignableFrom(tImplementation)) { ConfiguratedType configuratedType = new ConfiguratedType(tImplementation, tInterface, isSingleton); if (!dictionary.TryGetValue(tInterface, out var list)) { dictionary.Add(tInterface, new List <ConfiguratedType>() { configuratedType }); } else { if (!list.Contains(configuratedType)) { list.Add(configuratedType); } else { throw new Exception($"can't add value {nameof(tInterface)} to {nameof(dictionary)}"); } } } else { throw new Exception($"{nameof(tImplementation)} can't be added with {nameof(tInterface)}"); } }
public void RegisterType(Type TInterface, Type TImplementation, bool isSingleton = false) { if (!TImplementation.IsInterface && !TImplementation.IsAbstract && TInterface.IsAssignableFrom(TImplementation)) { ConfiguratedType configuratedType = new ConfiguratedType(TInterface, TImplementation, isSingleton); if (_configuration.ContainsKey(TInterface)) { _configuration[TInterface].Add(configuratedType); } else { _configuration.Add(TInterface, new List <ConfiguratedType> { configuratedType }); } } else { throw new Exception($"{TImplementation.ToString()} can't be added with {TInterface.ToString()}"); } }