public void Prepare()
        {
            var map = new DependencyMap();

            map.AddSingletonService <ISingleton, Singleton>();
            map.AddService <ITransient, Transient>();
            map.AddService <ICombined, Combined>();

            this.container = map.CreateContainer();
        }
 private static void RegisterComplex(DependencyMap map)
 {
     map.AddSingletonService<IFirstService, FirstService>();
     map.AddSingletonService<ISecondService, SecondService>();
     map.AddSingletonService<IThirdService, ThirdService>();
     map.AddService<ISubObjectOne, SubObjectOne>();
     map.AddService<ISubObjectTwo, SubObjectTwo>();
     map.AddService<ISubObjectThree, SubObjectThree>();
     map.AddService<IComplex, Complex>();
 }
 private static void RegisterComplex(DependencyMap map)
 {
     map.AddSingletonService <IFirstService, FirstService>();
     map.AddSingletonService <ISecondService, SecondService>();
     map.AddSingletonService <IThirdService, ThirdService>();
     map.AddService <ISubObjectOne, SubObjectOne>();
     map.AddService <ISubObjectTwo, SubObjectTwo>();
     map.AddService <ISubObjectThree, SubObjectThree>();
     map.AddService <IComplex, Complex>();
 }
Пример #4
0
        public override void Prepare()
        {
            var map = new DependencyMap();

            map.AddSingletonService<ISingleton, Singleton>();
            map.AddService<ITransient, Transient>();
            map.AddService<ICombined, Combined>();

            this.container = map.CreateContainer();
        }
 private static void RegisterStandard(DependencyMap map)
 {
     map.AddSingletonService<ISingleton1, Singleton1>();
     map.AddSingletonService<ISingleton2, Singleton2>();
     map.AddSingletonService<ISingleton3, Singleton3>();
     map.AddService<ITransient1, Transient1>();
     map.AddService<ITransient2, Transient2>();
     map.AddService<ITransient3, Transient3>();
     map.AddService<ICombined1, Combined1>();
     map.AddService<ICombined2, Combined2>();
     map.AddService<ICombined3, Combined3>();
 }
 private static void RegisterDummies(DependencyMap map)
 {
     map.AddService <IDummyOne, DummyOne>();
     map.AddService <IDummyTwo, DummyTwo>();
     map.AddService <IDummyThree, DummyThree>();
     map.AddService <IDummyFour, DummyFour>();
     map.AddService <IDummyFive, DummyFive>();
     map.AddService <IDummySix, DummySix>();
     map.AddService <IDummySeven, DummySeven>();
     map.AddService <IDummyEight, DummyEight>();
     map.AddService <IDummyNine, DummyNine>();
     map.AddService <IDummyTen, DummyTen>();
 }
 private static void RegisterDummies(DependencyMap map)
 {
     map.AddService<IDummyOne, DummyOne>();
     map.AddService<IDummyTwo, DummyTwo>();
     map.AddService<IDummyThree, DummyThree>();
     map.AddService<IDummyFour, DummyFour>();
     map.AddService<IDummyFive, DummyFive>();
     map.AddService<IDummySix, DummySix>();
     map.AddService<IDummySeven, DummySeven>();
     map.AddService<IDummyEight, DummyEight>();
     map.AddService<IDummyNine, DummyNine>();
     map.AddService<IDummyTen, DummyTen>();
 }
Пример #8
0
        public void ShouldUseFirstNamedServiceInstanceIfNoDefaultServiceIsAvailable()
        {
            var map = new DependencyMap();

            map.AddService <Garage, Garage>();
            map.AddService <IVehicle, Truck>("Truck");

            var container = map.CreateContainer();

            var garage = container.GetInstance <Garage>();

            Assert.IsNotNull(garage.Vehicle);
            Assert.IsInstanceOfType(typeof(Truck), garage.Vehicle);
        }
Пример #9
0
        public Hiro()
        {
            var map = new DependencyMap();
            map.AddSingletonService<Game, Game>();
            map.AddService<Player, Player>();
            map.AddService<Gun, Gun>();
            map.AddService<Bullet, Bullet>();

            Func<IMicroContainer, Bullet> createBullet = c => c.GetInstance<Bullet>();
            Func<IMicroContainer, Func<Bullet>> createBulletFunctor = c => () => createBullet(c);
            map.AddService(createBulletFunctor);

            container = map.CreateContainer();
        }
Пример #10
0
        public void ShouldBeAbleToInstantiateNamedGenericType()
        {
            var map = new DependencyMap();
            map.AddService("List", typeof(IEnumerable<>), typeof(List<>));
            map.AddService("Queue", typeof(IEnumerable<>), typeof(Queue<>));

            var container = map.CreateContainer();
            var list = container.GetInstance<IEnumerable<int>>("List");
            Assert.IsNotNull(list);

            var queue = container.GetInstance<IEnumerable<int>>("Queue");
            Assert.IsNotNull(queue);

            Assert.IsFalse(queue.GetType() != list.GetType());
        }
Пример #11
0
        public void ShouldInjectPropertyIfDependencyMapHasAPropertyInjectorAssignedToTheInjectorProperty()
        {
            var map = new DependencyMap();
            map.Injector = new PropertyInjector();

            map.AddService<IVehicle, Vehicle>();
            map.AddService<IPerson, 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);
        }
Пример #12
0
        public void ShouldBeAbleToCompileContainerUsingATypeWithMultipleConstructors()
        {
            var map = new DependencyMap();

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

            var container = Compile(map);
            var result    = container.GetInstance(typeof(IVehicle), null);

            var vehicle = (IVehicle)result;

            Assert.IsNotNull(vehicle);
            Assert.IsNotNull(vehicle.Driver);
        }
Пример #13
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);
        }
Пример #14
0
        public void ShouldUseConstructorParameterNameToInjectNamedServiceInstanceIfTheNamedServiceExists()
        {
            var map = new DependencyMap();

            map.AddService <IVehicle, Vehicle>("Vehicle");
            map.AddService <IVehicle, Truck>("Truck");
            map.AddService <Garage, Garage>();

            var container = map.CreateContainer();

            var garage = container.GetInstance <Garage>();

            Assert.IsNotNull(garage.Vehicle);
            Assert.IsInstanceOfType(typeof(Vehicle), garage.Vehicle);
        }
Пример #15
0
        public void Load(DependencyMap map)
        {
            _invoked = true;

            // Add a sample implementation
            map.AddService<IList<int>, List<int>>();
        }
Пример #16
0
        public void ShouldBeAbleToAddDeferredServiceToContainer()
        {
            var map = new DependencyMap();

            map.AddDeferredService(typeof(IPerson));

            map.Injector = new PropertyInjector();
            map.AddService(typeof(Truck), typeof(Truck));

            var mockPerson = new Mock <IPerson>();
            var container  = map.CreateContainer();

            container.AddService(mockPerson.Object);

            // The container must instantiate the mock person type
            var person = container.GetInstance <IPerson>();

            Assert.AreSame(mockPerson.Object, person);
            Assert.IsNotNull(person);

            // Make sure the person instance is injected into
            // the target property
            var truck = container.GetInstance <Truck>();

            Assert.IsNotNull(truck);
            Assert.AreSame(truck.Driver, mockPerson.Object);
        }
Пример #17
0
        public void ShouldBeAbleToReturnAllAvailableImplementationsForADependency()
        {
            var numberOfImplementations = 5;
            var implementations         = Enumerable.Range(0, numberOfImplementations)
                                          .Select(_ => A.Fake <IImplementation>()).ToArray();

            var dependency = new Dependency(typeof(IEnumerable <string>));
            var map        = new DependencyMap();

            for (var i = 0; i < numberOfImplementations; i++)
            {
                var implementation = implementations[i];
                map.AddService(dependency, implementation);
            }

            Assert.True(map.Contains(dependency));
            Assert.Contains(dependency, map.Dependencies);

            var results = map.GetImplementations(dependency, false).ToArray();

            for (var i = 0; i < numberOfImplementations; i++)
            {
                Assert.Contains(implementations[i], results);
            }
        }
Пример #18
0
        public void ShouldBeAbleToAddMultipleServices()
        {
            var numberOfDependencies = 5;
            var implementations      = Enumerable.Range(0, numberOfDependencies)
                                       .Select(_ => A.Fake <IImplementation>()).ToArray();

            var dependencies = Enumerable.Range(0, numberOfDependencies)
                               .Select(_ => A.Fake <IDependency>()).ToArray();

            var map = new DependencyMap();

            for (var i = 0; i < numberOfDependencies; i++)
            {
                var dependency     = dependencies[i];
                var implementation = implementations[i];
                map.AddService(dependency, implementation);
            }

            for (var i = 0; i < numberOfDependencies; i++)
            {
                var dependency = dependencies[i];
                Assert.True(map.Contains(dependency));
                Assert.Contains(dependency, map.Dependencies);
            }
        }
Пример #19
0
        /// <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 <System.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);
        }
Пример #20
0
        public void Load(DependencyMap map)
        {
            _invoked = true;

            // Add a sample implementation
            map.AddService <IList <int>, List <int> >();
        }
Пример #21
0
        public Hiro()
        {
            var map = new DependencyMap();

            map.AddSingletonService <Game, Game>();
            map.AddService <Player, Player>();
            map.AddService <Gun, Gun>();
            map.AddService <Bullet, Bullet>();

            Func <IMicroContainer, Bullet>         createBullet        = c => c.GetInstance <Bullet>();
            Func <IMicroContainer, Func <Bullet> > createBulletFunctor = c => () => createBullet(c);

            map.AddService(createBulletFunctor);

            container = map.CreateContainer();
        }
Пример #22
0
        /// <summary>
        /// Registers a service with a dependency map.
        /// </summary>
        /// <param name="map">The dependency map.</param>
        /// <param name="service">The service that will be registered with the dependency map.</param>
        internal static void Register(this DependencyMap map, IServiceInfo service)
        {
            var serviceName      = service.ServiceName;
            var serviceType      = service.ServiceType;
            var implementingType = service.ImplementingType;

            map.AddService(serviceName, serviceType, implementingType);
        }
Пример #23
0
        public void ShouldBeAbleToRegisterAnonymousServicesWithDependencyMapUsingGenerics()
        {
            var dependencyMap = new DependencyMap();
            dependencyMap.AddService<IVehicle, Vehicle>();

            Assert.IsTrue(dependencyMap.Contains(new Dependency(typeof(IVehicle))));
            Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle)));
        }
Пример #24
0
        public void ShouldInjectPropertyIfDependencyMapHasAPropertyInjectorAssignedToTheInjectorProperty()
        {
            var map = new DependencyMap();

            map.Injector = new PropertyInjector();

            map.AddService <IVehicle, Vehicle>();
            map.AddService <IPerson, 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);
        }
Пример #25
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);
        }
Пример #26
0
        public void ShouldBeAbleToRegisterNamedServicesWithDependencyMapUsingGenerics()
        {
            var serviceName   = "MyService";
            var dependencyMap = new DependencyMap();

            dependencyMap.AddService <IVehicle, Vehicle>(serviceName);
            Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle), serviceName));
        }
Пример #27
0
        public void ShouldBeAbleToInstantiateNamedGenericType()
        {
            var map = new DependencyMap();

            map.AddService("List", typeof(IEnumerable <>), typeof(List <>));
            map.AddService("Queue", typeof(IEnumerable <>), typeof(Queue <>));

            var container = map.CreateContainer();
            var list      = container.GetInstance <IEnumerable <int> >("List");

            Assert.IsNotNull(list);

            var queue = container.GetInstance <IEnumerable <int> >("Queue");

            Assert.IsNotNull(queue);

            Assert.IsFalse(queue.GetType() != list.GetType());
        }
Пример #28
0
        public void ShouldBeAbleToRegisterAnonymousServicesWithDependencyMap()
        {
            var dependencyMap = new DependencyMap();

            dependencyMap.AddService(typeof(IVehicle), typeof(Vehicle));

            Assert.IsTrue(dependencyMap.Contains(new Dependency(typeof(IVehicle))));
            Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle)));
        }
Пример #29
0
        public void ShouldBeAbleToInstantiateGenericTypes()
        {
            var map = new DependencyMap();
            map.AddService(typeof(IList<>), typeof(List<>));

            var container = map.CreateContainer();
            var list = container.GetInstance<IList<int>>();
            Assert.IsNotNull(list);
        }
        public void ShouldBeAbleToUseGenericsToGetAnonymousServiceInstances()
        {
            var map = new DependencyMap();
            map.AddService(typeof(IPerson), typeof(Person));

            var container = map.CreateContainer();
            IPerson person = container.GetInstance<IPerson>();

            Assert.IsNotNull(person);
        }
Пример #31
0
        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));
        }
Пример #32
0
        public void ShouldCallContainerPluginOnceContainerIsInstantiated()
        {
            var map = new DependencyMap();

            map.AddService <IContainerPlugin, SamplePlugin>();

            var container = map.CreateContainer();

            Assert.IsTrue(SamplePlugin.HasBeenCalled);
        }
Пример #33
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);
        }
Пример #34
0
        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));
        }
Пример #35
0
        public void ShouldBeAbleToInstantiateGenericTypes()
        {
            var map = new DependencyMap();

            map.AddService(typeof(IList <>), typeof(List <>));

            var container = map.CreateContainer();
            var list      = container.GetInstance <IList <int> >();

            Assert.IsNotNull(list);
        }
Пример #36
0
        public void ShouldHaveTwoEqualDependencyMapsIfBothMapsContainTheSameDependencies()
        {
            var firstMap  = new DependencyMap();
            var secondMap = new DependencyMap();

            firstMap.AddService(typeof(IPerson), typeof(Person));
            secondMap.AddService(typeof(IPerson), typeof(Person));

            Assert.AreEqual(firstMap, secondMap);
            Assert.AreEqual(firstMap.GetHashCode(), secondMap.GetHashCode());
        }
Пример #37
0
        public void ShouldBeAbleToUseGenericsToGetAnonymousServiceInstances()
        {
            var map = new DependencyMap();

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

            var     container = map.CreateContainer();
            IPerson person    = container.GetInstance <IPerson>();

            Assert.IsNotNull(person);
        }
Пример #38
0
        public void ShouldBeAbleToGetCurrentListOfDependencies()
        {
            var map = new DependencyMap();
            for (int i = 0; i < 10; i++)
            {
                var dependency = new Mock<IDependency>();
                var implementation = new Mock<IImplementation>();

                map.AddService(dependency.Object, implementation.Object);
                Assert.IsTrue(map.Dependencies.Contains(dependency.Object));
            }
        }
Пример #39
0
        public void ShouldBeAbleToCompileContainerFromDependencyMap()
        {
            var map = new DependencyMap();

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

            var container = map.CreateContainer();
            var result    = container.GetInstance(typeof(IPerson), null);

            Assert.IsNotNull(result);
            Assert.IsTrue(result is Person);
        }
Пример #40
0
        public void ShouldBeAbleToReturnAllAvailableImplementationsForADependencyExceptForTheIncompleteDependencies()
        {
            var numberOfImplementations           = 5;
            var numberOfIncompleteImplementations = 3;

            IEnumerable <IImplementation> CreateImplementations(int numberOfImplementationsToCreate, bool isCompleted)
            {
                var incompleteDependencies =
                    isCompleted ? Enumerable.Empty <IDependency>() : new[] { A.Fake <IDependency>() };

                return(Enumerable.Range(0, numberOfImplementationsToCreate)
                       .Select(_ =>
                {
                    var currentImplementation = A.Fake <IImplementation>();
                    A.CallTo(() => currentImplementation.GetMissingDependencies(A <IDependencyMap> .Ignored))
                    .Returns(incompleteDependencies);

                    return currentImplementation;
                }).ToArray());
            }

            var implementations = new List <IImplementation>();

            implementations.AddRange(CreateImplementations(numberOfImplementations, true));

            var incompleteImplementations = CreateImplementations(numberOfIncompleteImplementations, false)
                                            .ToArray();

            implementations.AddRange(incompleteImplementations);

            var dependency = new Dependency(typeof(IEnumerable <string>));
            var map        = new DependencyMap();

            foreach (var implementation in implementations)
            {
                map.AddService(dependency, implementation);
            }

            Assert.True(map.Contains(dependency));
            Assert.Contains(dependency, map.Dependencies);

            var results = map.GetImplementations(dependency, false).ToArray();

            for (var i = 0; i < numberOfImplementations; i++)
            {
                Assert.Contains(implementations[i], results);
            }

            foreach (var implementation in incompleteImplementations)
            {
                Assert.DoesNotContain(implementation, results);
            }
        }
Пример #41
0
        public void ShouldBeAbleToGetAllInstancesOfATypeFromACompiledContainer()
        {
            var map = new DependencyMap();

            map.AddService(typeof(IVehicle), typeof(Vehicle));
            map.AddService("Truck", typeof(IVehicle), typeof(Truck));

            var compiler = new ContainerCompiler();
            var assembly = compiler.Compile("MicroContainer", "Hiro.Containers", "Hiro.CompiledContainers", map);

            var container = Compile(map);
            var instances = container.GetAllInstances(typeof(IVehicle));

            Assert.IsNotNull(instances);
            Assert.IsTrue(instances.Count() == 2);

            var items = instances.ToArray();

            Assert.IsTrue(items[0] is Vehicle);
            Assert.IsTrue(items[1] is Truck);
        }
Пример #42
0
        public void ShouldBeAbleToCreateTheSameContainerTypeFromASingleDependencyMap()
        {
            var map = new DependencyMap();

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


            var firstContainer  = map.CreateContainer();
            var secondContainer = map.CreateContainer();

            Assert.AreEqual(firstContainer.GetType(), secondContainer.GetType());
        }
Пример #43
0
        public void ShouldCallImplementationInjectorIfItExists()
        {
            var injector = new Mock <IImplementationInjector>();
            var map      = new DependencyMap();

            map.Injector = injector.Object;

            injector.Expect(i => i.Inject(It.IsAny <IDependency>(), It.IsAny <IImplementation>())).Returns(new TransientType(typeof(Vehicle), map, new ConstructorResolver()));

            map.AddService <IVehicle, Vehicle>();
            injector.VerifyAll();
        }
Пример #44
0
        public void ShouldCallInitializeOnTransientTypeThatImplementsIInitializeWhenCallingGetAllInstances()
        {
            var map = new DependencyMap();

            map.AddService <IInitialize, SampleInitialize>();

            var container = map.CreateContainer();
            var result    = (SampleInitialize)container.GetAllInstances(typeof(IInitialize)).Cast <IInitialize>().First();

            Assert.AreSame(container, result.Container);
            Assert.IsTrue(result.NumberOfTimesInitialized == 1);
        }
Пример #45
0
        public void ShouldBeAbleToGetAllEnumerableInstancesOfAGivenService()
        {
            var map = new DependencyMap();
            map.AddService("Baz1", typeof(IBaz<int>), typeof(Baz1));
            map.AddService("Baz2", typeof(IBaz<int>), typeof(Baz2));
            map.AddService("Baz3", typeof(IBaz<int>), typeof(Baz3));
            map.AddService(typeof(IFizz), typeof(SampleClassWithEnumerableBazDependency));

            // Make the IEnumerable<IBazz<int>> service explictly resolvable
            map.AddAsEnumerableService(typeof(IBaz<int>));

            var container = map.CreateContainer();
            var fizz = container.GetInstance<IFizz>();
            Assert.IsNotNull(fizz);
            Assert.AreEqual(3, fizz.Services.Count());

            var services = fizz.Services.ToArray();
            Assert.IsInstanceOfType(typeof(Baz1), services[0]);
            Assert.IsInstanceOfType(typeof(Baz2), services[1]);
            Assert.IsInstanceOfType(typeof(Baz3), services[2]);
        }
Пример #46
0
        public void ShouldBeAbleToAddTypedFunctorToDependencyMap()
        {
            var expectedInstance = new Foo();
            Func<IMicroContainer, IFoo> functor = c => expectedInstance;

            var map = new DependencyMap();
            map.AddService(functor);

            var container = map.CreateContainer();
            var foo = container.GetInstance<IFoo>();

            Assert.AreSame(expectedInstance, foo);
        }
Пример #47
0
        public void ShouldBeAbleToCombineDependencyMaps()
        {
            var firstMap = new DependencyMap();
            var secondMap = new DependencyMap();

            firstMap.AddService(typeof(IPerson), typeof(Person));
            secondMap.AddService(typeof(IVehicle), typeof(Vehicle));

            DependencyMap combinedMap = firstMap + secondMap;
            var container = combinedMap.CreateContainer();
            Assert.IsNotNull(container.GetInstance<IVehicle>());
            Assert.IsNotNull(container.GetInstance<IPerson>());
        }
Пример #48
0
        public void ShouldBeAbleToAddNamedUntypedFunctorToDependencyMap()
        {
            var expectedInstance = new Foo();
            Func<IMicroContainer, object> functor = c => expectedInstance;

            var serviceType = typeof(IFoo);
            var map = new DependencyMap();
            map.AddService("myFoo", serviceType, functor);

            var container = map.CreateContainer();
            var foo = container.GetInstance<IFoo>("myFoo");
            Assert.IsNotNull(foo);
            Assert.AreSame(expectedInstance, foo);
        }
Пример #49
0
        public void ShouldBeAbleToInjectGenericConstructor()
        {
            var map = new DependencyMap();
            map.AddService<UnitOfWorkScopeBase<UserUnitOfWork>,
            SimpleUnitOfWorkScope<UserUnitOfWork>>();

            map.AddSingletonService<LightSpeedContext<UserUnitOfWork>,
            LightSpeedContext<UserUnitOfWork>>();

            var container = map.CreateContainer();
            var service = container.GetInstance<UnitOfWorkScopeBase<UserUnitOfWork>>();

            Assert.IsNotNull(service);
        }
Пример #50
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);
        }
Пример #51
0
        static HiroUseCase()
        {
            var map = new DependencyMap();
            map.AddService(typeof(IWebService), typeof(WebService));
            map.AddService(typeof(IAuthenticator), typeof(Authenticator));
            map.AddService(typeof(IStockQuote), typeof(StockQuote));
            map.AddService(typeof(IDatabase), typeof(Database));
            map.AddService(typeof(IErrorHandler), typeof(ErrorHandler));
            map.AddService(typeof(ILogger), typeof(Logger));

            IContainerCompiler compiler = new ContainerCompiler();
            var assembly = compiler.Compile(map);

            var loadedAssembly = assembly.ToAssembly();
            var containerType = loadedAssembly.GetTypes()[0];
            container = (IMicroContainer)Activator.CreateInstance(containerType);
        }
        public void ShouldBeAbleToAddDeferredServiceToContainer()
        {
            var map = new DependencyMap();
            map.AddDeferredService(typeof(IPerson));

            map.Injector = new PropertyInjector();
            map.AddService(typeof(Truck), typeof(Truck));

            var mockPerson = new Mock<IPerson>();
            var container = map.CreateContainer();
            container.AddService(mockPerson.Object);

            // The container must instantiate the mock person type
            var person = container.GetInstance<IPerson>();
            Assert.AreSame(mockPerson.Object, person);
            Assert.IsNotNull(person);

            // Make sure the person instance is injected into
            // the target property
            var truck = container.GetInstance<Truck>();
            Assert.IsNotNull(truck);
            Assert.AreSame(truck.Driver, mockPerson.Object);
        }
Пример #53
0
        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();
        }
Пример #54
0
 public void ShouldBeAbleToRegisterNamedServicesWithDependencyMap()
 {
     var serviceName = "MyService";
     var dependencyMap = new DependencyMap();
     dependencyMap.AddService<IVehicle, Vehicle>(serviceName);
     Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle), serviceName));
 }
Пример #55
0
        public void ShouldCallImplementationInjectorIfItExists()
        {
            var injector = new Mock<IImplementationInjector>();
            var map = new DependencyMap();
            map.Injector = injector.Object;

            injector.Expect(i => i.Inject(It.IsAny<IDependency>(), It.IsAny<IImplementation>())).Returns(new TransientType(typeof(Vehicle), map, new ConstructorResolver()));

            map.AddService<IVehicle, Vehicle>();
            injector.VerifyAll();
        }
Пример #56
0
        private static void RegisterPropertyInjection(DependencyMap map)
        {
            map.AddSingletonService<IServiceA, ServiceA>();
            map.AddSingletonService<IServiceB, ServiceB>();
            map.AddSingletonService<IServiceC, ServiceC>();
            map.AddService(new Func<IMicroContainer, ISubObjectA>(
                microContainer => new SubObjectA { ServiceA = microContainer.GetInstance<IServiceA>() }));
            map.AddService(new Func<IMicroContainer, ISubObjectB>(
                microContainer => new SubObjectB { ServiceB = microContainer.GetInstance<IServiceB>() }));
            map.AddService(new Func<IMicroContainer, ISubObjectC>(
                microContainer => new SubObjectC { ServiceC = microContainer.GetInstance<IServiceC>() }));
            
            // HACK: We must wrap the delegate explicitly in a Func<T, TResult> or else resolving will fail.
            map.AddService(new Func<IMicroContainer, IComplexPropertyObject1>(
                microContainer => new ComplexPropertyObject1
                {
                    ServiceA = microContainer.GetInstance<IServiceA>(),
                    ServiceB = microContainer.GetInstance<IServiceB>(),
                    ServiceC = microContainer.GetInstance<IServiceC>(),
                    SubObjectA = microContainer.GetInstance<ISubObjectA>(),
                    SubObjectB = microContainer.GetInstance<ISubObjectB>(),
                    SubObjectC = microContainer.GetInstance<ISubObjectC>()
                }));

            map.AddService(new Func<IMicroContainer, IComplexPropertyObject2>(
                microContainer => new ComplexPropertyObject2
                {
                    ServiceA = microContainer.GetInstance<IServiceA>(),
                    ServiceB = microContainer.GetInstance<IServiceB>(),
                    ServiceC = microContainer.GetInstance<IServiceC>(),
                    SubObjectA = microContainer.GetInstance<ISubObjectA>(),
                    SubObjectB = microContainer.GetInstance<ISubObjectB>(),
                    SubObjectC = microContainer.GetInstance<ISubObjectC>()
                }));

            map.AddService(new Func<IMicroContainer, IComplexPropertyObject3>(
                microContainer => new ComplexPropertyObject3
                {
                    ServiceA = microContainer.GetInstance<IServiceA>(),
                    ServiceB = microContainer.GetInstance<IServiceB>(),
                    ServiceC = microContainer.GetInstance<IServiceC>(),
                    SubObjectA = microContainer.GetInstance<ISubObjectA>(),
                    SubObjectB = microContainer.GetInstance<ISubObjectB>(),
                    SubObjectC = microContainer.GetInstance<ISubObjectC>()
                }));
        }
Пример #57
0
        public void ShouldHaveTwoEqualDependencyMapsIfBothMapsContainTheSameDependencies()
        {
            var firstMap = new DependencyMap();
            var secondMap = new DependencyMap();

            firstMap.AddService(typeof(IPerson), typeof(Person));
            secondMap.AddService(typeof(IPerson), typeof(Person));

            Assert.AreEqual(firstMap, secondMap);
            Assert.AreEqual(firstMap.GetHashCode(), secondMap.GetHashCode());
        }
Пример #58
0
 private static void RegisterStandard(DependencyMap map)
 {
     map.AddSingletonService<ISingleton, Singleton>();
     map.AddService<ITransient, Transient>();
     map.AddService<ICombined, Combined>();
 }
Пример #59
0
        public void ShouldBeAbleToResolvePlayerInstanceWithoutEncounteringACLRLimitationError()
        {
            var map = new DependencyMap();
            map.AddSingletonService<Game, Game>();
            map.AddService<Player, Player>();
            map.AddService<Gun, Gun>();
            map.AddService<Bullet, Bullet>();

            Func<IMicroContainer, Bullet> createBullet = c => c.GetInstance<Bullet>();
            Func<IMicroContainer, Func<Bullet>> createBulletFunctor = c => () => createBullet(c);
            map.AddService(createBulletFunctor);

            var container = map.CreateContainer();
            Assert.IsNotNull(container);

            var player = container.GetInstance<Player>();
            Assert.IsNotNull(player);

            player.Shoot();
        }
Пример #60
0
        public void should_create_either_both_empty_or_both_equivalent_and_not_null()
        {
            var map1 = new DependencyMapLoader().LoadFrom(typeof(ISomeService).Assembly);
            var container1 = map1.CreateContainer();
            var service1 = container1.GetInstance<ISomeService>();

            var map2 = new DependencyMap();
            map2.AddService<ISomeService,SomeService>();
            var container2 = map2.CreateContainer();
            var service2 = container2.GetInstance<ISomeService>();

            Assert.IsFalse(service2==null||service1==null,"At least one service is null 1:{0} 2:{1}",service1,service2);
            Assert.AreEqual(service1.GetType(),service2.GetType(),"types are not the same");
        }