Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Csajtai committed May 9, 2017
1 parent 205ebac commit 93618e7
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 15 deletions.
13 changes: 13 additions & 0 deletions src/stashbox.tests/DecoratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,19 @@ public void DecoratorTests_RemapDecorator_V2()
}
}

[TestMethod]
public void DecoratorTests_Service_ImplementationType()
{
using (var container = new StashboxContainer())
{
container.RegisterDecorator<ITest1, TestDecorator1>(context =>
{
Assert.AreEqual(typeof(ITest1), context.ServiceType);
Assert.AreEqual(typeof(TestDecorator1), context.ImplementationType);
});
}
}

public interface ITest1 { ITest1 Test { get; } }

public interface IDecoratorDep { }
Expand Down
2 changes: 1 addition & 1 deletion src/stashbox.tests/WireUpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void InjectionMemberTests_WireUp_WithoutService()
{
container.RegisterType<ITest, Test>();
var test1 = new Test1();
container.WireUpAs(test1);
container.WireUp(test1);
var inst = container.Resolve<Test1>();

Assert.IsNotNull(inst);
Expand Down
9 changes: 9 additions & 0 deletions src/stashbox/Infrastructure/IDependencyRegistrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ IDependencyRegistrator RegisterType<TTo>(Action<IFluentServiceRegistrator> confi
/// <returns>The <see cref="IDependencyRegistrator"/> which on this method was called.</returns>
IDependencyRegistrator WireUp(Type serviceType, object instance, object name = null, bool withoutDisposalTracking = false);

/// <summary>
/// Registers an already constructed instance, but the container will perform injections and extensions on it.
/// </summary>
/// <param name="instance">The constructed object.</param>
/// <param name="name">The name of the registration.</param>
/// <param name="withoutDisposalTracking">If it's set to true the container will exclude the instance from the disposal tracking.</param>
/// <returns>The <see cref="IDependencyRegistrator"/> which on this method was called.</returns>
IDependencyRegistrator WireUp(object instance, object name = null, bool withoutDisposalTracking = false);

/// <summary>
/// Registers a type with singleton lifetime.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/stashbox/Infrastructure/Registration/IBaseFluentRegistrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ namespace Stashbox.Infrastructure.Registration
/// </summary>
public interface IBaseFluentRegistrator<out TFluentRegistrator>
{
/// <summary>
/// The service type.
/// </summary>
Type ServiceType { get; }

/// <summary>
/// The implementation type.
/// </summary>
Type ImplementationType { get; }

/// <summary>
/// Sets injection parameters for the registration.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ namespace Stashbox.Infrastructure.Registration
/// </summary>
public interface IFluentServiceRegistrator : IBaseFluentRegistrator<IFluentServiceRegistrator>
{
/// <summary>
/// The service type.
/// </summary>
Type ServiceType { get; }

/// <summary>
/// The implementation type.
/// </summary>
Type ImplementationType { get; }

/// <summary>
/// Sets the lifetime of the registration.
/// </summary>
Expand Down
12 changes: 8 additions & 4 deletions src/stashbox/Registration/DecoratorRegistrationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ internal class DecoratorRegistrationContext : IDecoratorRegistrationContext
private readonly IServiceRegistrator serviceRegistrator;
private bool replaceExistingRegistration;

public Type ServiceType => this.registrationContext.ServiceType;

public Type ImplementationType => this.registrationContext.ImplementationType;

public DecoratorRegistrationContext(RegistrationContext registrationContext, IServiceRegistrator serviceRegistrator)
{
this.registrationContext = registrationContext;
this.serviceRegistrator = serviceRegistrator;
}

public IStashboxContainer Register() => this.serviceRegistrator.Register(this.registrationContext, true, this.replaceExistingRegistration);

public IStashboxContainer ReMap() => this.serviceRegistrator.ReMap(this.registrationContext, true);

public IFluentDecoratorRegistrator WithInjectionParameters(params InjectionParameter[] injectionParameters)
{
this.registrationContext.WithInjectionParameters(injectionParameters);
Expand All @@ -44,10 +52,6 @@ public IFluentDecoratorRegistrator WithoutDisposalTracking()
return this;
}

public IStashboxContainer Register() => this.serviceRegistrator.Register(this.registrationContext, true, this.replaceExistingRegistration);

public IStashboxContainer ReMap() => this.serviceRegistrator.ReMap(this.registrationContext, true);

public IFluentDecoratorRegistrator ReplaceExisting()
{
this.replaceExistingRegistration = true;
Expand Down
4 changes: 4 additions & 0 deletions src/stashbox/StashboxContainer.Registrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public IDependencyRegistrator RegisterInstance(Type serviceType, object instance
public IDependencyRegistrator WireUpAs<TFrom>(TFrom instance, object name = null, bool withoutDisposalTracking = false) =>
this.WireUp(typeof(TFrom), instance, name, withoutDisposalTracking);

/// <inheritdoc />
public IDependencyRegistrator WireUp(object instance, object name = null, bool withoutDisposalTracking = false) =>
this.WireUp(instance.GetType(), instance, name, withoutDisposalTracking);

/// <inheritdoc />
public IDependencyRegistrator WireUp(Type serviceType, object instance, object name = null, bool withoutDisposalTracking = false)
{
Expand Down

0 comments on commit 93618e7

Please sign in to comment.