Creating the Infrastructure Project
Gill Cleeren
CTO XPIRIT BELGIUM
@gillcleeren www.snowball.be
Overview
Understanding the goal of the
infrastructure projects
Adding Entity Framework Core
Implementing other infrastructure tasks
Understanding the Goal
of the Infrastructure Projects
The Infrastructure Layer
All external or I/O components
User interface
- Database
• EF Core
• DbContext
- Files
Interfaces
Entities - Service bus
- Service client
Application Core - Logging
Infrastructure
The Infrastructure Layer
Configuration of technical frameworks
User interface
Implementations for contracts defined
in the Core project
Reference to Core project
Interfaces - Through dependency inversion and
Entities
dependency injection, services get
implemented
Application Core
Infrastructure
Adding Data Persistence with
Entity Framework Core
Adding EF Core
NuGet packages DbContext Migrations Startup
registration
Repository
implementations
Generic repository contract in
Core project
Base repository implementation
Specific repositories if needed
Base Repository
public class BaseRepository<T> : IAsyncRepository<T> where T : class
{
protected readonly GloboTicketDbContext _dbContext;
public BaseRepository(GloboTicketDbContext dbContext)
{
_dbContext = dbContext;
}
public virtual async Task<T> GetByIdAsync(Guid id)
{
return await _dbContext.Set<T>().FindAsync(id);
}
}
Specific Repositories
public class CategoryRepository : BaseRepository<Category>, ICategoryRepository
{
public CategoryRepository(GloboTicketDbContext dbContext) : base(dbContext)
{ }
public async Task<List<Category>> GetCategoriesWithEvents ()
{
//TODO: include implementation here
}
}
Demo
Adding the Persistence project
Bringing in support for EF Core
Demo Adding the base repository
implementation
Adding specific repositories
Registering services in the
ServiceCollection
“Oh, I forgot to mention this in the
previous meeting…
We’ll need to send a mail when an event
has been created. Can this be added?”
Implementing Other Infrastructure Tasks
Sending a Mail Through SendGrid
Infrastructure code to send a NuGet package in
mail Infrastructure
Implementation in
Interface in Core project
Infrastructure
Creating the Infrastructure project
Demo Writing the mail service using SendGrid
API
Using the code from the Application
Core code
Registering the mail service in the
ServiceCollection
Summary
Infrastructure contains all configuration
for external services
Split up in 2 projects, based on their
target
Up next:
Creating the API
using ASP.NET Core