Пример #1
0
 protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
 {
     dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
     dependencies.CacheAs <IScrollingInfo>(scrollingInfo = new ScrollingInfo());
     return(dependencies);
 }
Пример #2
0
 public SkinSourceDependencyContainer(IReadOnlyDependencyContainer fallback)
 {
     this.fallback = fallback;
 }
Пример #3
0
 /// <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;
 }
Пример #4
0
 protected sealed override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
 {
     dependencies = new DelegatedDependencyContainer(base.CreateChildDependencies(parent));
     return(dependencies);
 }
Пример #5
0
 protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
 => DependencyActivator.MergeDependencies(shadowModel, base.CreateChildDependencies(parent), new CacheInfo(parent: typeof(TModel)));
Пример #6
0
 /// <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);
Пример #7
0
 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());
                        }
                    }
                }
            }
        }
Пример #13
0
 public DependencyIsolationContainer(IReadOnlyDependencyContainer parentDependencies)
 {
     this.parentDependencies = parentDependencies;
 }
Пример #14
0
 protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) =>
 new DependencyContainer(new DependencyIsolationContainer(base.CreateChildDependencies(parent)));
Пример #15
0
 /// <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));
 }
Пример #17
0
 /// <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));
 }
Пример #19
0
 protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
 {
     dependencies = new CachedModelDependencyContainer <Room>(base.CreateChildDependencies(parent));
     dependencies.Model.BindTo(currentRoom);
     return(dependencies);
 }
Пример #20
0
 protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) =>
 new DependencyContainer(donor?.Dependencies ?? base.CreateChildDependencies(parent));
Пример #21
0
 public DelegatedDependencyContainer(IReadOnlyDependencyContainer parent)
 {
     this.parent            = parent;
     injectableDependencies = new DependencyContainer(this);
 }
Пример #22
0
 /// <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);
Пример #23
0
 internal void CreateLeasedDependencies(IReadOnlyDependencyContainer dependencies) => createDependencies(dependencies);
Пример #24
0
 /// <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);
Пример #25
0
 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));
Пример #27
0
 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);
Пример #29
0
 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);
 }