Пример #1
0
        public void Cache_The_ImplementationType()
        {
            var manager = new ScannedTypeManager();
            var type    = manager.FindImplementationType(typeof(IMyAwesomeService));

            Assert.AreEqual(typeof(MyAwesomeService), type);

            Assert.AreEqual(1, manager.ResolvedTypes.Count);
            Assert.AreEqual("MvvmLib.IoC.Tests.Scanned.IMyAwesomeService, MvvmLib.IoC.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", typeof(IMyAwesomeService).AssemblyQualifiedName);
            Assert.AreEqual(type, manager.ResolvedTypes[typeof(IMyAwesomeService).AssemblyQualifiedName]);

            var type2 = manager.FindImplementationType(typeof(IMyAwesomeService));

            Assert.AreEqual(type2, type);
        }
Пример #2
0
        public void Cannot_Resolve_Multiple_Implementation_WIthout_Attribute_From_Another_Assembly()
        {
            var manager = new ScannedTypeManager();
            var type    = manager.FindImplementationType(typeof(IMyExternalServiceC));

            Assert.AreEqual(null, type);
        }
Пример #3
0
        public void Resolve_ImplementationType_With_PreferredImplementation_Attribute_From_Another_Assembly()
        {
            var manager = new ScannedTypeManager();
            var type    = manager.FindImplementationType(typeof(IMyExternalServiceB));

            Assert.AreEqual(typeof(MyExternalServiceB2), type);
        }
Пример #4
0
        public void Cannot_Resolve_Multiple_Implementation_WIthout_Attribute()
        {
            var manager = new ScannedTypeManager();
            var type    = manager.FindImplementationType(typeof(IMyAwesomeServiceC));

            Assert.AreEqual(null, type);
        }
Пример #5
0
        public void Resolve_ImplementationType_With_PreferredImplementation_Attribute()
        {
            var manager = new ScannedTypeManager();
            var type    = manager.FindImplementationType(typeof(IMyAwesomeServiceB));

            Assert.AreEqual(typeof(MyAwesomeServiceB2), type);
        }
Пример #6
0
        public void Resolve_ImplementationType()
        {
            var manager = new ScannedTypeManager();
            var type    = manager.FindImplementationType(typeof(IMyAwesomeService));

            Assert.AreEqual(typeof(MyAwesomeService), type);
        }
Пример #7
0
        private void TryRegisterTypeIfNotRegistered(Type type, string name)
        {
            if (!IsRegistered(type, name) && autoDiscovery)
            {
                if (type.IsInterface)
                {
                    var implementationType = scannedTypeManager.FindImplementationType(type);
                    if (implementationType == null)
                    {
                        throw new ResolutionFailedException($"Unable to resolve the type for the interface not registered \"{type.Name}\"");
                    }

                    switch (lifetimeOnDiscovery)
                    {
                    case LifetimeOnDiscovery.Transcient:
                        RegisterType(type, name, implementationType);
                        break;

                    case LifetimeOnDiscovery.SingletonOnlyForServices:
                        RegisterType(type, name, implementationType).AsSingleton();
                        break;
                    }
                }
                else
                {
                    if (IsValueContainerType(type))
                    {
                        throw new ResolutionFailedException($"Invalid registration type \"{type.Name}\"");
                    }

                    RegisterType(type, name);
                }
            }
        }
Пример #8
0
        public void Cache_The_Assembly()
        {
            var manager = new ScannedTypeManager();

            Assert.AreEqual(0, manager.Assemblies.Count);

            var type = manager.FindImplementationType(typeof(IMyAwesomeService));

            Assert.AreEqual(1, manager.Assemblies.Count);
            var a1 = manager.Assemblies.First();

            manager.FindImplementationType(typeof(IMyAwesomeService));
            Assert.AreEqual(1, manager.Assemblies.Count);

            manager.FindImplementationType(typeof(IMyExternalService));
            Assert.AreEqual(2, manager.Assemblies.Count);
        }