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 integration #20

Merged
merged 28 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2ad7b33
Change order of DI selection from assembly: Autofac => DotNetCore => …
pszulccompentio Nov 3, 2021
14d09fb
SourceMapper as linked project, not package.
pszulccompentio Nov 3, 2021
48d6a22
Code generation for Autofac processor.
pszulccompentio Nov 3, 2021
7ebf609
Using stringbuilder instead of string.
pszulccompentio Nov 3, 2021
391e01c
Adding Autofac strategy to available strategies list.
pszulccompentio Nov 3, 2021
9432eaa
Example project with Autofac DI.
pszulccompentio Nov 3, 2021
d5bc3d0
Missing mapping.
pszulccompentio Nov 3, 2021
a21f775
Missing mapping - continue.
pszulccompentio Nov 3, 2021
1b716e8
DependencyInjectionStrategyFactory - unit testing.
pszulccompentio Nov 3, 2021
35be453
Move plain major strings to resx file.
pszulccompentio Nov 4, 2021
a5803c3
Update comments.
pszulccompentio Nov 4, 2021
ae40b77
Register Autofac services via module.
pszulccompentio Nov 4, 2021
a90ef1b
Unit tests.
pszulccompentio Nov 4, 2021
7f07e77
Update unit tests.
pszulccompentio Nov 4, 2021
a39e8e8
Update unit tests - asserts for convention.
pszulccompentio Nov 4, 2021
d758f46
Removing unnecessary class.
pszulccompentio Nov 4, 2021
4df36d3
Update unit testing - create mocks with Fixture.
pszulccompentio Nov 4, 2021
21582f0
Update assembly names - from resx to const strings.
pszulccompentio Nov 4, 2021
5dfc6f5
Update naming convention for examples projects.
pszulccompentio Nov 5, 2021
b93d9af
Update unit tests - Result class.
pszulccompentio Nov 5, 2021
21e27d7
Update unit tests.
pszulccompentio Nov 5, 2021
a910185
Exclude from coverage - example projects.
pszulccompentio Nov 5, 2021
9eb28e1
Update comments.
pszulccompentio Nov 8, 2021
8e29694
Update docs.
pszulccompentio Nov 8, 2021
192b14f
Update unit tests.
pszulccompentio Nov 8, 2021
dba7a10
Merge branch 'master' into feature/autofac_integration
alekshura Nov 8, 2021
ed28fb4
Unused directories & usings & package version
alekshura Nov 8, 2021
70646d6
Merge branch 'master' into feature/autofac_integration
alekshura Nov 8, 2021
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
22 changes: 22 additions & 0 deletions Compentio.Example.Autofac.App/AutofacModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Autofac;
using Compentio.Example.Autofac.App.Repository;
using Compentio.Example.Autofac.App.Services;
using Compentio.SourceMapper.DependencyInjection;
using System.Diagnostics.CodeAnalysis;

namespace Compentio.Example.Autofac.App
{
/// <summary>
/// Module with registration for Autofac
/// </summary>
[ExcludeFromCodeCoverage]
public class AutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<BooksService>().As<IBooksService>().InstancePerDependency();
builder.RegisterType<BooksRepository>().As<IBooksRepository>().SingleInstance();
builder.AddMappers();
}
}
}
24 changes: 24 additions & 0 deletions Compentio.Example.Autofac.App/Compentio.Example.Autofac.App.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Generators\**" />
<Content Remove="Generators\**" />
<EmbeddedResource Remove="Generators\**" />
<None Remove="Generators\**" />
</ItemGroup>



<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Compentio.SourceMapper.Generator\Compentio.SourceMapper.csproj" OutputItemType="Analyzer" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions Compentio.Example.Autofac.App/Controllers/BooksController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Compentio.Example.Autofac.App.Entities;
using Compentio.Example.Autofac.App.Services;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Compentio.Example.Autofac.App.Controllers
{
[ExcludeFromCodeCoverage]
[Route("api/[controller]")]
[ApiController]
public class BooksController : ControllerBase
{
private readonly IBooksService _booksServices;

public BooksController(IBooksService booksService)
{
_booksServices = booksService;
}

[HttpGet]
public IEnumerable<BookDto> Get()
{
var books = _booksServices.GetBooks();

return books.Result;
}
}
}
15 changes: 15 additions & 0 deletions Compentio.Example.Autofac.App/Entities/AddressDao.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Compentio.Example.Autofac.App.Entities
{
public class AddressDao
{
public Guid Id { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public string City { get; set; }
public string PostCode { get; set; }
public string Street { get; set; }
public string HomeNumber { get; set; }
}
}
15 changes: 15 additions & 0 deletions Compentio.Example.Autofac.App/Entities/AddressDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Compentio.Example.Autofac.App.Entities
{
public class AddressDto
{
public Guid AddressId { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public string Street { get; set; }
public string Home { get; set; }
}
}
14 changes: 14 additions & 0 deletions Compentio.Example.Autofac.App/Entities/BookDao.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Compentio.Example.Autofac.App.Entities
{
public class BookDao
{
public Guid Id { get; set; }
public string Author { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int PublishingYear { get; set; }
public AddressDao LibraryAddress { get; set; }
}
}
14 changes: 14 additions & 0 deletions Compentio.Example.Autofac.App/Entities/BookDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Compentio.Example.Autofac.App.Entities
{
public class BookDto
{
public Guid BookId { get; set; }
public string Author { get; set; }
public string Title { get; set; }
public string BookDescription { get; set; }
public int PublishingYear { get; set; }
public AddressDto LibraryAddress { get; set; }
}
}
27 changes: 27 additions & 0 deletions Compentio.Example.Autofac.App/Mapper/IBooksMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Compentio.Example.Autofac.App.Entities;
using Compentio.SourceMapper.Attributes;

namespace Compentio.Example.Autofac.App.Mapper
{
[Mapper]
public interface IBooksMapper
{
[Mapping(Source = nameof(BookDao.Description), Target = nameof(BookDto.BookDescription))]
[Mapping(Source = nameof(BookDao.Id), Target = nameof(BookDto.BookId))]
BookDto MapBookToDto(BookDao source);

[Mapping(Source = nameof(BookDto.BookDescription), Target = nameof(BookDao.Description))]
[Mapping(Source = nameof(BookDto.BookId), Target = nameof(BookDao.Id))]
BookDao MapBookToDao(BookDto source);

[Mapping(Source = nameof(AddressDao.PostCode), Target = nameof(AddressDto.PostalCode))]
[Mapping(Source = nameof(AddressDao.HomeNumber), Target = nameof(AddressDto.Home))]
[Mapping(Source = nameof(AddressDao.Id), Target = nameof(AddressDto.AddressId))]
AddressDto MapAddressToDto(AddressDao source);

[Mapping(Source = nameof(AddressDto.PostalCode), Target = nameof(AddressDao.PostCode))]
[Mapping(Source = nameof(AddressDto.Home), Target = nameof(AddressDao.HomeNumber))]
[Mapping(Source = nameof(AddressDto.AddressId), Target = nameof(AddressDao.Id))]
AddressDao MapAddressToDao(AddressDto source);
}
}
31 changes: 31 additions & 0 deletions Compentio.Example.Autofac.App/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System.Diagnostics.CodeAnalysis;

namespace Compentio.Example.Autofac.App
{
/// <summary>
/// Sample code with Autofac Dependency Injection
/// </summary>
[ExcludeFromCodeCoverage]
static class Program
{
static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

/// <summary>
/// The UseServiceProviderFactory call attaches the
/// Autofac provider to the generic hosting mechanism.
/// </summary>
static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
31 changes: 31 additions & 0 deletions Compentio.Example.Autofac.App/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:27231",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/books",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Compentio.Example.Autofac.App": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "api/books",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
43 changes: 43 additions & 0 deletions Compentio.Example.Autofac.App/Repository/BooksRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Compentio.Example.Autofac.App.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;

namespace Compentio.Example.Autofac.App.Repository
{
[ExcludeFromCodeCoverage]
public class BooksRepository : IBooksRepository
{
public async Task<BookDao> CreateBook(BookDao book)
{
return await Task.FromResult(_bookDao);
}

public async Task<BookDao> GetBook(Guid bookId)
{
return await Task.FromResult(_bookDao);
}

public async Task<IEnumerable<BookDao>> GetBooks()
{
var booksList = new List<BookDao>();
booksList.Add(_bookDao);

return await Task.FromResult(booksList);
}

public async Task<BookDao> UpdateBook(BookDao book)
{
return await Task.FromResult(_bookDao);
}

private readonly BookDao _bookDao = new BookDao()
{
Id = Guid.NewGuid(),
Author = "Lem, S",
Title = "Solaris",
PublishingYear = 1961
};
}
}
18 changes: 18 additions & 0 deletions Compentio.Example.Autofac.App/Repository/IBooksRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Compentio.Example.Autofac.App.Entities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Compentio.Example.Autofac.App.Repository
{
public interface IBooksRepository
{
Task<IEnumerable<BookDao>> GetBooks();

Task<BookDao> GetBook(Guid bookId);

Task<BookDao> CreateBook(BookDao book);

Task<BookDao> UpdateBook(BookDao book);
}
}
50 changes: 50 additions & 0 deletions Compentio.Example.Autofac.App/Services/BooksService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Compentio.Example.Autofac.App.Entities;
using Compentio.Example.Autofac.App.Mapper;
using Compentio.Example.Autofac.App.Repository;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;

namespace Compentio.Example.Autofac.App.Services
{
[ExcludeFromCodeCoverage]
public class BooksService : IBooksService
{
private readonly IBooksRepository _booksRepository;
private readonly IBooksMapper _booksMapper;

public BooksService(IBooksRepository booksRepository, IBooksMapper booksMapper)
{
_booksRepository = booksRepository;
_booksMapper = booksMapper;
}

public async Task<BookDto> CreateBook(BookDto book)
{
var bookDao = _booksMapper.MapBookToDao(book);
var bookResult = await _booksRepository.CreateBook(bookDao);
return _booksMapper.MapBookToDto(bookResult);
}

public async Task<BookDto> GetBook(Guid bookId)
{
var bookResult = await _booksRepository.GetBook(bookId);
return _booksMapper.MapBookToDto(bookResult);
}

public async Task<IEnumerable<BookDto>> GetBooks()
{
var booksResult = await _booksRepository.GetBooks();
return booksResult.Select(bookDao => _booksMapper.MapBookToDto(bookDao));
}

public async Task<BookDto> UpdateBook(BookDto book)
{
var bookDao = _booksMapper.MapBookToDao(book);
var updateResult = await _booksRepository.UpdateBook(bookDao);
return _booksMapper.MapBookToDto(updateResult);
}
}
}
18 changes: 18 additions & 0 deletions Compentio.Example.Autofac.App/Services/IBooksService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Compentio.Example.Autofac.App.Entities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Compentio.Example.Autofac.App.Services
{
public interface IBooksService
{
Task<IEnumerable<BookDto>> GetBooks();

Task<BookDto> GetBook(Guid bookId);

Task<BookDto> CreateBook(BookDto book);

Task<BookDto> UpdateBook(BookDto book);
}
}
Loading