Commit af1e21e1 authored by Scott DePouw's avatar Scott DePouw Committed by Steve Smith

Scott: Autofac (#34)

* Generic paramter on repository lowered to methods so 1 repo instance per class is all that's required.

* Fixed code to use new repository method signatures.

* Add StructureMap NuGet package/dependency to CleanArchitecture.Web since project uses it (currently gets it indirectly via Web's dependnecy on Infrastructure).

* Initial inclusion of AutoFac and making .NET Core use it instead of StructureMap. Ditto the DomainEventDispatcher.

* Remove redundant default.

* Remove StructureMap code and NuGet references.

* Restore comments.
parent 59936d7d
......@@ -5,9 +5,9 @@ namespace CleanArchitecture.Core.Entities
{
public class ToDoItem : BaseEntity
{
public string Title { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public bool IsDone { get; private set; } = false;
public bool IsDone { get; private set; }
public void MarkComplete()
{
......@@ -15,4 +15,4 @@ namespace CleanArchitecture.Core.Entities
Events.Add(new ToDoItemCompletedEvent(this));
}
}
}
\ No newline at end of file
}
......@@ -10,9 +10,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="4.8.1" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" PrivateAssets="All" />
<PackageReference Include="StructureMap.Microsoft.DependencyInjection" Version="1.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
</ItemGroup>
</Project>
using System;
using System.Linq;
using StructureMap;
using Autofac;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Core.SharedKernel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace CleanArchitecture.Infrastructure.DomainEvents
{
......@@ -10,23 +12,22 @@ namespace CleanArchitecture.Infrastructure.DomainEvents
// http://lostechies.com/jimmybogard/2014/05/13/a-better-domain-events-pattern/
public class DomainEventDispatcher : IDomainEventDispatcher
{
private readonly IContainer _container;
private readonly IComponentContext _container;
public DomainEventDispatcher(IContainer container)
public DomainEventDispatcher(IComponentContext container)
{
_container = container;
}
public void Dispatch(BaseDomainEvent domainEvent)
{
var handlerType = typeof(IHandle<>).MakeGenericType(domainEvent.GetType());
var wrapperType = typeof(DomainEventHandler<>).MakeGenericType(domainEvent.GetType());
var handlers = _container.GetAllInstances(handlerType);
var wrappedHandlers = handlers
.Cast<object>()
Type handlerType = typeof(IHandle<>).MakeGenericType(domainEvent.GetType());
Type wrapperType = typeof(DomainEventHandler<>).MakeGenericType(domainEvent.GetType());
IEnumerable handlers = (IEnumerable)_container.Resolve(typeof(IEnumerable<>).MakeGenericType(handlerType));
IEnumerable<DomainEventHandler> wrappedHandlers = handlers.Cast<object>()
.Select(handler => (DomainEventHandler)Activator.CreateInstance(wrapperType, handler));
foreach (var handler in wrappedHandlers)
foreach (DomainEventHandler handler in wrappedHandlers)
{
handler.Handle(domainEvent);
}
......@@ -53,4 +54,4 @@ namespace CleanArchitecture.Infrastructure.DomainEvents
}
}
}
}
\ No newline at end of file
}
......@@ -19,6 +19,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="4.8.1" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.3.0" />
<PackageReference Include="Dapper" Version="1.50.5" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" PrivateAssets="All" />
......
using CleanArchitecture.Core.Interfaces;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using CleanArchitecture.Core.SharedKernel;
using CleanArchitecture.Infrastructure.Data;
using Microsoft.AspNetCore.Builder;
......@@ -9,9 +10,9 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using StructureMap;
using Swashbuckle.AspNetCore.Swagger;
using System;
using System.Reflection;
namespace CleanArchitecture.Web
{
......@@ -47,29 +48,24 @@ namespace CleanArchitecture.Web
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
var container = new Container();
return BuildDependencyInjectionProvider(services);
}
container.Configure(config =>
{
config.Scan(_ =>
{
_.AssemblyContainingType(typeof(Startup)); // Web
_.AssemblyContainingType(typeof(BaseEntity)); // Core
_.Assembly("CleanArchitecture.Infrastructure"); // Infrastructure
_.WithDefaultConventions();
_.ConnectImplementationsToTypesClosing(typeof(IHandle<>));
});
// TODO: Add Registry Classes to eliminate reference to Infrastructure
// TODO: Move to Infrastucture Registry
config.For<IRepository>().Add<EfRepository>();
//Populate the container using the service collection
config.Populate(services);
});
private static IServiceProvider BuildDependencyInjectionProvider(IServiceCollection services)
{
var builder = new ContainerBuilder();
// Populate the container using the service collection
builder.Populate(services);
// TODO: Add Registry Classes to eliminate reference to Infrastructure
Assembly webAssembly = Assembly.GetExecutingAssembly();
Assembly coreAssembly = Assembly.GetAssembly(typeof(BaseEntity));
Assembly infrastructureAssembly = Assembly.GetAssembly(typeof(EfRepository)); // TODO: Move to Infrastucture Registry
builder.RegisterAssemblyTypes(webAssembly, coreAssembly, infrastructureAssembly).AsImplementedInterfaces();
return container.GetInstance<IServiceProvider>();
IContainer applicationContainer = builder.Build();
return new AutofacServiceProvider(applicationContainer);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment