ubeac / ubeac-api

Source of uBeac .NET Core packages
3 stars 2 forks source link

Implement Exposing Enums #91

Closed ilhesam closed 2 years ago

ilhesam commented 2 years ago

❗ Overview: GET: /API/Enums/GetAllByAssembly Returns all enums in entry and referenced assemblies that have EnumAttribute. 😀

❓ How to implement: We have a based interface called IEnumsProvider in uBeac.Core.Enums:

public interface IEnumsProvider
{
    IEnumerable<EnumModel> GetAll();
}

So can have different types of providers for enums! We currently need an implementation of the IEnumProvider that exposes the enums of an assembly, I call this AssemblyEnumsProvider:

public interface IAssemblyEnumsProvider : IEnumsProvider
{
}

Inside the code you can see the full details.

❓ How to use in end projects: Just call AddAssemblyEnumsProvider in Program.cs and enjoy :)

builder.Services.AddAssemblyEnumsProvider();

EnumsController is available in uBeac.Core.Web.Common.

public class EnumsController : BaseController
{
    private readonly IAssemblyEnumsProvider _assemblyEnums;

    public EnumsController(IAssemblyEnumsProvider assemblyEnums)
    {
        _assemblyEnums = assemblyEnums;
    }

    [HttpGet]
    public IListResult<EnumModel> GetAllByAssembly() => _assemblyEnums.GetAll().ToListResult();
}