Skip to content

Commit

Permalink
adding autofac
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Gawlik authored and Artur Gawlik committed Jul 8, 2017
1 parent 9b9a16a commit a448ae1
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 26 deletions.
11 changes: 7 additions & 4 deletions Passenger.Api/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Passenger.Infrastructure.Commands;
using Passenger.Infrastructure.Commands.Users;
using Passenger.Infrastructure.DTO;
using Passenger.Infrastructure.Services;
Expand All @@ -13,9 +14,11 @@ namespace Passenger.Api.Controllers
public class UsersController : Controller
{
private readonly IUserService _userService;
public UsersController(IUserService userService)
private readonly ICommandDispatcher _commandDispatcher;
public UsersController(IUserService userService, ICommandDispatcher commandDispatcher)
{
_userService = userService;
_commandDispatcher = commandDispatcher;
}

[HttpGet("{email}")]
Expand All @@ -28,11 +31,11 @@ public async Task<IActionResult> Get(string email)
return Json(user);
}
[HttpPost]
public async Task<IActionResult> Post([FromBody]CreateUser request)
public async Task<IActionResult> Post([FromBody]CreateUser command)
{
await _userService.RegisterAsync(request.Email, request.UserName, request.Password);
await _commandDispatcher.DispatchAsync(command);

return Created($"users/{request.Email}", new object());
return Created($"users/{command.Email}", new object());
}
}
}
2 changes: 2 additions & 0 deletions Passenger.Api/Passenger.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
<PackageReference Include="Autofac" Version="4.6.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.1.0" />
</ItemGroup>

</Project>
20 changes: 17 additions & 3 deletions Passenger.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
using Passenger.Infrastructure.Services;
using Passenger.Core.Repositories;
using Passenger.Infrastructure.Mappers;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Passenger.Infrastructure.IoC.Modules;

namespace Passenger.Api
{
public class Startup
{

public IConfigurationRoot Configuration { get; }
public IContainer ApplicationContainer { get; private set; }

public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
Expand All @@ -26,26 +33,33 @@ public Startup(IHostingEnvironment env)
Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddScoped<IUserRepository, InMemoryUserRepository>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IDriverRepository, InMemoryDriverRepopsitory>();
services.AddSingleton(AutoMapperConfig.Initialize());
services.AddMvc();

var builder = new ContainerBuilder();
builder.Populate(services);
builder.RegisterModule<CommandModule>();
ApplicationContainer = builder.Build();

return new AutofacServiceProvider(ApplicationContainer);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifeTime)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

app.UseMvc();
appLifeTime.ApplicationStopped.Register(() => ApplicationContainer.Dispose());
}
}
}
25 changes: 25 additions & 0 deletions Passenger.Infrastructure/Commands/CommandDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Threading.Tasks;
using Autofac;
using Passenger.Infrastructure.Commands.Users;

namespace Passenger.Infrastructure.Commands
{
public class CommandDispatcher : ICommandDispatcher
{
private readonly IComponentContext _context;

public CommandDispatcher(IComponentContext context)
{
_context = context;
}
public async Task DispatchAsync<T>(T command) where T : ICommand
{
if(command == null)
throw new ArgumentNullException(nameof(command), $"Command '{typeof(T).Name}' can not be null.");

var handler = _context.Resolve<ICommandHandler<T>>();
await handler.HandleAsync(command);
}
}
}
10 changes: 10 additions & 0 deletions Passenger.Infrastructure/Commands/ICommandDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Tasks;
using Passenger.Infrastructure.Commands.Users;

namespace Passenger.Infrastructure.Commands
{
public interface ICommandDispatcher
{
Task DispatchAsync<T>(T command) where T : ICommand;
}
}
10 changes: 10 additions & 0 deletions Passenger.Infrastructure/Commands/ICommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Tasks;
using Passenger.Infrastructure.Commands.Users;

namespace Passenger.Infrastructure.Commands
{
public interface ICommandHandler<T> where T : ICommand
{
Task HandleAsync(T command);
}
}
21 changes: 21 additions & 0 deletions Passenger.Infrastructure/Handlers/Users/CreateUserHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Threading.Tasks;
using Passenger.Infrastructure.Commands;
using Passenger.Infrastructure.Commands.Users;
using Passenger.Infrastructure.Services;

namespace Passenger.Infrastructure.Handlers.Users
{
public class CreateUserHandler : ICommandHandler<CreateUser>
{
private readonly IUserService _userService;
public CreateUserHandler(IUserService userService)
{
_userService = userService;
}
public async Task HandleAsync(CreateUser command)
{
await _userService.RegisterAsync(command.Email, command.UserName, command.Password);
}
}
}
24 changes: 24 additions & 0 deletions Passenger.Infrastructure/IoC/Modules/CommandModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Reflection;
using Autofac;
using Passenger.Infrastructure.Commands;

namespace Passenger.Infrastructure.IoC.Modules
{
public class CommandModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
var assembly = typeof(CommandModule)
.GetTypeInfo()
.Assembly;

builder.RegisterAssemblyTypes(assembly)
.AsClosedTypesOf(typeof(ICommandHandler<>))
.InstancePerLifetimeScope();

builder.RegisterType<CommandDispatcher>()
.As<ICommandDispatcher>()
.InstancePerLifetimeScope();
}
}
}
1 change: 1 addition & 0 deletions Passenger.Infrastructure/Passenger.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="6.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Autofac" Version="4.6.0" />
</ItemGroup>

</Project>
7 changes: 4 additions & 3 deletions Passenger.Tests.EndToEnd/Passenger.Tests.EndToEnd.csproj
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.01" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="FluentAssertions" Version="4.19.0" />
<PackageReference Include="dotnet-test-nunit" Version="3.4.0-beta-3" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="FluentAssertions" Version="4.19.2" />
<PackageReference Include="Autofac" Version="4.6.0" />
</ItemGroup>

</Project>
16 changes: 0 additions & 16 deletions Passenger.Tests/Services/UserServicesTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Threading.Tasks;
using AutoMapper;
using FluentAssertions;
using Moq;
using Passenger.Core.Domain;
using Passenger.Core.Repositories;
using Passenger.Infrastructure.Services;
using Xunit;
using System.Collections.Generic;

namespace Passenger.Tests.Services
{
Expand All @@ -25,18 +22,5 @@ public async Task register_async_should_invoke_add_async_on_repository()
userRepositoryMock.Verify(x => x.AddAsync(It.IsAny<User>()), Times.Once);

}

[Fact]
public async Task trying_to_register_user_that_have_email_witch_is_already_in_use_should_trow_exeption()
{
var userRepositoryMock = new Mock<IUserRepository>();
var mapperMock = new Mock<IMapper>();

var userService = new UserService(userRepositoryMock.Object, mapperMock.Object);
await userService.RegisterAsync("[email protected]", "test", "password");
var a = await userService.GetAsync("[email protected]");

//await Assert.ThrowsAnyAsync<Exception>(() => userService.RegisterAsync("[email protected]", "test", "password"));
}
}
}

0 comments on commit a448ae1

Please sign in to comment.