Ejemplo n.º 1
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));
        }
 public void Add(DependencyRegistration registration)
 {
     registration.LifetimeManager.VerifyRegistration(registration);
     lock (_registrations)
     {
         GetSvcRegistrations(registration.ServiceType).Add(registration);
     }
 }
        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 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.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);
            }
        }
Ejemplo n.º 7
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();
            }
        }
        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;
        }
Ejemplo n.º 9
0
 public bool CanResolve(DependencyRegistration registration)
 {
     return !_recursionDefender.Contains(registration);
 }
 public virtual void VerifyRegistration(DependencyRegistration registration)
 {
 }
 public virtual object Resolve(ResolveContext context, DependencyRegistration registration)
 {
     return context.Builder.CreateObject(registration);
 }
 public virtual bool IsRegistrationAvailable(DependencyRegistration registration)
 {
     return true;
 }