This commit is contained in:
Eugene Sadovoi 2017-11-14 20:07:10 -05:00
Родитель 84efc8cc8c
Коммит 7fd1b3bf00
3 изменённых файлов: 30 добавлений и 46 удалений

Просмотреть файл

@ -1,13 +1,13 @@
<Project>
<PropertyGroup>
<Version>2.0.2</Version>
<Version>2.0.3</Version>
<PackageReleaseNotes>This package is compatible with .NET 4.5, and 4.7 frameworks.</PackageReleaseNotes>
</PropertyGroup>
<PropertyGroup>
<UnityContainerVersion>5.2.0</UnityContainerVersion>
<UnityAbstractionsVersion>2.2.0</UnityAbstractionsVersion>
<UnityAbstractionsVersion>2.2</UnityAbstractionsVersion>
<UnityContainerVersion>5.2</UnityContainerVersion>
</PropertyGroup>
</Project>

Просмотреть файл

@ -72,25 +72,33 @@ namespace Unity.RegistrationByConvention
foreach (var type in types ?? throw new ArgumentNullException(nameof(types)))
{
var fromTypes = getFromTypes(type);
var fromTypes = getFromTypes(type)?.ToArray() ?? new Type[0];
var name = getName(type);
var lifetimeManager = getLifetimeManager(type);
var injectionMembers = getInjectionMembers(type).ToArray();
var injectionMembers = getInjectionMembers(type)?.ToArray();
foreach (var fromType in fromTypes.Where(t => t != typeof(IDisposable)))
if (0 < fromTypes.Length)
{
if (!overwriteExistingMappings)
foreach (var fromType in fromTypes.Where(t => t != typeof(IDisposable)))
{
var key = new NamedTypeBuildKey(fromType, name);
if (mappings.TryGetValue(key, out var currentMappedToType) && (type != currentMappedToType))
if (!overwriteExistingMappings)
{
throw new DuplicateTypeMappingException(name, fromType, currentMappedToType, type);
var key = new NamedTypeBuildKey(fromType, name);
if (mappings.TryGetValue(key, out var currentMappedToType) && (type != currentMappedToType))
{
throw new DuplicateTypeMappingException(name, fromType, currentMappedToType, type);
}
mappings[key] = type;
}
mappings[key] = type;
container.RegisterType(fromType, type, name, getLifetimeManager(type), injectionMembers);
}
container.RegisterType(fromType, type, name, lifetimeManager, injectionMembers);
}
else
{
var lifetimeManager = getLifetimeManager(type);
if (null != lifetimeManager || (null != injectionMembers && 0 < injectionMembers.Length))
container.RegisterType(null, type, name, lifetimeManager, injectionMembers);
}
}

Просмотреть файл

@ -79,20 +79,14 @@ namespace Microsoft.Practices.Unity.Tests
var registrations = container.Registrations.Where(r => r.MappedToType == typeof(MockLogger)).ToArray();
Assert.AreEqual(2, registrations.Length);
Assert.AreEqual(1, registrations.Length);
var mappingRegistration = registrations.Single(r => r.RegisteredType == typeof(ILogger));
var implementationRegistration = registrations.Single(r => r.RegisteredType == typeof(MockLogger));
Assert.AreSame(typeof(ILogger), mappingRegistration.RegisteredType);
Assert.AreSame(typeof(MockLogger), mappingRegistration.MappedToType);
Assert.AreEqual("name", mappingRegistration.Name);
Assert.IsInstanceOfType(mappingRegistration.LifetimeManager, typeof(ContainerControlledLifetimeManager));
Assert.AreSame(typeof(MockLogger), implementationRegistration.RegisteredType);
Assert.AreSame(typeof(MockLogger), implementationRegistration.MappedToType);
Assert.AreEqual("name", implementationRegistration.Name);
Assert.IsInstanceOfType(implementationRegistration.LifetimeManager, typeof(ContainerControlledLifetimeManager));
}
[TestMethod]
@ -103,20 +97,14 @@ namespace Microsoft.Practices.Unity.Tests
var registrations = container.Registrations.Where(r => r.MappedToType == typeof(MockLogger)).ToArray();
Assert.AreEqual(2, registrations.Length);
Assert.AreEqual(1, registrations.Length);
var mappingRegistration = registrations.Single(r => r.RegisteredType == typeof(ILogger));
var implementationRegistration = registrations.Single(r => r.RegisteredType == typeof(MockLogger));
Assert.AreSame(typeof(ILogger), mappingRegistration.RegisteredType);
Assert.AreSame(typeof(MockLogger), mappingRegistration.MappedToType);
Assert.AreEqual("name", mappingRegistration.Name);
Assert.IsInstanceOfType(mappingRegistration.LifetimeManager, typeof(ContainerControlledLifetimeManager));
Assert.AreSame(typeof(MockLogger), implementationRegistration.RegisteredType);
Assert.AreSame(typeof(MockLogger), implementationRegistration.MappedToType);
Assert.AreEqual("name", implementationRegistration.Name);
Assert.IsInstanceOfType(implementationRegistration.LifetimeManager, typeof(ContainerControlledLifetimeManager));
}
[TestMethod]
@ -126,20 +114,14 @@ namespace Microsoft.Practices.Unity.Tests
container.RegisterTypes(AllClasses.FromAssemblies(typeof(MockLogger).GetTypeInfo().Assembly).Where(t => t == typeof(MockLogger)), WithMappings.FromAllInterfaces, WithName.Default, WithLifetime.ContainerControlled);
var registrations = container.Registrations.Where(r => r.MappedToType == typeof(MockLogger)).ToArray();
Assert.AreEqual(2, registrations.Length);
Assert.AreEqual(1, registrations.Length);
var mappingRegistration = registrations.Single(r => r.RegisteredType == typeof(ILogger));
var implementationRegistration = registrations.Single(r => r.RegisteredType == typeof(MockLogger));
Assert.AreSame(typeof(ILogger), mappingRegistration.RegisteredType);
Assert.AreSame(typeof(MockLogger), mappingRegistration.MappedToType);
Assert.AreEqual(null, mappingRegistration.Name);
Assert.IsInstanceOfType(mappingRegistration.LifetimeManager, typeof(ContainerControlledLifetimeManager));
Assert.AreSame(typeof(MockLogger), implementationRegistration.RegisteredType);
Assert.AreSame(typeof(MockLogger), implementationRegistration.MappedToType);
Assert.AreEqual(null, implementationRegistration.Name);
Assert.IsInstanceOfType(implementationRegistration.LifetimeManager, typeof(ContainerControlledLifetimeManager));
}
[TestMethod]
@ -149,20 +131,14 @@ namespace Microsoft.Practices.Unity.Tests
container.RegisterTypes(AllClasses.FromLoadedAssemblies(), WithMappings.FromAllInterfaces, WithName.TypeName, WithLifetime.ContainerControlled, overwriteExistingMappings: true);
var registrations = container.Registrations.Where(r => r.MappedToType == typeof(MockLogger)).ToArray();
Assert.AreEqual(2, registrations.Length);
Assert.AreEqual(1, registrations.Length);
var mappingRegistration = registrations.Single(r => r.RegisteredType == typeof(ILogger));
var implementationRegistration = registrations.Single(r => r.RegisteredType == typeof(MockLogger));
Assert.AreSame(typeof(ILogger), mappingRegistration.RegisteredType);
Assert.AreSame(typeof(MockLogger), mappingRegistration.MappedToType);
Assert.AreEqual("MockLogger", mappingRegistration.Name);
Assert.IsInstanceOfType(mappingRegistration.LifetimeManager, typeof(ContainerControlledLifetimeManager));
Assert.AreSame(typeof(MockLogger), implementationRegistration.RegisteredType);
Assert.AreSame(typeof(MockLogger), implementationRegistration.MappedToType);
Assert.AreEqual("MockLogger", implementationRegistration.Name);
Assert.IsInstanceOfType(implementationRegistration.LifetimeManager, typeof(ContainerControlledLifetimeManager));
}
public void CanResolveTypeRegisteredWithAllInterfaces()
@ -195,7 +171,7 @@ namespace Microsoft.Practices.Unity.Tests
var registrations = container.Registrations.Where(r => r.MappedToType == typeof(MockLogger) || r.MappedToType == typeof(SpecialLogger)).ToArray();
Assert.AreEqual(4, registrations.Length);
Assert.AreEqual(2, registrations.Length);
}
[TestMethod]
@ -235,12 +211,12 @@ namespace Microsoft.Practices.Unity.Tests
var container = new UnityContainer();
container.RegisterType<object, string>("string");
container.RegisterInstance("string", "a string");
container.RegisterInstance("int", 42);
container.RegisterInstance<int>("int", 42);
container.RegisterTypes(new[] { typeof(int) }, t => new[] { typeof(object) }, t => "int");
Assert.AreNotEqual("a string", container.Resolve<object>("string"));
Assert.AreNotEqual(42, container.Resolve<object>("int"));
Assert.AreEqual("a string", container.Resolve<object>("string"));
Assert.AreEqual(42, container.Resolve<object>("int"));
}
[TestMethod]