Represents a service dependency.
Inheritance: IDependency
コード例 #1
0
ファイル: DependencyTests.cs プロジェクト: xerxesb/Hiro
        public void ShouldBeAbleToAddItemsToDependencyMap()
        {
            var ctor = typeof(Vehicle).GetConstructor(new Type[0]);
            var dependency = new Dependency(typeof(IVehicle), string.Empty);
            var constructorImplementation = new ConstructorCall(ctor);

            var dependencyMap = new DependencyMap();
            dependencyMap.AddService(dependency, constructorImplementation);
            Assert.IsTrue(dependencyMap.Contains(dependency));
        }
コード例 #2
0
        /// <summary>
        /// Adds a named service to the dependency map.
        /// </summary>
        /// <remarks>This service will be created once per web session.</remarks>
        /// <param name="map">The target dependency map.</param>
        /// <param name="serviceName">The service name.</param>
        /// <param name="serviceType">The service type.</param>
        /// <param name="implementingType">The implementing type.</param>
        public static void AddPerSessionService(this DependencyMap map, string serviceName, Type serviceType, Type implementingType)
        {
            // Add the SessionCache by default
            if (!map.Contains(typeof(ICache)))
                map.AddService<ICache, SessionCache>();

            // The cached instantiation class will use the cache in the container
            // to cache service instances
            var dependency = new Dependency(serviceType, serviceName);
            var implementation = new CachedInstantiation(new TransientType(implementingType, map, new ConstructorResolver()));

            map.AddService(dependency, implementation);
        }
コード例 #3
0
        public void ShouldInjectDefaultServiceImplementationIntoTargetProperty()
        {
            var map = new DependencyMap();

            var dependency = new Dependency(typeof(IVehicle));
            var injector = new PropertyInjectionCall(new TransientType(typeof(Vehicle), map, new ConstructorResolver()));
            map.AddService(dependency, injector);

            map.AddService(typeof(IPerson), typeof(Person));

            var container = map.CreateContainer();

            var result = (IVehicle)container.GetInstance(typeof(IVehicle), null);
            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Driver);
            Assert.IsTrue(result.Driver is Person);
        }
コード例 #4
0
        public void ShouldCallCacheOnCachedServiceType()
        {
            var map = new DependencyMap();
            var dependency = new Dependency(typeof(IFoo));
            var implementation = new TransientType(typeof(Foo), map, new ConstructorResolver());
            var cachedImplementation = new CachedInstantiation(implementation);

            map.AddSingletonService<ICache, MockCache>();
            map.AddService(dependency, cachedImplementation);

            // Compile the container
            var container = map.CreateContainer();

            // Grab the service instance
            var firstResult = container.GetInstance<IFoo>();
            var secondResult = container.GetInstance<IFoo>();

            Assert.IsNotNull(firstResult);
            Assert.AreSame(firstResult, secondResult);
        }
コード例 #5
0
ファイル: DependencyTests.cs プロジェクト: xerxesb/Hiro
        public void ShouldReturnImplementationsFromDependencyMapFromImplementationsThatHaveNoMissingDependencies()
        {
            var map = new DependencyMap();
            var dependency = new Dependency(typeof(IVehicle), string.Empty);
            var implementation = new Mock<IImplementation>();
            implementation.Expect(impl => impl.GetMissingDependencies(map)).Returns(new IDependency[0]);

            bool addIncompleteImplementations = false;
            map.AddService(dependency, implementation.Object);
            var results = map.GetImplementations(dependency, addIncompleteImplementations);

            Assert.IsTrue(results.Count() > 0);
            Assert.IsTrue(results.Contains(implementation.Object));

            implementation.VerifyAll();
        }
コード例 #6
0
ファイル: DependencyTests.cs プロジェクト: xerxesb/Hiro
        public void ShouldBeEqualIfServiceNameAndServiceTypeAreTheSame()
        {
            var first = new Dependency(typeof(IPerson), string.Empty);
            var second = new Dependency(typeof(IPerson), string.Empty);

            Assert.AreEqual(first, second);
            Assert.AreEqual(first.GetHashCode(), second.GetHashCode());
        }
コード例 #7
0
ファイル: DependencyMapLoader.cs プロジェクト: xerxesb/Hiro
        /// <summary>
        /// Registers a type as a factory type if it implements the <see cref="IFactory{T}"/> interface.
        /// </summary>
        /// <param name="type">The target type</param>
        /// <param name="defaultImplementations">The list of default implementations per service type.</param>
        /// <param name="map">The dependency map.</param>
        private void RegisterNamedFactoryType(Type type, IDictionary<Type, IImplementation> defaultImplementations, IDependencyMap map)
        {
            var factoryTypeDefinition = typeof(IFactory<>);
            var interfaces = type.GetInterfaces();
            foreach (var interfaceType in interfaces)
            {
                if (!interfaceType.IsGenericType)
                    continue;

                var definitionType = interfaceType.GetGenericTypeDefinition();
                if (definitionType != factoryTypeDefinition)
                    continue;

                var genericArguments = interfaceType.GetGenericArguments();
                var actualServiceType = genericArguments[0];
                var serviceName = type.Name;

                var nameLength = serviceName.Length;
                var hasSpecialName = serviceName.EndsWith("Factory") && nameLength > 7;
                serviceName = hasSpecialName ? serviceName.Substring(0, nameLength - 7) : serviceName;
                var implementation = new FactoryCall(actualServiceType, type.Name);

                // Register the default implementation if necessary
                if (!defaultImplementations.ContainsKey(actualServiceType))
                    defaultImplementations[actualServiceType] = implementation;

                var dependency = new Dependency(actualServiceType, serviceName);
                map.AddService(dependency, implementation);
            }
        }
コード例 #8
0
ファイル: DependencyMapLoader.cs プロジェクト: xerxesb/Hiro
        /// <summary>
        /// Loads a dependency map using the types in the given <paramref name="assemblies"/>.
        /// </summary>
        /// <param name="assemblies">The list of assemblies that will be used to construct the dependency map.</param>
        /// <returns>A dependency map.</returns>
        public DependencyMap LoadFrom(IEnumerable<Assembly> assemblies)
        {
            var map = new DependencyMap(_constructorResolver) { Injector = new PropertyInjector() };

            var defaultImplementations = new Dictionary<Type, IImplementation>();
            foreach (var assembly in assemblies)
            {
                var embeddedTypes = _typeLoader.LoadTypes(assembly);
                foreach (var type in embeddedTypes)
                {
                    if (type.IsInterface || type.IsAbstract || type.IsGenericTypeDefinition || type.IsValueType)
                        continue;

                    RegisterNamedFactoryType(type, defaultImplementations, map);
                }
            }

            foreach (var serviceType in defaultImplementations.Keys)
            {
                var dependency = new Dependency(serviceType);
                var implementation = defaultImplementations[serviceType];
                map.AddService(dependency, implementation);
            }

            RegisterServicesFrom(assemblies, map);

            return map;
        }