Skip to content

Commit

Permalink
added fialing test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Perry authored and jeremydmiller committed Feb 20, 2017
1 parent b24788c commit bd2b113
Showing 1 changed file with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using StructureMap.Pipeline;
using Xunit;

namespace StructureMap.Testing.Bugs
{
public class OpenGenericDecoratorsWithConstructorPolicy_Error
{
[Fact]
public void applies_the_constructor_policy_to_decorated_instances()
{
var container = new Container(_ =>
{
_.For(typeof(ICmdHandler<>)).DecorateAllWith(typeof(CmdDecorator<>));
_.Policies.Add<TestLoggerConstructorPolicy>();
_.Scan(a =>
{
a.TheCallingAssembly();
a.ConnectImplementationsToTypesClosing(typeof(ICmdHandler<>));
});
});

Console.WriteLine(container.WhatDoIHave());

var handler = container.GetInstance<ICmdHandler<TestCmd>>();
}
}

public interface ICmdHandler<T>
{
void Handle(T cmd);
}

public class CmdHandler : ICmdHandler<TestCmd>
{
public void Handle(TestCmd cmd)
{
}
}

public class TestCmd { }

public class CmdDecorator<T> : ICmdHandler<T>
{
private readonly ICmdHandler<T> _decorated;
private readonly ITestLogger _logger;

public CmdDecorator(ICmdHandler<T> decorated, ITestLogger logger)
{
_decorated = decorated;
_logger = logger;
}

public void Handle(T cmd)
{
_decorated.Handle(cmd);
}
}

public interface ITestLogger
{
}

public class TestLogger
{
}

public class TestLoggerConstructorPolicy : ConfiguredInstancePolicy
{
protected override void apply(Type pluginType, IConfiguredInstance instance)
{
var param = instance.Constructor.GetParameters().FirstOrDefault(x => x.ParameterType == typeof(ITestLogger));
if (param != null)
{
var logger = new TestLogger();
instance.Dependencies.AddForConstructorParameter(param, logger);
}
else
{
// Try to inject an ILogger via public-settable property.
var prop = instance.SettableProperties().FirstOrDefault(x => x.PropertyType == typeof(ITestLogger));
if (prop != null)
{
var logger = new TestLogger();
instance.Dependencies.AddForProperty(prop, logger);
}
}
}
}
}

0 comments on commit bd2b113

Please sign in to comment.