Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autofac support #154

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions Lamar.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2036
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.202
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lamar.Testing", "src\Lamar.Testing\Lamar.Testing.csproj", "{865C14C3-4B54-46EC-B394-9BAD73905086}"
EndProject
Expand Down Expand Up @@ -41,15 +41,19 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UserApp", "src\UserApp\User
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Bug-124-Widgets", "Bug-124-Widgets", "{317DED50-F632-48EB-8E02-E1B6739B35E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Widget.Aspect.Logger", "src\Aspect.Logger\Widget.Aspect.Logger.csproj", "{A367AE1C-B39D-438B-BADD-3B7E368DDF97}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Widget.Aspect.Logger", "src\Aspect.Logger\Widget.Aspect.Logger.csproj", "{A367AE1C-B39D-438B-BADD-3B7E368DDF97}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Widget.Instance", "Widget.Instance\Widget.Instance.csproj", "{6F721733-9012-4434-A830-2F5E9A6A926B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Widget.Instance", "Widget.Instance\Widget.Instance.csproj", "{6F721733-9012-4434-A830-2F5E9A6A926B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Widget.Core", "Widget.Core\Widget.Core.csproj", "{178C596D-65D9-42F9-927C-DF63F348DCA5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Widget.Core", "Widget.Core\Widget.Core.csproj", "{178C596D-65D9-42F9-927C-DF63F348DCA5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Widget.Registration", "Widget.Registration\Widget.Registration.csproj", "{39F01E1B-0549-422A-A60F-747C1CDF0C08}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Widget.Registration", "Widget.Registration\Widget.Registration.csproj", "{39F01E1B-0549-422A-A60F-747C1CDF0C08}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LamarCodeGeneration", "src\LamarCodeGeneration\LamarCodeGeneration.csproj", "{2030BA2A-5D44-4BDA-9EF7-7EB0BC7A1C6F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LamarCodeGeneration", "src\LamarCodeGeneration\LamarCodeGeneration.csproj", "{2030BA2A-5D44-4BDA-9EF7-7EB0BC7A1C6F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lamar.AutoFactory", "src\Lamar.AutoFactory\Lamar.AutoFactory.csproj", "{2250C640-88AF-4DA5-A1D1-7BC3717E1D60}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lamar.AutoFactory.Testing", "src\Lamar.AutoFactory.Testing\Lamar.AutoFactory.Testing.csproj", "{EFB244CD-F185-4052-A4B5-4A869682D616}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -145,6 +149,14 @@ Global
{2030BA2A-5D44-4BDA-9EF7-7EB0BC7A1C6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2030BA2A-5D44-4BDA-9EF7-7EB0BC7A1C6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2030BA2A-5D44-4BDA-9EF7-7EB0BC7A1C6F}.Release|Any CPU.Build.0 = Release|Any CPU
{2250C640-88AF-4DA5-A1D1-7BC3717E1D60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2250C640-88AF-4DA5-A1D1-7BC3717E1D60}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2250C640-88AF-4DA5-A1D1-7BC3717E1D60}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2250C640-88AF-4DA5-A1D1-7BC3717E1D60}.Release|Any CPU.Build.0 = Release|Any CPU
{EFB244CD-F185-4052-A4B5-4A869682D616}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EFB244CD-F185-4052-A4B5-4A869682D616}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EFB244CD-F185-4052-A4B5-4A869682D616}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EFB244CD-F185-4052-A4B5-4A869682D616}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
214 changes: 214 additions & 0 deletions src/Lamar.AutoFactory.Testing/AutoFactoryTester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
using System;
using System.Collections.Generic;
using Shouldly;
using Xunit;

namespace Lamar.AutoFactory.Testing
{
public class AutoFactoryTester
{
private Container container;

public AutoFactoryTester()
{
}

[Fact]
public void Can_build_the_factory()
{
container = new Container(cfg =>
{
cfg.For<IDummyService>().Use<DummyService>();
cfg.For<IDummyFactory>().CreateFactory();
});

var factory = container.GetInstance<IDummyFactory>();

factory.ShouldNotBeNull();
}

[Fact]
public void Can_resolve_component()
{
container = new Container(cfg =>
{
cfg.For<IDummyService>().Use<DummyService>();
cfg.For<IDummyFactory>().CreateFactory();
});

var factory = container.GetInstance<IDummyFactory>();

var component = factory.CreateDummyService();

component.ShouldNotBeNull();
component.ShouldBeOfType<DummyService>();
}

[Fact]
public void Can_resolve_generic_components_via_a_generic_method()
{
container = new Container(cfg =>
{
cfg.For<IDummyService>().Use<DummyService>();
cfg.For<IDummyFactory>().CreateFactory();
});

var factory = container.GetInstance<IDummyFactory>();

var component = factory.CreateService<IDummyService>();

component.ShouldNotBeNull();
component.ShouldBeOfType<DummyService>();
}

[Fact]
public void Can_resolve_a_closed_generic_return_type()
{
container = new Container(cfg =>
{
cfg.For<IHandler<Message>>().Use<MessageHandler>();
cfg.For<IDummyFactory>().CreateFactory();
});

var factory = container.GetInstance<IDummyFactory>();

var component = factory.CreateHandler<Message>();

component.ShouldNotBeNull();
component.ShouldBeOfType<MessageHandler>();
}

[Fact]
public void ResolveServiceWithRedundantExplicitArguments()
{
container = new Container(cfg =>
{
cfg.For<IDummyService>().Use<DummyService>();
cfg.For<IDummyFactory>().CreateFactory();
});

var factory = container.GetInstance<IDummyFactory>();

var component = factory.CreateDummyService("John", "Smith");

component.ShouldSatisfyAllConditions(
() => component.ShouldNotBeNull(),
() => component.ShouldBeOfType<DummyService>(),
() => component.Name.ShouldBeNull()
);
}

[Fact]
public void TryToResolveNotRegisteredNamedService()
{
container = new Container(cfg =>
{
cfg.For<IDummyService>().Use<DummyService>();
cfg.For<IDummyService>().Add<DummyServiceWithName>().Named("direct");
cfg.For<IDummyService>().Add<DummyServiceWithReversedName>().Named("reversed");
cfg.For<IDummyFactory>().CreateFactory();
});

var factory = container.GetInstance<IDummyFactory>();

var component = factory.GetNamedDummyService("unknown", "John", "Smith");

component.ShouldBeNull();
}

[Fact]
public void ResolveServiceNames()
{
container = new Container(cfg =>
{
cfg.For<IDummyService>().Use<DummyService>();
cfg.For<IDummyService>().Add<DummyServiceWithName>().Named("direct");
cfg.For<IDummyService>().Add<DummyServiceWithReversedName>().Named("reversed");
cfg.For<IDummyFactory>().CreateFactory();
});

var factory = container.GetInstance<IDummyFactory>();

var names = factory.GetNames<IDummyService>();

names.ShouldBe(new[] { string.Empty, "direct", "reversed" }, true);
}
}

public interface IHandler<T>
{
void Handle(T thing);
}

public class Message
{
}

public class MessageHandler : IHandler<Message>
{
public void Handle(Message thing)
{
}
}

// SAMPLE: IDummyService
public interface IDummyService
{
string Name { get; }
}

// ENDSAMPLE

// SAMPLE: DummyService
public class DummyService : IDummyService
{
public string Name { get; set; }
}

// ENDSAMPLE

public class DummyServiceWithName : IDummyService
{
public DummyServiceWithName(string namePart1, string namePart2)
{
Name = string.Format("{0} {1}", namePart1, namePart2);
}

public string Name { get; set; }
}

public class DummyServiceWithReversedName : IDummyService
{
public DummyServiceWithReversedName(string namePart1, string namePart2)
{
Name = string.Format("{0} {1}", namePart2, namePart1);
}

public string Name { get; set; }
}

// SAMPLE: IDummyFactory
public interface IDummyFactory
{
// This method will return the names of all registered implementations of TService.
IList<string> GetNames<TService>();

// This method will just create the default IDummyService implementation.
IDummyService CreateDummyService();

// This method will just create the default IDummyService implementation and pass namePart1 and namePart2 as
// dependencies.
IDummyService CreateDummyService(string namePart1, string namePart2);

// This method will create IDummyService implementation with serviceName name.
IDummyService GetNamedDummyService(string serviceName, string namePart1, string namePart2);

// Generic methods are also allowed as factory methods.
TService CreateService<TService>();

// Something that is common for event-sourcing implementations.
IHandler<TMessage> CreateHandler<TMessage>();
}

// ENDSAMPLE
}
26 changes: 26 additions & 0 deletions src/Lamar.AutoFactory.Testing/Lamar.AutoFactory.Testing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>Lamar.AutoFactory.Testing</AssemblyName>
<PackageId>Lamar.AutoFactory.Testing</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Lamar.AutoFactory\Lamar.AutoFactory.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="Shouldly" Version="2.8.3" />
<PackageReference Include="Moq" Version="4.8.1" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions src/Lamar.AutoFactory.Testing/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("StructureMap.AutoFactory.Testing")]
[assembly: AssemblyTrademark("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("fa9f954b-ee4a-4172-b38b-a342df9ec15f")]
59 changes: 59 additions & 0 deletions src/Lamar.AutoFactory.Testing/ProxyFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using Castle.DynamicProxy;
using Moq;
using Shouldly;
using Xunit;

namespace Lamar.AutoFactory.Testing
{
public class ProxyFactoryTests
{
public interface IFactory
{
IService CreateService();
}

public interface IService
{
}

public class Service : IService
{
}

[Fact]
public void Should_create_a_proxy_factory()
{
var proxyGenerator = new ProxyGenerator();
var context = new Mock<IServiceContext>().Object;

var proxyFactory = new ProxyFactory<IFactory>(proxyGenerator, context, new DefaultAutoFactoryConventionProvider());

var factory = proxyFactory.Create();

factory.ShouldNotBeNull();
}

[Fact]
public void Should_build_the_service_by_return_type_from_context()
{
var proxyGenerator = new ProxyGenerator();
var contextMock = new Mock<IServiceContext>();
var containerMock = new Mock<IContainer>();

var service = new Service();

contextMock.Setup(x => x.GetInstance<IContainer>()).Returns(containerMock.Object);
containerMock.Setup(x => x.TryGetInstance(typeof(IService))).Returns(service);

var proxyFactory = new ProxyFactory<IFactory>(proxyGenerator, contextMock.Object, new DefaultAutoFactoryConventionProvider());

var factory = proxyFactory.Create();

var built = factory.CreateService();

built.ShouldNotBeNull();
built.ShouldBe(service);
}
}
}
Loading