Minimal API's در دات نت 6 - قسمت چهارم - تدارک مقدمات معماری بر اساس ویژگیها
نویسنده: وحید نصیری
تاریخ: ۱۴۰۰/۱۲/۲۴ ۱۱:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace MinimalBlog.Api.Contracts;
public interface IModule
{
IEndpointRouteBuilder RegisterEndpoints(IEndpointRouteBuilder endpoints);
} public static class ServiceCollectionExtensions
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services,
WebApplicationBuilder builder)
{
// ...
builder.Services.AddAllModules(typeof(Program));
return services;
}
private static void AddAllModules(this IServiceCollection services, params Type[] types)
{
// Using the `Scrutor` to add all of the application's modules at once.
services.Scan(scan =>
scan.FromAssembliesOf(types)
.AddClasses(classes => classes.AssignableTo<IModule>())
.AsImplementedInterfaces()
.WithSingletonLifetime());
}
} using MinimalBlog.Api.Contracts;
namespace MinimalBlog.Api.Extensions;
public static class ModuleExtensions
{
public static WebApplication RegisterEndpoints(this WebApplication app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
var modules = app.Services.GetServices<IModule>();
foreach (var module in modules)
{
module.RegisterEndpoints(app);
}
return app;
}
} using MinimalBlog.Api.Extensions; var builder = WebApplication.CreateBuilder(args); builder.Services.AddApplicationServices(builder); var app = builder.Build(); app.ConfigureApplication(); app.RegisterEndpoints(); app.Run();
namespace MinimalBlog.Api.Features.Authors;
public class AuthorModule : IModule
{
public IEndpointRouteBuilder RegisterEndpoints(IEndpointRouteBuilder endpoints)
{
endpoints.MapGet("/api/authors", async (MinimalBlogDbContext ctx) =>
{
var authors = await ctx.Authors.ToListAsync();
return authors;
});
endpoints.MapPost("/api/authors", async (MinimalBlogDbContext ctx, AuthorDto authorDto) =>
{
var author = new Author();
author.FirstName = authorDto.FirstName;
author.LastName = authorDto.LastName;
author.Bio = authorDto.Bio;
author.DateOfBirth = authorDto.DateOfBirth;
ctx.Authors.Add(author);
await ctx.SaveChangesAsync();
return author;
});
return endpoints;
}
} <Project Sdk="Microsoft.NET.Sdk.Web">
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
</ItemGroup>
</Project> public static class ServiceCollectionExtensions
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services,
WebApplicationBuilder builder)
{
// ...
builder.Services.AddMediatR(typeof(Program));
builder.Services.AddAutoMapper(typeof(Program));
return services;
}
} using AutoMapper;
using MinimalBlog.Domain.Model;
namespace MinimalBlog.Api.Features.Authors;
public class AuthorProfile : Profile
{
public AuthorProfile()
{
CreateMap<AuthorDto, Author>().ReverseMap();
}
}