protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) { dependencies = new DependencyContainer(base.CreateChildDependencies(parent)); dependencies.CacheAs <IScrollingInfo>(scrollingInfo = new ScrollingInfo()); return(dependencies); }
public SkinSourceDependencyContainer(IReadOnlyDependencyContainer fallback) { this.fallback = fallback; }
/// <summary> /// Create a new DependencyContainer instance. /// </summary> /// <param name="parent">An optional parent container which we should use as a fallback for cache lookups.</param> public DependencyContainer(IReadOnlyDependencyContainer parent = null) { parentContainer = parent; }
protected sealed override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) { dependencies = new DelegatedDependencyContainer(base.CreateChildDependencies(parent)); return(dependencies); }
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) => DependencyActivator.MergeDependencies(shadowModel, base.CreateChildDependencies(parent), new CacheInfo(parent: typeof(TModel)));
/// <summary> /// Retrieves a cached dependency of type <typeparamref name="T"/> if it exists, and default(<typeparamref name="T"/>) otherwise. /// </summary> /// <typeparam name="T">The dependency type to query for.</typeparam> /// <param name="container">The <see cref="IReadOnlyDependencyContainer"/> to query.</param> /// <returns>The requested dependency, or default(<typeparamref name="T"/>) if not found.</returns> internal static T GetValue <T>(this IReadOnlyDependencyContainer container) => GetValue <T>(container, default);
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) => new CachedModelDependencyContainer <Room>(base.CreateChildDependencies(parent)) { Model = { BindTarget = room } };
private static void FillPamaterInjectionList(ParameterInfo[] parameterInfo, object[] parameters, IReadOnlyDependencyContainer container) { // Resolve the dependencies for the method parameters for (int i = 0; i < parameterInfo.Length; ++i) { ParameterInfo p = parameterInfo[i]; Type parameterType = p.ParameterType; parameters[i] = container.BindingExists(parameterType) ? container.GetBinding(parameterType).GetInstance() : (parameterType.GetTypeInfo().IsValueType ? Activator.CreateInstance(parameterType, true) : null); } }
/// <summary> /// Creates an instance of the target type and injects the newly create object. /// Note: a suitable constructor is necessary to create the instance. Either a constructor marked /// with the 'Inject' attribute, or an accessable default constructor. /// </summary> /// <param name="container">Container with resources to inject.</param> /// <typeparam name="T">The type of object to create an instance of.</typeparam> /// <returns>An instance of the target type, injected with resources found in the container.</returns> public static T CreateAndInject <T>(IReadOnlyDependencyContainer container) { return(CreateAndInject <T>(container, string.Empty)); }
/// <summary> /// Resolve all injectable properties across the type-chain. /// </summary> /// <param name="objToInject">The object for which to inject its properties.</param> /// <param name="currentType">The current type across the type chain.</param> /// <param name="container">The resource container.</param> /// <param name="injectionId">An identifier to restrict to specifically defined scopes.</param> private static void ResolvePropertyDependencies(object objToInject, Type currentType, IReadOnlyDependencyContainer container, string injectionId = null) { if (TryGetTypeInjectionInfo(currentType, out TypeInjectionCache cache)) { bool isStaticInjection = objToInject is Type; foreach (MemberInjectionValue <PropertyInfo> property in cache.InjectableProperties) { Type propertyType = property.Member.PropertyType; if (property.Attribute.IsInjectionIdDefined(injectionId) && container.BindingExists(propertyType)) { if (isStaticInjection && property.Member.SetMethod.IsStatic) { property.Member.SetValue(null, container.GetBinding(propertyType).GetInstance()); } else if (!isStaticInjection && !property.Member.SetMethod.IsStatic) { property.Member.SetValue(objToInject, container.GetBinding(propertyType).GetInstance()); } } } } }
/// <summary> /// Resolve all injectable methods across the type-chain. /// </summary> /// <param name="objToInject">The object for which to inject its methods.</param> /// <param name="currentType">The current type across the type chain.</param> /// <param name="container">The resource container.</param> /// <param name="injectionId">An identifier to restrict to specifically defined scopes.</param> private static void ResolveMethodDependencies(object objToInject, Type currentType, IReadOnlyDependencyContainer container, string injectionId = null) { if (TryGetTypeInjectionInfo(currentType, out TypeInjectionCache cache)) { bool isStaticInjection = objToInject is Type; foreach (MemberInjectionValue <MethodInfo> method in cache.InjectableMethods) { if (method.Attribute.IsInjectionIdDefined(injectionId)) { if (isStaticInjection && method.Member.IsStatic) { ParameterInfo[] parameterInfo = method.Member.GetParameters(); object[] parameters = TypeReflectionUtilities.GetParameterInvokationList(parameterInfo.Length); FillPamaterInjectionList(parameterInfo, parameters, container); method.Member.Invoke(null, parameters); TypeReflectionUtilities.ReturnParameterInvokationList(parameters); } else if (!isStaticInjection && !method.Member.IsStatic) { ParameterInfo[] parameterInfo = method.Member.GetParameters(); object[] parameters = TypeReflectionUtilities.GetParameterInvokationList(parameterInfo.Length); FillPamaterInjectionList(parameterInfo, parameters, container); method.Member.Invoke(objToInject, parameters); TypeReflectionUtilities.ReturnParameterInvokationList(parameters); } } } } }
/// <summary> /// Resolve all injectable fields across the type-chain. /// </summary> /// <param name="objToInject">The object for which to inject its fields.</param> /// <param name="currentType">The current type across the type chain.</param> /// <param name="container">The resource container.</param> /// <param name="injectionId">An identifier to restrict to specifically defined scopes.</param> private static void ResolveFieldDependencies(object objToInject, Type currentType, IReadOnlyDependencyContainer container, string injectionId = null) { if (TryGetTypeInjectionInfo(currentType, out TypeInjectionCache cache)) { bool isStaticInjection = objToInject is Type; foreach (MemberInjectionValue <FieldInfo> field in cache.InjectableFields) { Type fieldType = field.Member.FieldType; if (field.Attribute.IsInjectionIdDefined(injectionId) && container.BindingExists(fieldType)) { if (isStaticInjection && field.Member.IsStatic) { field.Member.SetValue(null, container.GetBinding(fieldType).GetInstance()); } else if (!isStaticInjection && !field.Member.IsStatic) { field.Member.SetValue(objToInject, container.GetBinding(fieldType).GetInstance()); } } } } }
public DependencyIsolationContainer(IReadOnlyDependencyContainer parentDependencies) { this.parentDependencies = parentDependencies; }
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) => new DependencyContainer(new DependencyIsolationContainer(base.CreateChildDependencies(parent)));
/// <summary> /// Retrieves a cached dependency of type <typeparamref name="T"/> if it exists, and null otherwise. /// </summary> /// <typeparam name="T">The dependency type to query for.</typeparam> /// <param name="container">The <see cref="IReadOnlyDependencyContainer"/> to query.</param> /// <returns>The requested dependency, or null if not found.</returns> public static T Get <T>(this IReadOnlyDependencyContainer container) where T : class => Get <T>(container, default);
/// <summary> /// Creates an instance of the target type and injects the newly create object. /// Note: a suitable constructor is necessary to create the instance. Either a constructor marked /// with the 'Inject' attribute, or an accessable default constructor. /// </summary> /// <param name="targetType">The type of object to create an instance of.</param> /// <param name="container">Container with resources to inject.</param> /// <returns>An instance of the target type, injected with resources found in the container.</returns> public static object CreateAndInject(Type targetType, IReadOnlyDependencyContainer container) { return(CreateAndInject(targetType, container, string.Empty)); }
/// <summary> /// Retrieves a cached dependency of type <typeparamref name="T"/> if it exists, and null otherwise. /// </summary> /// <typeparam name="T">The dependency type to query for.</typeparam> /// <param name="container">The <see cref="IReadOnlyDependencyContainer"/> to query.</param> /// <param name="info">Extra information that identifies the cached dependency.</param> /// <returns>The requested dependency, or null if not found.</returns> public static T Get <T>(this IReadOnlyDependencyContainer container, CacheInfo info) where T : class => (T)container.Get(typeof(T), info);
/// <summary> /// Creates an instance of the target type and injects the newly create object. /// Note: a suitable constructor is necessary to create the instance. Either a constructor marked /// with the 'Inject' attribute (with matching injection identifier), or an accessable default constructor. /// </summary> /// <param name="container">Container with resources to inject.</param> /// <param name="injectionId">Only injects members with the same injection identifier.</param> /// <typeparam name="T">The type of object to create an instance of.</typeparam> /// <returns>An instance of the target type, injected with resources found in the container.</returns> public static T CreateAndInject <T>(IReadOnlyDependencyContainer container, string injectionId) { return((T)CreateAndInject(typeof(T), container, injectionId)); }
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) { dependencies = new CachedModelDependencyContainer <Room>(base.CreateChildDependencies(parent)); dependencies.Model.BindTo(currentRoom); return(dependencies); }
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) => new DependencyContainer(donor?.Dependencies ?? base.CreateChildDependencies(parent));
public DelegatedDependencyContainer(IReadOnlyDependencyContainer parent) { this.parent = parent; injectableDependencies = new DependencyContainer(this); }
/// <summary> /// Injects dependencies from a <see cref="DependencyContainer"/> into an object. /// </summary> /// <param name="obj">The object to inject the dependencies into.</param> /// <param name="dependencies">The dependencies to use for injection.</param> public static void Activate(object obj, IReadOnlyDependencyContainer dependencies) => getActivator(obj.GetType()).activate(obj, dependencies);
internal void CreateLeasedDependencies(IReadOnlyDependencyContainer dependencies) => createDependencies(dependencies);
/// <summary> /// Merges existing dependencies with new dependencies from an object into a new <see cref="IReadOnlyDependencyContainer"/>. /// </summary> /// <param name="obj">The object whose dependencies should be merged into the dependencies provided by <paramref name="dependencies"/>.</param> /// <param name="dependencies">The existing dependencies.</param> /// <param name="info">Extra information to identify parameters of <paramref name="obj"/> in the cache with.</param> /// <returns>A new <see cref="IReadOnlyDependencyContainer"/> if <paramref name="obj"/> provides any dependencies, otherwise <paramref name="dependencies"/>.</returns> public static IReadOnlyDependencyContainer MergeDependencies(object obj, IReadOnlyDependencyContainer dependencies, CacheInfo info = default) => getActivator(obj.GetType()).mergeDependencies(obj, dependencies, info);
protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent) => dependencies = new DependencyContainer(parent);
/// <summary> /// Retrieves a cached dependency of type <typeparamref name="T"/> if it exists, and null otherwise. /// </summary> /// <typeparam name="T">The dependency type to query for.</typeparam> /// <param name="container">The <see cref="IReadOnlyDependencyContainer"/> to query.</param> /// <returns>The requested dependency, or null if not found.</returns> public static T Get <T>(this IReadOnlyDependencyContainer container) where T : class => (T)container.Get(typeof(T));
protected sealed override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) => new DependencyContainer(dependencies = new SkinSourceDependencyContainer(base.CreateChildDependencies(parent)));
/// <summary> /// Merges existing dependencies with new dependencies from an object into a new <see cref="IReadOnlyDependencyContainer"/>. /// </summary> /// <param name="obj">The object whose dependencies should be merged into the dependencies provided by <paramref name="dependencies"/>.</param> /// <param name="dependencies">The existing dependencies.</param> /// <returns>A new <see cref="IReadOnlyDependencyContainer"/> if <paramref name="obj"/> provides any dependencies, otherwise <paramref name="dependencies"/>.</returns> public static IReadOnlyDependencyContainer MergeDependencies(object obj, IReadOnlyDependencyContainer dependencies) => getActivator(obj.GetType()).mergeDependencies(obj, dependencies);
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) => dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
/// <summary> /// Tries to retrieve a cached dependency of type <typeparamref name="T"/>. /// </summary> /// <param name="container">The <see cref="IReadOnlyDependencyContainer"/> to query.</param> /// <param name="value">The requested dependency, or null if not found.</param> /// <typeparam name="T">The dependency type to query for.</typeparam> /// <returns>Whether the requested dependency existed.</returns> public static bool TryGet <T>(this IReadOnlyDependencyContainer container, out T value) where T : class { value = container.Get <T>(); return(value != null); }