Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeFactory" /> class.
        /// </summary>
        /// <param name="serviceLocator">The service locator.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceLocator"/> is <c>null</c>.</exception>
        public TypeFactory(IServiceLocator serviceLocator)
        {
            Argument.IsNotNull("serviceLocator", serviceLocator);

            _currentTypeRequestPath = new ThreadLocal <TypeRequestPath>(() => TypeRequestPath.Root(TypeRequestPathName));

            _serviceLocator = serviceLocator;
            _serviceLocator.TypeRegistered += OnServiceLocatorTypeRegistered;

            // Note: this will cause memory leaks (TypeCache will keep this class alive), but it's an acceptable "loss"
            TypeCache.AssemblyLoaded += OnAssemblyLoaded;
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceLocator"/> class.
        /// </summary>
        public ServiceLocator()
        {
            _currentTypeRequestPath = new ThreadLocal <TypeRequestPath>(() => TypeRequestPath.Root("ServiceLocator"));

            // Must be registered first, already resolved by TypeFactory
            RegisterInstance(typeof(IServiceLocator), this);
            RegisterInstance(typeof(IDependencyResolver), IoCFactory.CreateDependencyResolverFunc(this));

            _typeFactory = IoCFactory.CreateTypeFactoryFunc(this);
            RegisterInstance(typeof(ITypeFactory), _typeFactory);

            _autoRegistrationManager = new ServiceLocatorAutoRegistrationManager(this);

            IgnoreRuntimeIncorrectUsageOfRegisterAttribute = true;
            CanResolveNonAbstractTypesWithoutRegistration  = true;

            // Register default implementations
            // TODO: Enable for CTL-272
            //RegisterType(typeof(ICollection<>), typeof(Collection<>));
            //RegisterType(typeof(IEnumerable<>), typeof(List<>));
            //RegisterType(typeof(IList<>), typeof(List<>));
        }