Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed Mar 13, 2017
1 parent 0539768 commit 0dcf9eb
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/stashbox.tests/AttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void AttributeTests_Resolve_Activator()
container.RegisterType<ITest1, Test11>("test11");
container.RegisterType<ITest1, Test12>("test12");

var inst = container.ActivationContext.Activate(ResolutionInfo.New(), new TypeInformation { Type = typeof(ITest1), DependencyName = "test12" });
var inst = container.ActivationContext.Activate(ResolutionInfo.New(), typeof(ITest1), "test12");
Assert.IsNotNull(inst);
Assert.IsInstanceOfType(inst, typeof(Test12));
}
Expand All @@ -62,7 +62,7 @@ public void AttributeTests_Resolve_Activator_Resolver()
container.RegisterType<ITest1, Test11>("test11");
container.RegisterType<ITest1, Test12>("test12");

var inst = container.ActivationContext.Activate(ResolutionInfo.New(), new TypeInformation { Type = typeof(IEnumerable<ITest1>) });
var inst = container.ActivationContext.Activate(ResolutionInfo.New(), typeof(IEnumerable<ITest1>));
Assert.IsNotNull(inst);
Assert.IsInstanceOfType(inst, typeof(IEnumerable<ITest1>));
Assert.AreEqual(3, ((IEnumerable<ITest1>)inst).Count());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ public interface IRegistrationRepository
/// <summary>
/// Adds or updates an element in the repository.
/// </summary>
/// <param name="typeKey">The key type.</param>
/// <param name="nameKey">The name of the registration.</param>
/// <param name="type">The type.</param>
/// <param name="name">The name of the registration.</param>
/// <param name="canUpdate">Indicates that update is allowed</param>
/// <param name="registration">The registration.</param>
void AddOrUpdateRegistration(Type typeKey, string nameKey, bool canUpdate, IServiceRegistration registration);
void AddOrUpdateRegistration(Type type, string name, bool canUpdate, IServiceRegistration registration);

/// <summary>
/// Retrieves a registration.
/// </summary>
/// <param name="type">The requested type.</param>
/// <param name="name">The requested name.</param>
/// <returns>The registration or null, if it doesn't exist.</returns>
IServiceRegistration GetRegistrationOrDefault(Type type, string name);
IServiceRegistration GetRegistrationOrDefault(Type type, string name = null);

/// <summary>
/// Retrieves a registration.
Expand Down
5 changes: 3 additions & 2 deletions src/stashbox/Infrastructure/Resolution/IActivationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public interface IActivationContext
/// Activates a type.
/// </summary>
/// <param name="resolutionInfo">The resolution info.</param>
/// <param name="typeInfo">The type info.</param>
/// <param name="type">The type.</param>
/// <param name="name">The service name.</param>
/// <returns>The resolved object.</returns>
object Activate(ResolutionInfo resolutionInfo, TypeInformation typeInfo);
object Activate(ResolutionInfo resolutionInfo, Type type, string name = null);

/// <summary>
/// Activates a type via a delegate.
Expand Down
6 changes: 3 additions & 3 deletions src/stashbox/Registration/RegistrationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public RegistrationRepository(IContainerConfigurator containerConfigurator)
this.conditionalRepository = new ConcurrentTree<Type, ConcurrentTree<string, IServiceRegistration>>();
}

public void AddOrUpdateRegistration(Type typeKey, string nameKey, bool canUpdate, IServiceRegistration registration)
public void AddOrUpdateRegistration(Type type, string name, bool canUpdate, IServiceRegistration registration)
{
this.AddOrUpdateRegistration(typeKey, nameKey, canUpdate, registration,
this.AddOrUpdateRegistration(type, name, canUpdate, registration,
registration.HasCondition ? this.conditionalRepository : this.serviceRepository);
}

public IServiceRegistration GetRegistrationOrDefault(Type type, string name) =>
public IServiceRegistration GetRegistrationOrDefault(Type type, string name = null) =>
name != null ? this.GetNamedRegistrationOrDefault(type, name) : this.GetDefaultRegistrationOrDefault(type);

public IServiceRegistration GetRegistrationOrDefault(TypeInformation typeInfo, bool checkConditions = false)
Expand Down
22 changes: 3 additions & 19 deletions src/stashbox/Resolution/ActivationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ public ActivationContext(IContainerContext containerContext, IResolverSelector r
public object Activate(Type type, string name = null)
{
var cachedFactory = this.containerContext.DelegateRepository.GetDelegateCacheOrDefault(type, name);
return cachedFactory != null ? cachedFactory() : this.ActivateType(ResolutionInfo.New(), type, name);
return cachedFactory != null ? cachedFactory() : this.Activate(ResolutionInfo.New(), type, name);
}

public Delegate ActivateFactory(Type type, Type[] parameterTypes, string name = null)
{
var cachedFactory = this.containerContext.DelegateRepository.GetFactoryDelegateCacheOrDefault(type, parameterTypes, name);
return cachedFactory ?? ActivateFactoryDelegate(type, parameterTypes, name);
}

public object Activate(ResolutionInfo resolutionInfo, TypeInformation typeInfo) =>
this.ActivateType(resolutionInfo, typeInfo);

private object ActivateType(ResolutionInfo resolutionInfo, Type type, string name)

public object Activate(ResolutionInfo resolutionInfo, Type type, string name)
{
var registration = this.containerContext.RegistrationRepository.GetRegistrationOrDefault(type, name);
if (registration != null)
Expand All @@ -53,19 +50,6 @@ private object ActivateType(ResolutionInfo resolutionInfo, Type type, string nam
return factory();
}

private object ActivateType(ResolutionInfo resolutionInfo, TypeInformation typeInformation)
{
var registration = this.containerContext.RegistrationRepository.GetRegistrationOrDefault(typeInformation);
if (registration != null)
return registration.GetExpression(resolutionInfo, typeInformation.Type).CompileDelegate()();

var expr = this.resolverSelector.GetResolverExpression(containerContext, typeInformation, resolutionInfo);
if (expr == null)
throw new ResolutionFailedException(typeInformation.Type.FullName);

return expr.CompileDelegate()();
}

private Delegate ActivateFactoryDelegate(Type type, Type[] parameterTypes, string name = null)
{
var resolutionInfo = new ResolutionInfo
Expand Down

0 comments on commit 0dcf9eb

Please sign in to comment.