Пример #1
0
 public IProvideInstance GetProviderForPlugin(Type requested, IDependencyDefinition definition, IDependencyAdapter container)
 {
     return(Providers
            .ReadOrWrite(
                Tuple.Create(requested, definition.ConcreteType, definition.PluginName ?? ""),
                () => CreateProvider(requested, definition, container)));
 }
Пример #2
0
        public bool TryGet(Type requiredType, out IDependencyDefinition definition)
        {
            Ensure.NotNull(requiredType, "requiredType");

            DependencyDefinition result;

            if (definitionByRequiredType.TryGetValue(requiredType, out result))
            {
                definition = result.Clone(isResolvable(requiredType));
                return(true);
            }

            if (parentCollection != null)
            {
                if (parentCollection.TryGet(requiredType, out definition))
                {
                    if (!definition.IsResolvable && isResolvable(requiredType))
                    {
                        definition = new DependencyDefinition(definition.RequiredType, definition.Lifetime, definition.Target, true);
                    }

                    return(true);
                }
            }

            definition = null;
            return(false);
        }
 public void EliminateDuplicateDependencies(IDependencyDefinition newDependency, List <IDependencyDefinition> dependencies, Type requestedType)
 {
     if (newDependency.IsAdd)
     {
         dependencies.RemoveAll(d =>
         {
             var remove = false;
             if (d.PluginName == newDependency.PluginName && d.ConcreteType == newDependency.ConcreteType)
             {
                 Providers.RemoveProvider(requestedType, d);
                 remove = true;
             }
             return(remove);
         });
     }
     else
     {
         dependencies.RemoveAll(d =>
         {
             var remove = false;
             if (d.PluginName == newDependency.PluginName)
             {
                 Providers.RemoveProvider(requestedType, d);
                 remove = true;
             }
             return(remove);
         });
     }
 }
Пример #4
0
 public bool IsDuplicate(IDependencyDefinition dependency)
 {
     return(GetTypesRegisteredFor(dependency.PluginType)
            .Any(x => ReferenceEquals(x, dependency.ConcreteInstance == null
                                            ? dependency.ConcreteType
                                            : dependency.ConcreteInstance.GetType())));
 }
Пример #5
0
        private void HandleAdd(IDependencyDefinition dependency)
        {
            var isDuplicate = IsDuplicate(dependency);

            ObjectFactory
            .Configure(x =>
            {
                if (dependency.IsSingleton && !isDuplicate)
                {
                    var singleton = x.For(dependency.PluginType).Singleton();
                    if (dependency.HasSingleton)
                    {
                        singleton.Add(dependency.ConcreteInstance);
                    }
                    else
                    {
                        singleton.Add(dependency.ConcreteType);
                    }
                }
                else if (dependency.IsNamed)
                {
                    x.For(dependency.PluginType).Add(dependency.ConcreteType).Named(
                        dependency.PluginName);
                }
                else if (!isDuplicate)
                {
                    x.For(dependency.PluginType).Add(dependency.ConcreteType);
                }
            });
        }
 /// <summary>
 /// Adds a dependency to the Bindable LINQ query given a dependency definition. This allows developers to create custom
 /// dependency types by implementing the <see cref="IDependencyDefinition"/> interface.
 /// </summary>
 /// <typeparam name="TResult">The type of the result.</typeparam>
 /// <param name="query">The query.</param>
 /// <param name="definition">The definition.</param>
 /// <returns></returns>
 public static TResult DependsOn <TResult>(this TResult query, IDependencyDefinition definition) where TResult : IAcceptsDependencies
 {
     if (query != null && definition != null)
     {
         query.AcceptDependency(definition);
     }
     return(query);
 }
Пример #7
0
 /// <summary>
 /// Sets a new dependency on a Bindable LINQ operation.
 /// </summary>
 /// <param name="definition">A definition of the dependency.</param>
 public void AcceptDependency(IDependencyDefinition definition)
 {
     if (definition.AppliesToCollections())
     {
         var dependency = definition.ConstructForCollection(_sourceCollection, BindingConfigurations.Default.CreatePathNavigator());
         dependency.SetReevaluateCallback(o => Refresh());
         _dependencies.Add(dependency);
     }
 }
Пример #8
0
        protected override IDependencyDefinition ExtractFromRoot(Expression rootExpression, string propertyPath)
        {
            IDependencyDefinition result = null;

            if (rootExpression is ParameterExpression parameterExpression)
            {
                result = new ItemDependencyDefinition(propertyPath, parameterExpression.Name);
            }
            return(result);
        }
Пример #9
0
        public Func <object> BuildDelegateCall(Type requested, IDependencyDefinition definition)
        {
            var context = new RequestContext();

            if (definition.PluginType.IsOpenGeneric())
            {
                context.TypeArguments = requested.GetGenericArguments();
            }

            return(() => definition.CreatorDelegate.DynamicInvoke(context));
        }
Пример #10
0
 public void Register(IDependencyDefinition dependency)
 {
     if (dependency.IsAdd)
     {
         HandleAdd(dependency);
     }
     else
     {
         HandleFor(dependency);
     }
 }
        public bool TryGet(Type requiredType, out IDependencyDefinition definition)
        {
            DependencyDefinition result;

            if (TryGetInternal(requiredType, out result))
            {
                definition = result;
                return(true);
            }

            definition = null;
            return(false);
        }
Пример #12
0
        /// <summary>
        /// When overridden in a derived class, extracts the appropriate dependency from the root of the expression.
        /// </summary>
        /// <param name="rootExpression">The root expression.</param>
        /// <param name="propertyPath">The property path.</param>
        /// <returns></returns>
        protected override IDependencyDefinition ExtractFromRoot(Expression rootExpression, string propertyPath)
        {
            IDependencyDefinition result = null;

            if (rootExpression is ConstantExpression)
            {
                var constantExpression = (ConstantExpression)rootExpression;
                if (propertyPath != null || (propertyPath == null && constantExpression.Value is INotifyPropertyChanged))
                {
                    result = new ExternalDependencyDefinition(propertyPath, constantExpression.Value);
                }
            }
            return(result);
        }
Пример #13
0
        protected override IDependencyDefinition ExtractFromRoot(Expression rootExpression, string propertyPath)
        {
            IDependencyDefinition result = null;

            if (!(rootExpression is MemberExpression memberExpression))
            {
                return(null);
            }
            if (memberExpression.Expression == null)
            {
                result = new StaticDependencyDefinition(propertyPath, memberExpression.Member);
            }
            return(result);
        }
Пример #14
0
        /// <summary>
        /// If the dependency is a For
        ///     AND the plugin name matches
        /// Else If the dependency is an Add
        ///     AND the concrete type matches
        /// </summary>
        /// <param name="dependency"></param>
        /// <returns></returns>
        public bool IsDuplicate(IDependencyDefinition dependency)
        {
            var definitions = new List <IDependencyDefinition>();
            var duplicate   = false;
            var predicate   = dependency.IsAdd
                            ? IsDuplicateAdd
                            : IsDuplicateFor;

            if (Definitions.TryGetValue(dependency.PluginType, out definitions))
            {
                duplicate = definitions
                            .Any(x => predicate(x, dependency));
            }
            return(duplicate);
        }
Пример #15
0
        /// <summary>
        /// When overridden in a derived class, extracts the appropriate dependency from the root of the expression.
        /// </summary>
        /// <param name="rootExpression">The root expression.</param>
        /// <param name="propertyPath">The property path.</param>
        /// <returns></returns>
        protected override IDependencyDefinition ExtractFromRoot(Expression rootExpression, string propertyPath)
        {
            IDependencyDefinition result = null;

            if (rootExpression is MemberExpression)
            {
                // We are left with a member expression that does not have a source. It must
                // use a static item
                var nextMember = (MemberExpression)rootExpression;
                if (nextMember.Expression == null)
                {
                    result = new StaticDependencyDefinition(propertyPath, nextMember.Member);
                }
            }
            return(result);
        }
Пример #16
0
        /// <summary>
        /// Accepts a dependency.
        /// </summary>
        /// <param name="definition">The definition.</param>
        public void AcceptDependency(IDependencyDefinition definition)
        {
            AssertDispatcherThread();
            AssertUnsealed();

            if (!definition.AppliesToCollections())
            {
                return;
            }

            var dependency = definition.ConstructForCollection(SourceCollection, BindingConfigurations.Default.CreatePathNavigator());

            dependency.SetReevaluateElementCallback(((element, propertyName) => ReactToItemPropertyChanged((TSource)element, propertyName)));
            dependency.SetReevaluateCallback((element => ReactToReset()));
            _dependencies.Add(dependency);
        }
Пример #17
0
        public IProvideInstance CreateProvider(Type requested, IDependencyDefinition definition, IDependencyAdapter container)
        {
            IProvideInstance valueProvider = null;

            if (definition.IsSingleton)
            {
                valueProvider = definition.HasSingleton
                                    ? new SingletonFactory(definition.ConcreteInstance)
                                    : new SingletonFactory(BuildFactory(requested, definition, container));
            }
            else
            {
                valueProvider = new InstanceFactory(BuildFactory(requested, definition, container));
            }
            return(valueProvider);
        }
Пример #18
0
        private void HandleFor(IDependencyDefinition dependency)
        {
            ObjectFactory
            .Configure(x =>
            {
                try
                {
                    var forExpression = x.For(dependency.PluginType);

                    Instance instance;
                    if (dependency.IsSingleton)
                    {
                        if (dependency.HasSingleton)
                        {
                            instance =
                                forExpression.Singleton().Use(dependency.ConcreteInstance);
                        }
                        else
                        {
                            instance = forExpression.Singleton().Use(dependency.ConcreteType);
                        }
                    }
                    else if (dependency.HasDelegate)
                    {
                        instance =
                            forExpression.Use(f => dependency.CreatorDelegate.DynamicInvoke());
                    }
                    else
                    {
                        instance = forExpression.Use(dependency.ConcreteType);
                    }

                    if (dependency.IsNamed)
                    {
                        instance.Name = dependency.PluginName;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
                       );
        }
Пример #19
0
        public Func <object> BuildFactory(Type requested, IDependencyDefinition definition, IDependencyAdapter container)
        {
            if (definition.HasDelegate)
            {
                return(BuildDelegateCall(requested, definition));
            }

            ContainerReference = ContainerReference ?? Expression.Constant(container);
            ResolveMethod      = ResolveMethod ?? container.GetType().GetMethod("GetInstance", new Type[] { typeof(Type) });

            var genericTypeArgs = requested.IsGenericType
                                      ? requested.GetGenericArguments()
                                      : new Type[] {};

            var type = genericTypeArgs.Length > 0 && definition.ConcreteType.IsGenericTypeDefinition
                           ? definition.ConcreteType.MakeGenericType(genericTypeArgs)
                           : definition.ConcreteType;

            var constructors = Reflector.GetConstructorInfo(type).OrderBy(x => x.Item2.Length).Reverse();
            var constructor  = constructors
                               .FirstOrDefault(x => x.Item2.All(p => container.HasPluginFor(p.ParameterType)));

            var parameters = constructor
                             .Item2
                             .Select(x =>
            {
                var argExpr  = Expression.Constant(x.ParameterType);
                var callExpr = Expression.Call(ContainerReference, ResolveMethod, argExpr);
                return(Expression.Convert(callExpr, x.ParameterType));
            });

            var newExpr  = Expression.New(constructor.Item1, parameters);
            var castExpr = Expression.Convert(newExpr, typeof(object));

            return(Expression.Lambda <Func <object> >(castExpr).Compile());
        }
Пример #20
0
        public void Register(IDependencyDefinition dependency)
        {
            //var key = dependency.PluginType.IsGenericType && dependency.ConcreteType.IsGenericType
            //              ? dependency.PluginType.GetGenericTypeDefinition()
            //              : dependency.PluginType;

            var key = dependency.PluginType;

            Definitions
            .AddOrUpdate(key,
                         x => new List <IDependencyDefinition>()
            {
                dependency
            },
                         (x, y) =>
            {
                if (IsDuplicate(dependency))
                {
                    EliminateDuplicateDependencies(dependency, y, key);
                }
                y.Add(dependency);
                return(y);
            });
        }
Пример #21
0
 public void Register(IDependencyDefinition dependency)
 {
     throw new AssimilationException(_msg);
 }
Пример #22
0
 public void AddDependency(IDependencyDefinition dependency)
 {
     _dependencyDefinitions.Add(dependency);
 }
Пример #23
0
 public void RemoveProvider(Type requested, IDependencyDefinition definition)
 {
     Providers.Remove(Tuple.Create(requested, definition.ConcreteType, definition.PluginName ?? ""));
 }
Пример #24
0
 public void AddDependency(IDependencyDefinition dependency)
 {
     _property.AddDependency(dependency);
 }
Пример #25
0
        public object GetInstanceFromDefinition(Type serviceType, IDependencyDefinition definition)
        {
            var provider = Providers.GetProviderForPlugin(serviceType, definition, this);

            return(provider.Get());
        }
Пример #26
0
 public IProvideInstance GetProviderForPlugin <TRequest>(IDependencyDefinition definition, IDependencyAdapter container)
 {
     return(GetProviderForPlugin(typeof(TRequest), definition, container));
 }
Пример #27
0
 /// <summary>
 /// Sets a new dependency on a Bindable LINQ operation.
 /// </summary>
 /// <param name="definition">A definition of the dependency.</param>
 public void AcceptDependency(IDependencyDefinition definition)
 {
     throw new NotSupportedException("This object cannot accept dependencies directly.");
 }
Пример #28
0
 public void RemoveProvider <TRequest>(IDependencyDefinition definition)
 {
     RemoveProvider(typeof(TRequest), definition);
 }