Пример #1
0
        void IObjectContainer.RegisterType(Type type, string name, Lifecycle lifetime)
        {
            if (completed >= 1)
            {
                throw new ApplicationException(
                          "can not register type because this container is completed, please invoke before 'Complete' method.");
            }

            Assertions.NotNull(type, "type");

            if (!type.IsClass || type.IsAbstract)
            {
                throw new ApplicationException(
                          string.Format("the type of '{0}' must be a class and cannot be abstract.", type.FullName));
            }

            var typeRegistration = new TypeRegistration(type, name, lifetime == Lifecycle.Singleton && typeof(IInitializer).IsAssignableFrom(type));

            if (registeredTypes.Contains(typeRegistration) || IsRegistered(type, name))
            {
                throw new ApplicationException(
                          string.Format("the type of '{0}' as name '{1}' has been registered.", type.FullName, name));
            }

            registeredTypes.Add(typeRegistration);
            RegisterType(type, name, lifetime);
        }
Пример #2
0
        public override void RegisterInstance(Type type, object instance, string name)
        {
            var typeRegistration = new TypeRegistration(type, name);

            if (_typeInstanceMap.TryAdd(typeRegistration, instance))
            {
                _typeRegistrationMap.GetOrAdd(type, () => new List <TypeRegistration>()).Add(typeRegistration);
            }
        }
Пример #3
0
        public override void RegisterType(Type from, Type to, string name, Lifecycle lifetime)
        {
            var typeRegistration = new TypeRegistration(from, name);

            if (_typeImplementerMap.TryAdd(typeRegistration, to))
            {
                _typeRegistrationMap.GetOrAdd(from, () => new List <TypeRegistration>()).Add(typeRegistration);
                _typeBuilderMap.TryAdd(to, new ObjectBuilder(to, lifetime, this));
            }
        }
Пример #4
0
        private object Resolve(TypeRegistration typeRegistration)
        {
            object instance = null;

            if (!_typeInstanceMap.TryGetValue(typeRegistration, out instance))
            {
                Type implementerType;
                if (_typeImplementerMap.TryGetValue(typeRegistration, out implementerType))
                {
                    ObjectBuilder objectBuilder;
                    if (_typeBuilderMap.TryGetValue(implementerType, out objectBuilder))
                    {
                        instance = objectBuilder.GetInstance();
                    }
                }
            }

            return(instance);
        }
Пример #5
0
        void IObjectContainer.RegisterInstance(Type type, object instance, string name)
        {
            if (completed >= 1)
            {
                throw new ApplicationException(
                          "can not register type because this container is completed, please invoke before 'Complete' method.");
            }

            Assertions.NotNull(type, "type");
            Assertions.NotNull(instance, "instance");

            var typeRegistration = new TypeRegistration(type, name, instance is IInitializer);

            if (registeredTypes.Contains(typeRegistration) || IsRegistered(type, name))
            {
                throw new ApplicationException(
                          string.Format("the type of '{0}' as name '{1}' has been registered.", type.FullName, name));
            }

            registeredTypes.Add(typeRegistration);
            RegisterInstance(type, instance, name);
        }