Exemplo n.º 1
0
 public static ProfileResolver Find(
     ResolveContext context,
     DependencyRegistration reg)
 {
     return(Simple.Find(reg)
            ?? Func.Find(context, reg));
 }
Exemplo n.º 2
0
        public object CreateObject(DependencyRegistration registration)
        {
            StringBuilder unresolvedDependenciesMessage = null;

            foreach (var constructor in registration.Constructors)
            {
                var unresolvedDependencies = new List <ParameterInfo>();
                var dependents             = constructor.Value
                                             .Select(pi =>
                {
                    var success = ResolveContext.TryResolve(pi.ParameterType, out var instance);
                    if (!success)
                    {
                        unresolvedDependencies.Add(pi);
                    }
                    return(instance);
                }).ToArray();


                if (unresolvedDependencies.Any() == false)
                {
                    return(AssignProperties(constructor.Key.Invoke(dependents)));
                }

                LogUnresolvedConstructor(unresolvedDependencies, ref unresolvedDependenciesMessage);
            }
            throw new DependencyResolutionException(
                      $"Could not resolve type {registration.ConcreteType.Name} because its dependencies couldn't be fullfilled\r\n{unresolvedDependenciesMessage}");
        }
Exemplo n.º 3
0
        public object CreateObject(DependencyRegistration registration)
        {
            StringBuilder unresolvedDependenciesMessage = null;

            foreach (var constructor in registration.Constructors)
            {
                var unresolvedDependencies = constructor.Value.Aggregate(new List <ParameterInfo>(),
                                                                         (unresolved, pi) =>
                {
                    if (
                        !ResolveContext.Registrations.HasRegistrationForService(
                            pi.ParameterType))
                    {
                        unresolved.Add(pi);
                    }
                    return(unresolved);
                });
                if (unresolvedDependencies.Count > 0)
                {
                    LogUnresolvedConstructor(unresolvedDependencies, ref unresolvedDependenciesMessage);
                    continue;
                }

                var dependents = from pi in constructor.Value
                                 select ResolveContext.Resolve(pi.ParameterType);

                return(AssignProperties(constructor.Key.Invoke(dependents.ToArray())));
            }
            throw new DependencyResolutionException(
                      "Could not resolve type {0} because its dependencies couldn't be fullfilled\r\n{1}".With(registration.ConcreteType.Name, unresolvedDependenciesMessage));
        }
Exemplo n.º 4
0
        public override object Resolve(ResolveContext context, DependencyRegistration registration)
        {
            var lazy = _instances
                       .GetOrAdd(registration.Key, key => ThreadSafeLazyFactory(context, registration));

            return(lazy.Value);
        }
Exemplo n.º 5
0
        public object CreateObject(DependencyRegistration registration)
        {
            StringBuilder unresolvedDependenciesMessage = null;
            foreach (var constructor in registration.Constructors)
            {
                var unresolvedDependencies = constructor.Value.Aggregate(new List<ParameterInfo>(), 
                                                                         (unresolved, pi) =>
                                                                         {
                                                                             if (
                                                                                 !ResolveContext.Registrations.HasRegistrationForService(
                                                                                      pi.ParameterType))
                                                                                 unresolved.Add(pi);
                                                                             return unresolved;
                                                                         });
                if (unresolvedDependencies.Count > 0)
                {
                    LogUnresolvedConstructor(unresolvedDependencies, ref unresolvedDependenciesMessage);
                    continue;
                }

                var dependents = from pi in constructor.Value
                                 select ResolveContext.Resolve(pi.ParameterType);

                return AssignProperties(constructor.Key.Invoke(dependents.ToArray()));
            }
            throw new DependencyResolutionException(
                "Could not resolve type {0} because its dependencies couldn't be fullfilled\r\n{1}".With(registration.ConcreteType.Name, unresolvedDependenciesMessage));
        }
Exemplo n.º 6
0
 public void Add(DependencyRegistration registration)
 {
     registration.LifetimeManager.VerifyRegistration(registration);
     lock (_registrations)
     {
         GetSvcRegistrations(registration.ServiceType).Add(registration);
     }
 }
 public void Add(DependencyRegistration registration)
 {
     registration.LifetimeManager.VerifyRegistration(registration);
     lock (_registrations)
     {
         GetSvcRegistrations(registration.ServiceType).Add(registration);
     }
 }
Exemplo n.º 8
0
            public static ProfileResolver Find(DependencyRegistration reg)
            {
                bool tryResolve(IDependencyRegistrationCollection registrations, ResolveContext context, out object instance)
                => context.TryResolve(reg, out instance);

                if (reg != null)
                {
                    return(tryResolve);
                }
                return(null);
            }
        public override bool IsRegistrationAvailable(DependencyRegistration registration)
        {
            if (!Resolver.HasDependency(typeof(IContextStore))) return false;

            if (!registration.IsInstanceRegistration)
                return true;
            var store = Resolver.Resolve<IContextStore>();

            bool storeHasRegistration = store.GetContextInstances().Any(x => x.Key == registration.Key);
            return storeHasRegistration;
        }
Exemplo n.º 10
0
        public override object Resolve(ResolveContext context, DependencyRegistration registration)
        {
            if (!_resolver.TryResolve <IContextStore>(out var store))
            {
                throw new DependencyResolutionException(
                          "Could not resolve the context store. Make sure you have registered one.");
            }

            return(store
                   .GetConcurrentContextInstances()
                   .GetOrAdd(registration, reg => reg.Factory(context)));
        }
        public override object Resolve(ResolveContext context, DependencyRegistration registration)
        {
            object instance;

            if (!_instances.TryGetValue(registration.Key, out instance))
                lock (_instances)
                {
                    if (!_instances.TryGetValue(registration.Key, out instance))
                        _instances.Add(registration.Key, instance = base.Resolve(context, registration));
                }
            return instance;
        }
 public override void VerifyRegistration(DependencyRegistration registration)
 {
     if (registration.IsInstanceRegistration)
     {
         if (_instances[registration.Key] != null)
             throw new InvalidOperationException("Trying to register an instance for a registration that already has one.");
         lock (_instances)
         {
             _instances[registration.Key] = registration.Instance;
         }
         registration.Instance = null;
     }
 }
        public override void VerifyRegistration(DependencyRegistration registration)
        {
            if (registration.RegistrationKind == RegistrationKind.Instance)
            {
                CheckContextStoreAvailable();
                var instance = registration.Instance;
                registration.Instance = null;
                var store = Resolver.Resolve<IContextStore>();
                if (store[registration.Key] != null)
                    throw new DependencyResolutionException("An instance is being registered for an existing registration.");

                StoreInstanceInContext(store, registration.Key, instance);
            }
        }
Exemplo n.º 14
0
        public object Resolve(DependencyRegistration registration)
        {
            if (_recursionDefender.Contains(registration))
                throw new InvalidOperationException("Recursive dependencies are not allowed.");
            try
            {
                _recursionDefender.Push(registration);

                return registration.LifetimeManager.Resolve(this, registration);
            }
            finally
            {
                _recursionDefender.Pop();
            }
        }
Exemplo n.º 15
0
 public override void VerifyRegistration(DependencyRegistration registration)
 {
     if (registration.IsInstanceRegistration)
     {
         if (_instances[registration.Key] != null)
         {
             throw new InvalidOperationException("Trying to register an instance for a registration that already has one.");
         }
         lock (_instances)
         {
             _instances[registration.Key] = registration.Instance;
         }
         registration.Instance = null;
     }
 }
Exemplo n.º 16
0
        public override object Resolve(ResolveContext context, DependencyRegistration registration)
        {
            object instance;

            if (!_instances.TryGetValue(registration.Key, out instance))
            {
                lock (_instances)
                {
                    if (!_instances.TryGetValue(registration.Key, out instance))
                    {
                        _instances.Add(registration.Key, instance = base.Resolve(context, registration));
                    }
                }
            }
            return(instance);
        }
        public override void VerifyRegistration(DependencyRegistration registration)
        {
            if (registration.IsInstanceRegistration)
            {
                CheckContextStoreAvailable();
                var instance = registration.Instance;
                registration.Instance = null;
                var store = Resolver.Resolve <IContextStore>();
                if (store[registration.Key] != null)
                {
                    throw new DependencyResolutionException("An instance is being registered for an existing registration.");
                }

                StoreInstanceInContext(store, registration.Key, instance);
            }
        }
        public object Resolve(DependencyRegistration registration)
        {
            if (_recursionDefender.Contains(registration))
            {
                throw new InvalidOperationException("Recursive dependencies are not allowed.");
            }
            try
            {
                _recursionDefender.Push(registration);

                return(registration.LifetimeManager.Resolve(this, registration));
            }
            finally
            {
                _recursionDefender.Pop();
            }
        }
        public override bool IsRegistrationAvailable(DependencyRegistration registration)
        {
            if (!Resolver.HasDependency(typeof(IContextStore)))
            {
                return(false);
            }

            if (!registration.IsInstanceRegistration)
            {
                return(true);
            }
            var store = Resolver.Resolve <IContextStore>();

            bool storeHasRegistration = store.GetContextInstances().Any(x => x.Key == registration.Key);

            return(storeHasRegistration);
        }
 public bool TryResolve(DependencyRegistration registration, out object instance)
 {
     instance = null;
     if (_recursionDefender.Contains(registration))
     {
         return(false);
     }
     try
     {
         _recursionDefender.Push(registration);
         instance = registration.ResolveInContext(this);
         return(true);
     }
     finally
     {
         _recursionDefender.Pop();
     }
 }
        public override object Resolve(ResolveContext context, DependencyRegistration registration)
        {
            CheckContextStoreAvailable();

            object instance;
            var contextStore = Resolver.Resolve<IContextStore>();

            if ((instance = contextStore[registration.Key]) == null)
            {
                if (registration.RegistrationKind == RegistrationKind.Instance)
                    throw new DependencyResolutionException("A dependency registered as an instance wasn't found. The registration was removed.");

                instance = base.Resolve(context, registration);

                StoreInstanceInContext(contextStore, registration.Key, instance);
            }
            return instance;
        }
        public override object Resolve(ResolveContext context, DependencyRegistration registration)
        {
            CheckContextStoreAvailable();

            object instance;
            var    contextStore = Resolver.Resolve <IContextStore>();

            if ((instance = contextStore[registration.Key]) == null)
            {
                if (registration.IsInstanceRegistration)
                {
                    throw new DependencyResolutionException("A dependency registered as an instance wasn't found. The registration was removed.");
                }

                instance = base.Resolve(context, registration);

                StoreInstanceInContext(contextStore, registration.Key, instance);
            }
            return(instance);
        }
Exemplo n.º 23
0
            public static ProfileResolver Find(ResolveContext context, DependencyRegistration registration)
            {
                var serviceType = registration.ServiceType;

                if (serviceType.IsGenericType == false ||
                    serviceType.GetGenericTypeDefinition() != typeof(Func <>))
                {
                    return(null);
                }

                var innerType    = serviceType.GetGenericArguments()[0];
                var innerProfile = ResolveProfiles.Find(context, registration);

                ProfileResolver getTypedResolver(ProfileResolver resolver)
                {
                    var profile =
                        (ResolveProfile)Activator.CreateInstance(typeof(FuncProfile <>).MakeGenericType(innerType), innerProfile);

                    return(profile.TryResolve);
                }

                return(innerProfile != null?getTypedResolver(innerProfile) : null);
            }
 public virtual bool IsRegistrationAvailable(DependencyRegistration registration)
 {
     return true;
 }
 public virtual object Resolve(ResolveContext context, DependencyRegistration registration)
 {
     return context.Builder.CreateObject(registration);
 }
Exemplo n.º 26
0
 static Lazy <object> ThreadSafeLazyFactory(ResolveContext context, DependencyRegistration registration)
 {
     return(new Lazy <object>(() => registration.Factory(context), LazyThreadSafetyMode.ExecutionAndPublication));
 }
 public void Add(DependencyRegistration registration)
 {
     _registrations.Push(registration);
     Last = registration;
 }
        public void Add(DependencyRegistration registration)
        {
            var bag = _registrations.GetOrAdd(registration.ServiceType, type => new RegistrationBag());

            bag.Add(registration.OverrideLifetimeManager(_lifetimes[registration.Lifetime]));
        }
Exemplo n.º 29
0
 public bool CanResolve(DependencyRegistration registration)
 {
     return !this.recursionDefender.Contains(registration);
 }
Exemplo n.º 30
0
 public void Add(DependencyRegistration registration)
 {
     _registrations
     .GetOrAdd(registration.ServiceType, t => new RegistrationBag())
     .Add(registration);
 }
Exemplo n.º 31
0
 public object Resolve(DependencyRegistration registration)
 {
     return(TryResolve(registration, out var instance)
 ? instance
 : throw new InvalidOperationException("Recursive dependencies are not allowed."));
 }
 public bool CanResolve(DependencyRegistration registration)
 {
     return(!_recursionDefender.Contains(registration));
 }
 public virtual void VerifyRegistration(DependencyRegistration registration)
 {
 }