internal ObjectDI(ObjectDI parent) { _root = parent._root; _registries = _root._registries; _services = new ConcurrentDictionary <ServiceRegistry, object>(); _disposables = new ConcurrentBag <IDisposable>(); }
public ObjectDI() { _registries = new ConcurrentDictionary <Type, ServiceRegistry>(); _root = this; _services = new ConcurrentDictionary <ServiceRegistry, object>(); _disposables = new ConcurrentBag <IDisposable>(); }
public static ObjectDI Register <TServiceType>( this ObjectDI cat, Func <ObjectDI, TServiceType> factory, LifecCycle lifetime) { cat.Register(new ServiceRegistry( typeof(TServiceType), lifetime, (_, arguments) => factory(_))); return(cat); }
public static ObjectDI Register <TServiceType>( this ObjectDI cat, TServiceType instance) { Func <ObjectDI, Type[], object> factory = (_, arguments) => instance; cat.Register(new ServiceRegistry(typeof(TServiceType), LifecCycle.Single, factory)); return(cat); }
public static ObjectDI Register( this ObjectDI cat, Type from, Type to, LifecCycle lifetime) { Func <ObjectDI, Type[], object> factory = (_, arguments) => Create(_, to, arguments); cat.Register(new ServiceRegistry(from, lifetime, factory)); return(cat); }
private static object Create( ObjectDI cat, Type type, Type[] genericArguments) { if (genericArguments.Length > 0) { type = type.MakeGenericType(genericArguments); } var constructors = type .GetConstructors(); if (constructors.Length == 0) { throw new InvalidOperationException($"Cannot create the instance of {type} which does not have an public constructor."); } var constructor = constructors.FirstOrDefault( it => it.GetCustomAttributes(false) .OfType <InjectionAttribute>().Any()); constructor = constructor ?? constructors.First(); var parameters = constructor.GetParameters(); if (parameters.Length == 0) { return(Activator.CreateInstance(type)); } var arguments = new object[parameters.Length]; for (int index = 0; index < arguments.Length; index++) { var parameter = parameters[index]; var parameterType = parameter.ParameterType; if (cat.HasRegistry(parameterType)) { arguments[index] = cat.GetService(parameterType); } else if (parameter.HasDefaultValue) { arguments[index] = parameter.DefaultValue; } else { throw new InvalidOperationException($"Cannot create the instance of {type} whose constructor has non-registered parameter type(s)"); } } return(Activator.CreateInstance(type, arguments)); }
/// <summary> /// 私有构造 /// </summary> private StartService() { //ContainerBuilder containerBuilder = new ContainerBuilder(); //var typeFinder = new AppDomainTypeFinder(); //containerBuilder.RegisterInstance(typeFinder).As<ITypeFinder>().SingleInstance(); //Container = containerBuilder.Build(); ObjectDI objectDI = new ObjectDI(); var typeFinder = new AppDomainTypeFinder(); objectDI.Register <ITypeFinder>(typeFinder); var drTypes = typeFinder.FindClassesOfType <IDependencyManager>(); var drInstances = new List <IDependencyManager>(); foreach (var item in drTypes) { drInstances.Add((IDependencyManager)Activator.CreateInstance(item)); } drInstances = drInstances.AsQueryable().OrderBy(x => x.Order).ToList(); foreach (var item in drInstances) { item.Register(objectDI, typeFinder); } Container = objectDI; }
public static T GetService <T>(this ObjectDI cat) => (T)cat.GetService(typeof(T));
public static IEnumerable <T> GetServices <T>(this ObjectDI cat) => cat.GetService <IEnumerable <T> >();
public static bool HasRegistry(this ObjectDI cat, Type serviceType) => cat._root._registries.ContainsKey(serviceType);
public static bool HasRegistry <T>(this ObjectDI cat) => cat.HasRegistry(typeof(T));
public static ObjectDI Register <TFrom, TTo>( this ObjectDI cat, LifecCycle lifetime) where TTo : TFrom => cat.Register(typeof(TFrom), typeof(TTo), lifetime);