Deviser Admin
Deviser Admin is a core feature of Deviser Platform to build admin user interfaces with just few lines of code. Admin UIs are generated based on an UI model that can be built using fluent API. In addition to the Admin UI, web service (Web APIs) are also generated for the UI. An UI model can be created either directly from an Entity Framework (EF) Core data context or from an implementation of admin service. EF Core Data context method is a basic approach where CRUD operations for an Admin UI can be built. On other hand, customized UI model can be built using an admin service. These two approaches are explained in the following sections.
Database Context
In this approach, Deviser Admin uses an EF Core dabase context to build Admin UIs. The below diagram illustrates the datbase context approach.
Follow the steps to build Admin UIs with database context approach:
Create Deviser Module
Create a deviser module project with database context as explained in this section.
Implement IAdminConfigurator
Implement a class with the interface IAdminConfigurator<MyDbContext>
. The ConfigureAdmin()
method is used to build Admin UI model.
public class AdminConfigurator : IAdminConfigurator<BlogDbContext>
{
public void ConfigureAdmin(IAdminBuilder adminBuilder)
{
adminBuilder.MapperConfiguration = BlogMapper.MapperConfiguration;
adminBuilder.Register<DTO.Post>(modelBuilder =>
{
modelBuilder.GridBuilder.Title = "Posts";
modelBuilder.FormBuilder.Title = "Post";
modelBuilder.GridBuilder
.AddField(p => p.Title)
.AddField(p => p.Category)
.AddField(p => p.Tags)
.AddField(p => p.CreatedOn, option => option.Format = "dd.MM.yyyy")
.AddField(p => p.CreatedBy, option => option.DisplayName = "Author");
modelBuilder.FormBuilder
.AddKeyField(p => p.Id)
.AddField(p => p.Title)
.AddField(s => s.Summary)
.AddField(s => s.Thumbnail)
.AddField(s => s.Content)
.AddSelectField(s => s.Category, expr => expr.Name)
.AddInlineMultiSelectField<DTO.Tag>(s => s.Tags, expr => expr.Name)
.AddField(p => p.CreatedOn, option => option.Format = "dd.MM.yyyy");
modelBuilder.FormBuilder
.Property(p => p.Tags)
.AddItemBy(t => t.Name);
modelBuilder.AddChildConfig(s => s.Comments, (childForm) =>
{
childForm.FormBuilder
.AddKeyField(c => c.Id)
.AddField(c => c.UserName)
.AddField(c => c.Comment)
.AddField(c => c.CreatedOn)
.AddField(c => c.IsApproved);
});
});
adminBuilder.Register<DTO.Category>(modelBuilder =>
{
modelBuilder.GridBuilder
.AddField(p => p.Name);
modelBuilder.FormBuilder
.AddKeyField(p => p.Id)
.AddField(p => p.Name);
});
adminBuilder.Register<DTO.Tag>(modelBuilder =>
{
modelBuilder.GridBuilder
.AddField(p => p.Name);
modelBuilder.FormBuilder
.AddKeyField(p => p.Id)
.AddField(p => p.Name);
});
}
}
Note
A deviser module can have only one implementation of IAdminConfigurator
, but it can have one or more admin pages. Each admin page is registered by a class type (usually a DTO) and it should be unique in a IAdminConfigurator
implementation.
IAdminBuilder.Register<TModel>()
method registers a admin page for the model TModel
. ModelBuilder
API on this method has GridBuilder
and FormBuilder
API to build Grid and Form respectively. In addition, parent-child relationship can be built using ModelBuilder.AddChildConfig()
method.
Note
A KeyField is must for an admin page. It can be either declared in GridBuilder
or FormBuilder
.
Configure Mapping using AutoMapper
Create a IMapper using Automapper. For example:
public class BlogMapper
{
public static MapperConfiguration MapperConfiguration;
public static IMapper Mapper;
static BlogMapper()
{
MapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Post, DTO.Post>().ReverseMap();
cfg.CreateMap<Tag, DTO.Tag>().ReverseMap();
cfg.CreateMap<Comments, DTO.Comments>().ReverseMap();
cfg.CreateMap<Category, DTO.Category>().ReverseMap();
});
Mapper = MapperConfiguration.CreateMapper();
}
}
Note
Why mapping is required? Exposing all fields (in particular audit and secure fields) from data layer to UI layer or Web API layer is not secure as well not a best practice. Therefore, mapping from data models to DTOs (Data Transfer Objects) allows to hide audit/secure fields. In addition, this approach decouples the data layer and UI layer.
Create AdminController
Finally, create a AdminController
class inherits AdminController<TAdminConfigurator>
, TAdminConfigurator
is the class that has been created in previous step.
TAdminConfigurator
should implement IAdminConfigurator
interface.
[Module("DemoAdminBlog")]
public class AdminController : AdminController<AdminConfigurator>
{
public AdminController(IServiceProvider serviceProvider)
: base(serviceProvider)
{
}
}
Create a Page
Once the IAdminConfigurator
has been implemetned, each UI model should be hosted on a page. To create a page type Admin, navigate to Admin (click the Deviser logo on top left corner) -> Page Management
.
[Page Management image goes here....]
- Click Add button to create a page.
- Select Page Type as Admin.
- Enter Name and Title for the the Page.
- Select the Module Name. It should match the module name configured in the Module Managment and in the
AdminController
. In this example, select DemoAdminBlog. - Enter the Model Name of UI. It should match the configured UI Model. For example, DTO.Post to display Post UI model in this page.
- Save the page
Page configuration to display Post UI model would look like this
[Page Management to display Post UI model image goes here....]
Result
The generated UI would look like this