Garcia is a .Net framework that includes various integrations and utility services. Garcia provides developers to develop fast and powerful web APIs without wasting time with integrations.
The architecture is based on the Onion Architecture, so it can be easily used in DDD projects. You can take a look at what Onion Architecture is.
The project basically consists of 4 layers. These are :
Layer | Content |
---|---|
Domain: | Contains different types of base entity classes and interfaces. |
Application: | Contains some application services , infrastructure services contracts and persistence services contracts. |
Infrastructure: | The layer of integration and infrastructural services. It contains external service registrations, integration adapter services, some settings classes and interfaces, different kind of base controllers. |
Persistence : | The layer of persistency. It contains repository services, db context classes and registration services of them. |
Garcia has own service bus system. Take a look at here.
You can use the following technologies either with garcia service bus or with their own services.
Intall Garcia and it's dependencies using NuGet:
NuGet
> Install-Package Garcia
Or:
.NET CLI:
> dotnet add package Garcia
Let's create a simple crud service with Entity Framework for a quick start.
> dotnet new webapi --name QuickStart
> dotnet add package Garcia
using Garcia.Domain;
namespace QuickStart;
public class Sample : Entity<long>
{
public string Name { get; set; }
}
public class SampleDto
{
public long Id { get; private set; }
public string Name { get; set; }
}
using Garcia.Application.Services;
using Garcia.Infrastructure.Api.Controllers;
using Microsoft.AspNetCore.Mvc;
namespace QuickStart.Controllers;
[ApiController]
public class SampleController : BaseController<Sample, SampleDto>
// BaseController has 2 generic parameters. These are an Entity<long> and a Dto of the Entity.
{
public SampleController(IBaseService<Sample, SampleDto, long> services) : base(services)
{
}
}
Let's register the EntityFramework, repository services and IBaseService.
Add the following statements to the program class:
using Garcia.Application;
using Garcia.Application.Contracts.Persistence;
using Garcia.Persistence.EntityFramework;
using QuickStart;
var builder = WebApplication.CreateBuilder(args);
// Other configurations ...
builder.Services
.AddEfCoreInMemory<BaseContext>("Sample")
.AddEfCoreRepository();
builder.Services.AddBaseService<Sample, SampleDto>();
// BaseService also has a 2 generic parameters. These are an Entity<long> and a Dto of the Entity.
You need a just few things adding an authentication.
using System.ComponentModel.DataAnnotations.Schema;
using Garcia.Domain;
using Garcia.Domain.Identity;
public class User : Entity<long>, IUserEntity<long> // User must be an entity and implemented from IUserEntity<TKey>
{
public string Username { get; set; }
public string Password { get; set; }
[NotMapped]
public List<string> Roles { get; set; } = new List<string>();
// The role field comes from IUserEntity. If you want, you can customize the Context and define it as the owned entity type.
// If you are using MongoDB or Cassandra for persistence of the User you don't need [NotMapped] attribute.
}
using Garcia.Application.Contracts.Identity;
public class UserDto : IUser
{
public string Id { get; set; }
public string Username { get; set; }
public List<string> Roles { get; set; } = new List<string>();
}
using Garcia.Application.Identity.Services;
using Garcia.Infrastructure.Api.Controllers;
using Microsoft.AspNetCore.Mvc;
namespace QuickStart.Controllers;
[ApiController]
public class AuthenticationController : BaseAuthController<User, UserDto>
/* BaseAuthController has also 2 generic parameters like a BaseController.
These are a User BaseType is Entity<long> and implemented from IUserEntity<long>,
a UserDto implemented from IUser. */
{
public AuthenticationController(IAuthenticationService<User, UserDto, long> service) : base(service)
{
}
}
Add the following statement to the Program.cs:
builder.Services.AddAuthenticationService<User, UserDto>(builder.Configuration);
/* AuthenticationService also has a 2 generic parameters like BaseService.
These are a User BaseType is Entity<long> and implemented from IUserEntity<long> and a UserDto implemented from IUser.*/
AuthenticationService uses JwtService for generate and verify a new jwt. JwtService needs JwtIssuerOptions to do this jobs.
"JwtIssuerOptions": {
"Issuer": "garcia",
"Audience": "garcia",
"SecretKey": "2c47231f-9da0-45a6-b6ea-4cf89281b63d",
"ValidFor": 7200
}
To protect your controller add Authorize attribute top of the controller.
using Garcia.Application.Services;
using Garcia.Infrastructure.Api.Controllers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace QuickStart.Controllers;
[ApiController]
[Authorize(AuthenticationSchemes = "Bearer")]
public class SampleController : BaseController<Sample, SampleDto>
{
public SampleController(IBaseService<Sample, SampleDto, long> services) : base(services)
{
}
}
using Garcia.Infrastructure.Api;
// Other Configurations...
builder.Services.AddSwaggerWithAuthorization();
> dotnet run
You can find complete project in here.
And thats it. Many more different features are waiting for you in here. Welcome to Garcia.