Skip to content

Instantly share code, notes, and snippets.

@theit8514
Created March 28, 2017 04:49
Show Gist options
  • Select an option

  • Save theit8514/edd9658cd58d5f708954302e47c60460 to your computer and use it in GitHub Desktop.

Select an option

Save theit8514/edd9658cd58d5f708954302e47c60460 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace LightInject.Tests
{
public class Issue209 : TestBase
{
[Fact]
public void ShouldThrowInvalidOperationException()
{
var container = (ServiceContainer)CreateContainer();
container.Register<IFoo, Foo>();
Assert.Throws<InvalidOperationException>(() => container.GetInstance<IFoo>());
}
[Fact]
public void ShouldResolveViaDependencySelector()
{
var container = (ServiceContainer)CreateContainer();
container.ConstructorDependencySelector = new TestConstructorDependencySelector();
container.PropertyDependencySelector = new TestPropertyDependencySelector();
container.Register<IFoo, Foo>();
var instance = container.GetInstance<IFoo>();
var foo = (Foo)instance;
Assert.IsType(typeof(Bar1), foo.Bar1);
var bar1 = (Bar1)foo.Bar1;
Assert.Equal(typeof(Foo), bar1.Type);
Assert.IsType(typeof(Bar2), foo.Bar2);
var bar2 = (Bar2)foo.Bar2;
Assert.Equal(typeof(Foo), bar2.Type);
}
[Fact]
public void ShouldResolveViaRegister()
{
var container = CreateContainer();
container.RegisterPropertyDependency<IBar1>((factory, info) => new Bar1(info.DeclaringType));
container.RegisterConstructorDependency<IBar2>((factory, info) => new Bar2(info.Member.DeclaringType));
container.Register<IFoo, Foo>();
var instance = container.GetInstance<IFoo>();
var foo = (Foo)instance;
Assert.IsType(typeof(Bar1), foo.Bar1);
var bar1 = (Bar1)foo.Bar1;
Assert.Equal(typeof(Foo), bar1.Type);
Assert.IsType(typeof(Bar2), foo.Bar2);
var bar2 = (Bar2)foo.Bar2;
Assert.Equal(typeof(Foo), bar2.Type);
}
class TestConstructorDependencySelector : ConstructorDependencySelector
{
public override IEnumerable<ConstructorDependency> Execute(ConstructorInfo constructor)
{
ConstructorDependency[] dependencies = base.Execute(constructor).ToArray();
ConstructorDependency dependency = dependencies.FirstOrDefault(d => d.ServiceType == typeof(IBar2));
if (dependency != null)
{
Expression<Func<IBar2>> factoryExpression = () => new Bar2(constructor.DeclaringType);
dependency.FactoryExpression = factoryExpression.Compile();
}
return dependencies;
}
}
class TestPropertyDependencySelector : PropertyDependencySelector
{
public TestPropertyDependencySelector() : base(new PropertySelector()) { }
public override IEnumerable<PropertyDependency> Execute(Type type)
{
PropertyDependency[] dependencies = base.Execute(type).ToArray();
PropertyDependency dependency = dependencies.FirstOrDefault(d => d.ServiceType == typeof(IBar1));
if (dependency != null)
{
Expression<Func<IBar1>> factoryExpression = () => new Bar1(type);
dependency.FactoryExpression = factoryExpression.Compile();
}
return dependencies;
}
}
public interface IFoo
{
}
public interface IBar1
{
}
public interface IBar2
{
}
public class Bar1 : IBar1
{
public Type Type { get; }
public Bar1(Type type)
{
Type = type;
}
}
public class Bar2 : IBar2
{
public Type Type { get; }
public Bar2(Type type)
{
Type = type;
}
}
public class Foo : IFoo
{
public IBar1 Bar1 { get; set; }
private IBar2 _bar2;
public IBar2 Bar2 => _bar2;
public Foo(IBar2 bar2)
{
_bar2 = bar2;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment