Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmus committed Apr 27, 2015
2 parents 6b5a78a + 62910d1 commit 970b422
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 101 deletions.
18 changes: 18 additions & 0 deletions Source/EventFlow.Autofac/EventFlow.Autofac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\EventFlow\Configuration\Registrations\AutofacDecorator.cs">
<Link>Registrations\AutofacDecorator.cs</Link>
</Compile>
<Compile Include="..\EventFlow\Configuration\Registrations\AutofacRegistration.cs">
<Link>Registrations\AutofacRegistration.cs</Link>
</Compile>
<Compile Include="..\EventFlow\Configuration\Registrations\AutofacResolver.cs">
<Link>Registrations\AutofacResolver.cs</Link>
</Compile>
<Compile Include="..\EventFlow\Configuration\Registrations\AutofacRootResolver.cs">
<Link>Registrations\AutofacRootResolver.cs</Link>
</Compile>
<Compile Include="..\EventFlow\Configuration\Registrations\AutofacScopeResolver.cs">
<Link>Registrations\AutofacScopeResolver.cs</Link>
</Compile>
<Compile Include="..\EventFlow\Configuration\Registrations\AutofacServiceRegistration.cs">
<Link>Registrations\AutofacServiceRegistration.cs</Link>
</Compile>
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
using System;
using Autofac;
using EventFlow.Configuration.Registrations;
using EventFlow.Configuration.Registrations.Resolvers;

namespace EventFlow.Autofac.Extensions
{
Expand Down
4 changes: 4 additions & 0 deletions Source/EventFlow.Owin.Tests/EventFlow.Owin.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EventFlow.Autofac\EventFlow.Autofac.csproj">
<Project>{26f06682-3364-4c22-b9b2-2f2653d0be0d}</Project>
<Name>EventFlow.Autofac</Name>
</ProjectReference>
<ProjectReference Include="..\EventFlow.Owin\EventFlow.Owin.csproj">
<Project>{ee6f7b78-3ef1-488f-b90a-8e7f350b7d51}</Project>
<Name>EventFlow.Owin</Name>
Expand Down
15 changes: 6 additions & 9 deletions Source/EventFlow.Owin.Tests/IntegrationTests/Site/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
using System.Web.Http.ExceptionHandling;
using Autofac;
using Autofac.Integration.WebApi;
using EventFlow.Autofac.Extensions;
using EventFlow.Configuration.Registrations;
using EventFlow.Configuration.Registrations.Resolvers;
using EventFlow.EventStores.Files;
using EventFlow.Extensions;
using EventFlow.Logs;
Expand Down Expand Up @@ -85,19 +85,16 @@ public void Configuration(IAppBuilder appBuilder)
Path.GetTempPath(),
Guid.NewGuid().ToString());

var resolver = EventFlowOptions.New
.UseServiceRegistration(new AutofacServiceRegistration(containerBuilder))
var container = EventFlowOptions.New
.UseAutofacContainerBuilder(containerBuilder)
.AddEvents(EventFlowTest.Assembly)
.AddCommandHandlers(EventFlowTest.Assembly)
.AddOwinMetadataProviders()
.UseFilesEventStore(FilesEventStoreConfiguration.Create(storePath))
.RegisterServices(f => f.Register(r => new DirectoryCleaner(storePath), Lifetime.Singleton))
.CreateResolver(false);
.CreateContainer(false);

resolver.Resolve<DirectoryCleaner>();

var autofacRootResolver = (AutofacRootResolver) resolver;
var container = autofacRootResolver.Container;
container.Resolve<DirectoryCleaner>();

var config = new HttpConfiguration
{
Expand All @@ -106,7 +103,7 @@ public void Configuration(IAppBuilder appBuilder)
};
config.MapHttpAttributeRoutes();
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Services.Add(typeof(IExceptionLogger), new LogProviderExceptionLogger(resolver.Resolve<ILog>()));
config.Services.Add(typeof(IExceptionLogger), new LogProviderExceptionLogger(container.Resolve<ILog>()));

appBuilder.UseAutofacMiddleware(container);
appBuilder.UseAutofacWebApi(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,11 @@
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using Autofac;
using Autofac.Core;

namespace EventFlow.Extensions
namespace EventFlow.Configuration.Registrations
{
internal static class ContainerExtensions
public enum Lifetime
{
internal static void ValidateRegistrations(this IContainer container)
{
var services = container
.ComponentRegistry
.Registrations
.SelectMany(x => x.Services)
.OfType<TypedService>()
.Where(x => !x.ServiceType.Name.StartsWith("Autofac"))
.ToList();
var exceptions = new List<Exception>();
foreach (var typedService in services)
{
try
{
container.Resolve(typedService.ServiceType);
}
catch (DependencyResolutionException ex)
{
exceptions.Add(ex);
}
}
if (!exceptions.Any())
{
return;
}

var message = string.Join(", ", exceptions.Select(e => e.Message));
throw new AggregateException(message, exceptions);
}
AlwaysUnique,
Singleton,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@

using System;
using Autofac;
using EventFlow.Configuration.Registrations.Resolvers;

namespace EventFlow.Configuration.Registrations
{
internal abstract class Decorator
internal abstract class AutofacDecorator
{
public abstract Type ServiceType { get; }
public abstract void Configure(ContainerBuilder containerBuilder, int level, bool hasMore);
Expand All @@ -44,21 +43,21 @@ protected string GetKey(int level)
}
}

internal class Decorator<TService> : Decorator
internal class AutofacDecorator<TService> : AutofacDecorator
{
private readonly Func<IResolverContext, TService, TService> _factory;
private readonly Type _serviceType = typeof(TService);
public override Type ServiceType { get { return _serviceType; } }

public Decorator(Func<IResolverContext, TService, TService> factory)
public AutofacDecorator(Func<IResolverContext, TService, TService> factory)
{
_factory = factory;
}

public override void Configure(ContainerBuilder containerBuilder, int level, bool hasMore)
{
var registration = containerBuilder.RegisterDecorator<TService>(
(r, inner) => _factory(new AutofacResolverContext(new AutofacResolver(r)), inner),
(r, inner) => _factory(new ResolverContext(new AutofacResolver(r)), inner),
GetKey(level - 1));
if (hasMore)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,19 @@
using System;
using Autofac;
using Autofac.Builder;
using EventFlow.Configuration.Registrations.Resolvers;

namespace EventFlow.Configuration.Registrations
{
public enum Lifetime
{
AlwaysUnique,
Singleton,
}

public class Registration
internal class AutofacRegistration
{
private readonly Type _implementationType;

public Type ServiceType { get; protected set; }
public Lifetime Lifetime { get; protected set; }

public Registration(){ }
public AutofacRegistration(){ }

public Registration(Type serviceType, Type implementationType, Lifetime lifetime = Lifetime.AlwaysUnique)
public AutofacRegistration(Type serviceType, Type implementationType, Lifetime lifetime = Lifetime.AlwaysUnique)
{
if (!serviceType.IsAssignableFrom(implementationType))
{
Expand Down Expand Up @@ -87,12 +80,12 @@ protected void HandleDecoration(
}
}

public class Registration<TService> : Registration
internal class AutofacRegistration<TService> : AutofacRegistration
where TService : class
{
public Func<IResolverContext, object> Factory { get; protected set; }

public Registration(Func<IResolverContext, TService> factory, Lifetime lifetime = Lifetime.AlwaysUnique)
public AutofacRegistration(Func<IResolverContext, TService> factory, Lifetime lifetime = Lifetime.AlwaysUnique)
{
ServiceType = typeof (TService);
Factory = factory;
Expand All @@ -105,12 +98,12 @@ internal override void Configure(ContainerBuilder containerBuilder, bool hasDeco
{
case Lifetime.AlwaysUnique:
HandleDecoration(
containerBuilder.Register(cc => Factory(new AutofacResolverContext(new AutofacResolver(cc)))),
containerBuilder.Register(cc => Factory(new ResolverContext(new AutofacResolver(cc)))),
hasDecorator);
break;
case Lifetime.Singleton:
HandleDecoration(
containerBuilder.Register(cc => Factory(new AutofacResolverContext(new AutofacResolver(cc)))).SingleInstance(),
containerBuilder.Register(cc => Factory(new ResolverContext(new AutofacResolver(cc)))).SingleInstance(),
hasDecorator);
break;
default:
Expand All @@ -119,10 +112,10 @@ internal override void Configure(ContainerBuilder containerBuilder, bool hasDeco
}
}

public class Registration<TService, TImplementation> : Registration
internal class AutofacRegistration<TService, TImplementation> : AutofacRegistration
where TImplementation : class, TService
{
public Registration(Lifetime lifetime = Lifetime.AlwaysUnique)
public AutofacRegistration(Lifetime lifetime = Lifetime.AlwaysUnique)
{
Lifetime = lifetime;
ServiceType = typeof (TService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
using System.Linq;
using Autofac;

namespace EventFlow.Configuration.Registrations.Resolvers
namespace EventFlow.Configuration.Registrations
{
public class AutofacResolver : IResolver
internal class AutofacResolver : IResolver
{
private readonly IComponentContext _componentContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

using Autofac;

namespace EventFlow.Configuration.Registrations.Resolvers
namespace EventFlow.Configuration.Registrations
{
public class AutofacRootResolver : AutofacScopeResolver, IRootResolver
internal class AutofacRootResolver : AutofacScopeResolver, IRootResolver
{
public IContainer Container { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System.Collections.Generic;
using Autofac;

namespace EventFlow.Configuration.Registrations.Resolvers
namespace EventFlow.Configuration.Registrations
{
public class AutofacScopeResolver : AutofacResolver, IScopeResolver
internal class AutofacScopeResolver : AutofacResolver, IScopeResolver
{
private readonly ILifetimeScope _lifetimeScope;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
using System.Collections.Generic;
using System.Linq;
using Autofac;
using EventFlow.Configuration.Registrations.Resolvers;
using EventFlow.Extensions;
using Autofac.Core;

namespace EventFlow.Configuration.Registrations
{
public class AutofacServiceRegistration : IServiceRegistration
internal class AutofacServiceRegistration : IServiceRegistration
{
private readonly ContainerBuilder _containerBuilder;
private readonly List<Registration> _registrations = new List<Registration>();
private readonly Dictionary<Type, List<Decorator>> _decorators = new Dictionary<Type, List<Decorator>>();
private readonly List<AutofacRegistration> _registrations = new List<AutofacRegistration>();
private readonly Dictionary<Type, List<AutofacDecorator>> _decorators = new Dictionary<Type, List<AutofacDecorator>>();

public AutofacServiceRegistration() : this(null) { }
public AutofacServiceRegistration(ContainerBuilder containerBuilder)
Expand All @@ -45,32 +44,32 @@ public void Register<TService, TImplementation>(Lifetime lifetime = Lifetime.Alw
where TImplementation : class, TService
where TService : class
{
_registrations.Add(new Registration<TService, TImplementation>(lifetime));
_registrations.Add(new AutofacRegistration<TService, TImplementation>(lifetime));
}

public void Register<TService>(Func<IResolverContext, TService> factory, Lifetime lifetime = Lifetime.AlwaysUnique)
where TService : class
{
_registrations.Add(new Registration<TService>(factory, lifetime));
_registrations.Add(new AutofacRegistration<TService>(factory, lifetime));
}

public void Register(Type serviceType, Type implementationType, Lifetime lifetime = Lifetime.AlwaysUnique)
{
_registrations.Add(new Registration(serviceType, implementationType, lifetime));
_registrations.Add(new AutofacRegistration(serviceType, implementationType, lifetime));
}

public void Decorate<TService>(Func<IResolverContext, TService, TService> factory)
{
var serviceType = typeof (TService);
List<Decorator> decorators;
List<AutofacDecorator> decorators;

if (!_decorators.TryGetValue(serviceType, out decorators))
{
decorators = new List<Decorator>();
decorators = new List<AutofacDecorator>();
_decorators.Add(serviceType, decorators);
}

decorators.Add(new Decorator<TService>(factory));
decorators.Add(new AutofacDecorator<TService>(factory));
}

public bool HasRegistrationFor<TService>()
Expand Down Expand Up @@ -106,10 +105,40 @@ public IRootResolver CreateResolver(bool validateRegistrations)

if (validateRegistrations)
{
container.ValidateRegistrations();
ValidateRegistrations(container);
}

return new AutofacRootResolver(container);
}

private static void ValidateRegistrations(IComponentContext container)
{
var services = container
.ComponentRegistry
.Registrations
.SelectMany(x => x.Services)
.OfType<TypedService>()
.Where(x => !x.ServiceType.Name.StartsWith("Autofac"))
.ToList();
var exceptions = new List<Exception>();
foreach (var typedService in services)
{
try
{
container.Resolve(typedService.ServiceType);
}
catch (DependencyResolutionException ex)
{
exceptions.Add(ex);
}
}
if (!exceptions.Any())
{
return;
}

var message = string.Join(", ", exceptions.Select(e => e.Message));
throw new AggregateException(message, exceptions);
}
}
}
Loading

0 comments on commit 970b422

Please sign in to comment.