Skip to content

Commit

Permalink
adding priority for fluent decorators issue #255
Browse files Browse the repository at this point in the history
  • Loading branch information
ipjohnson committed Apr 4, 2020
1 parent 7c9cb79 commit 4bd8dcd
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ public interface IFluentDecoratorStrategyConfiguration
IWhenConditionConfiguration<IFluentDecoratorStrategyConfiguration> When { get; }

/// <summary>
/// Add a specific value for a particuar parameter in the constructor
/// Sets priority for decorator
/// </summary>
/// <param name="priority"></param>
/// <returns></returns>
IFluentDecoratorStrategyConfiguration Priority(int priority);

/// <summary>
/// Add a specific value for a particular parameter in the constructor
/// </summary>
/// <typeparam name="TParam">type of parameter</typeparam>
/// <param name="paramValue">Func(T) value for the parameter</param>
/// <returns>configuration object</returns>
IFluentDecoratorWithCtorConfiguration<TParam> WithCtorParam<TParam>(Func<TParam> paramValue = null);

/// <summary>
/// Add a specific value for a particuar parameter in the constructor
/// Add a specific value for a particular parameter in the constructor
/// </summary>
/// <typeparam name="TParam">type of parameter</typeparam>
/// <typeparam name="TArg1"></typeparam>
Expand All @@ -43,7 +50,7 @@ public interface IFluentDecoratorStrategyConfiguration
IFluentDecoratorWithCtorConfiguration<TParam> WithCtorParam<TArg1, TParam>(Func<TArg1, TParam> paramValue);

/// <summary>
/// Add a specific value for a particuar parameter in the constructor
/// Add a specific value for a particular parameter in the constructor
/// </summary>
/// <typeparam name="TParam">type of parameter</typeparam>
/// <typeparam name="TArg1"></typeparam>
Expand All @@ -53,7 +60,7 @@ public interface IFluentDecoratorStrategyConfiguration
IFluentDecoratorWithCtorConfiguration<TParam> WithCtorParam<TArg1, TArg2, TParam>(Func<TArg1, TArg2, TParam> paramValue);

/// <summary>
/// Add a specific value for a particuar parameter in the constructor
/// Add a specific value for a particular parameter in the constructor
/// </summary>
/// <typeparam name="TParam">type of parameter</typeparam>
/// <typeparam name="TArg1"></typeparam>
Expand All @@ -64,7 +71,7 @@ public interface IFluentDecoratorStrategyConfiguration
IFluentDecoratorWithCtorConfiguration<TParam> WithCtorParam<TArg1, TArg2, TArg3, TParam>(Func<TArg1, TArg2, TArg3, TParam> paramValue);

/// <summary>
/// Add a specific value for a particuar parameter in the constructor
/// Add a specific value for a particular parameter in the constructor
/// </summary>
/// <typeparam name="TParam">type of parameter</typeparam>
/// <typeparam name="TArg1"></typeparam>
Expand All @@ -76,7 +83,7 @@ public interface IFluentDecoratorStrategyConfiguration
IFluentDecoratorWithCtorConfiguration<TParam> WithCtorParam<TArg1, TArg2, TArg3, TArg4, TParam>(Func<TArg1, TArg2, TArg3, TArg4, TParam> paramValue);

/// <summary>
/// Add a specific value for a particuar parameter in the constructor
/// Add a specific value for a particular parameter in the constructor
/// </summary>
/// <typeparam name="TParam">type of parameter</typeparam>
/// <typeparam name="TArg1"></typeparam>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public IFluentDecoratorStrategyConfiguration As(Type type)
public IWhenConditionConfiguration<IFluentDecoratorStrategyConfiguration> When
=> new WhenConditionConfiguration<IFluentDecoratorStrategyConfiguration>(_strategy.AddCondition, this);

/// <inheritdoc />
public IFluentDecoratorStrategyConfiguration Priority(int priority)
{
_strategy.Priority = priority;

return this;
}

/// <summary>
/// Add a specific value for a particuar parameter in the constructor
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public IFluentDecoratorStrategyConfiguration As(Type type)
/// </summary>
public IWhenConditionConfiguration<IFluentDecoratorStrategyConfiguration> When => _configuration.When;


/// <inheritdoc />
public IFluentDecoratorStrategyConfiguration Priority(int priority)
{
return _configuration.Priority(priority);
}

/// <summary>
/// Add a specific value for a particuar parameter in the constructor
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
using System.Text;
using Grace.DependencyInjection;
using Grace.Tests.Classes.Simple;
using Xunit;

namespace Grace.Tests.DependencyInjection.Decorator
{
public class PriorityDecoratorTests
{
[Fact]
public void DecoratorInOrderTest()
{
var container = new DependencyInjectionContainer{
_ =>
{
_.ExportFactory<IBasicService>(() => new BasicService {Count = 5});
_.ExportDecorator(typeof(DecoratorOne)).As(typeof(IBasicService));
_.ExportDecorator(typeof(DecoratorTwo)).As(typeof(IBasicService));
}
};

var instance = container.Locate<IBasicService>();

Assert.IsType<DecoratorTwo>(instance);
Assert.Equal(55, instance.Count);
}


[Fact]
public void DecoratorPriorityInOrderTest()
{
var container = new DependencyInjectionContainer{
_ =>
{
_.ExportFactory<IBasicService>(() => new BasicService {Count = 5});
_.ExportDecorator(typeof(DecoratorOne)).As(typeof(IBasicService));
_.ExportDecorator(typeof(DecoratorTwo)).As(typeof(IBasicService)).Priority(10);
}
};

var instance = container.Locate<IBasicService>();

Assert.IsType<DecoratorTwo>(instance);
Assert.Equal(55, instance.Count);
}


[Fact]
public void DecoratorPriorityOutOfOrderTest()
{
var container = new DependencyInjectionContainer{
_ =>
{
_.ExportFactory<IBasicService>(() => new BasicService {Count = 5});
_.ExportDecorator(typeof(DecoratorOne)).As(typeof(IBasicService)).Priority(10);
_.ExportDecorator(typeof(DecoratorTwo)).As(typeof(IBasicService));
}
};

var instance = container.Locate<IBasicService>();

Assert.IsType<DecoratorOne>(instance);
Assert.Equal(100, instance.Count);
}


public class DecoratorOne : IBasicService
{
private IBasicService _basicService;

public DecoratorOne(IBasicService basicService)
{
_basicService = basicService;
}

public int Count
{
get => _basicService.Count * 10;
set => _basicService.Count = value;
}

public int TestMethod()
{
return _basicService.TestMethod();
}
}


public class DecoratorTwo : IBasicService
{
private IBasicService _basicService;

public DecoratorTwo(IBasicService basicService)
{
_basicService = basicService;
}

public int Count
{
get => _basicService.Count + 5;
set => _basicService.Count = value;
}

public int TestMethod()
{
return _basicService.TestMethod();
}
}

}
}

0 comments on commit 4bd8dcd

Please sign in to comment.