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