Skip to content

A .NET library to help coding easily like Spring Boot + Mybatis with Java.

Notifications You must be signed in to change notification settings

ikaroinory/Fabric.Web

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fabric.Web

Fabric.Web is a .NET library to help coding easily like Spring Boot + Mybatis with Java.

You can use Controller, Service and Repository with Fabric.Web build a Web API Solution.

Description

  • Integrate Microsoft Entity Framework Core(EF Core).
  • Support SQL Server, MySQL and MariaDB.
  • Integrate common CRUD.
  • Support SQL query.
  • Support UTC time formatting.

Configuration

Register Fabric.Web service.

builder.Services.AddFabricWebService(new DatabaseConfiguration());

You can configure properties in the class DatabaseConfiguration like this:

new DatabaseConfiguration
{
    DatabaseType    =   <DatabaseType>,
    Server          =   <Server URL>,
    Port            =   <Port>,
    Username        =   <Database Username>,
    Password        =   <Database Password>,
    Database        =   <Schema Name>,
    DatabaseVersion =   [Database Version]
}

The property DatabaseVersion is not required if you are connecting SQL Server.

Mark IOC containers with attributes.

In Web API Solution, we often use Controller-Service-Repository(DAO) pattern, therefore we use attributes for Inversion of Control these containers.

Generally, controllers is managed by ASP.NET Core(Because of MVC container), so we only need to mark services and repositories.

  • Mark entities with EntityAttribute.
[Entity]
public class Foo
{
    [Key]
    [Required]
    public required string Name { get; set; }

    [Required]
    [MinLength(32), MaxLength(32)]
    public required string Phone{ get; set; }

    [Required]
    [EmailAddress]
    public required string Email { get; set; }
}
  • Mark repository with RepositoryAttribute.
public interface IFooRepository
{
    IEnumerable<Foo> GetFooList();
}

[Repository]
public class FooRepository : IFooRepository
{
    // Fabric.Web.Repository.Context
    private readonly Context context;

    // Auto-wire by constructor
    public FooRepository(Context context) { this.context = context; }

    public IEnumerable<Foo> GetFooList() => context.Set<Foo>().ToList();
}
  • Mark service with ServiceAttribute.
public interface IFooService
{
    Foo GetFooByName(string name);
}

[Service]
public class FooService : IFooService
{
    private readonly IFooRepository fooRepository;

    // Auto-wire by constructor
    public FooService(IFooRepository fooRepository) { this.fooRepository = fooRepository; }

    public Foo GetFooByName(string name) => fooRepository.GetFooList().Where(foo => foo.Name == name).First();
}

Wire services into the controller

.NET can auto-wire your containers by IOC, you only need add a constructor into your controller.

[ApiController]
[Route("[controller]")]
public class FooController : ControllerBase
{
    private readonly IFooService fooService;

    // Auto-wire by constructor
    public TestController(IFooService fooService) { this.fooService = fooService; }
}

CRUD Interface

Fabric.Web provides a interface IBaseRepository<TEntity> includes some crud methods, you can use the interface in repositories like this:

public interface IFooRepository : IBaseRepository<Foo> { }

[Repository]
public class FooRepository : BaseRepository<Foo>, IFooRepository
{
    // Required.
    public FooRepository(Context context) : base(context) { }
}

You can use CRUD method after that:

[Service]
public class FooService : IFooService
{
    private readonly IFooRepository fooRepository;

    public FooService(IFooRepository fooRepository) { this.fooRepository = fooRepository; }

    // IBaseRepository<TEntity>.SelectByPrimaryKey()
    public string? GetPhoneByName(string name) => SelectByPrimaryKey(name)?.Phone;
}

Similarly, services also have CRUD interface.

public interface IFooService : IBaseService<Foo> { }

[Service]
public class FooService : BaseService<Foo, IFooRepository>, IFooService
{
    // Required.
    public FooService(IFooRepository repository) : base(repository) { }
    
    public string? GetPhoneByName(string name) => Repository.GetPhoneByName(name);
}

Formatter

Fabric.Web provides some formatter, you can use them like this:

Date formatter

You can add date formatter like this:

builder.Services.AddFabricWebService(config, addDateTimeFormatter: true);

Before: 2023-01-01T00:00:00

After: 2023-01-01 00:00:00

No content formatter

You can add no content formatter like this:

builder.Services.AddFabricWebService(config, addNoContentFormatter: true);

Before: Return Http 204: No Content. After: null

About

A .NET library to help coding easily like Spring Boot + Mybatis with Java.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages