Search K
Appearance
You can create and configure a resource for every entity you want to manage with Centias.
Each resource has to extend the abstract class Resource<TEntity, TContext, TIdentifier>. A basic resource looks like this:
using Linkerion.Centias.Pages.Resources;
namespace DemoApp.Resources;
public class EmployeeResource : Resource<Employee, ApplicationDbContext, int>
{
public EmployeeResource(ApplicationDbContext context): base(context) {}
public override IList<IFieldBuilder> Fields() => [];
}The generic types are defined as follows:
TEntity: The entity type the Resource represents.TContext: The database context managing the entity.TIdentifier: The ID type defined for the entity.A resource must implement the Fields() method.
using Linkerion.Centias.Pages.Resources;
namespace DemoApp.Resources;
public class EmployeeResource : Resource<Employee, ApplicationDbContext, int>
{
public EmployeeResource(ApplicationDbContext context) : base(context) {}
public override IList<IFieldBuilder> Fields()
{
return
[
TextField.Make("Name", nameof(Employee.Name)),
EmailField.Make("Email", nameof(Employee.Email),
];
}
}At least one field must be added to a Resource, otherwise a NoFieldsDefinedException will be thrown on startup. Fields correspond to properties in model classes managed by EF Core.
Labels are the text representation of an entity. As default both singular and plural labels are the name of the entity written in code, e.g., for an entity Employee both labels will be Employee. To change the labels, override either Label for the plural version, SingularLabel for the single version, or both.
using Linkerion.Centias.Pages.Resources;
namespace DemoApp.Resources;
public class EmployeeResource : Resource<Employee, ApplicationDbContext, int>
{
public EmployeeResource(ApplicationDbContext context) : base(context) {}
protected override string Label => "Employees";
protected override string SingularLabel => "Employee";
public override IList<IFieldBuilder> Fields()
{
return
[
TextField.Make("Name", nameof(Employee.Name)),
EmailField.Make("Email", nameof(Employee.Email),
];
}
}You can group several entities under the same menu heading by overriding the MenuGroup property, otherwise the group will default to null.
using Linkerion.Centias.Pages.Resources;
namespace DemoApp.Resources;
public class EmployeeResource : Resource<Employee, ApplicationDbContext, int>
{
public EmployeeResource(ApplicationDbContext context) : base(context) {}
protected override string? MenuGroup => "HR";
public override IList<IFieldBuilder> Fields()
{
return
[
TextField.Make("Name", nameof(Employee.Name)),
EmailField.Make("Email", nameof(Employee.Email),
];
}
}You can define the options for items per page by overriding the PerPageOptions property, otherwise default values 10, 20, 40 are used.
using Linkerion.Centias.Pages.Resources;
namespace DemoApp.Resources;
public class EmployeeResource : Resource<Employee, ApplicationDbContext, int>
{
public EmployeeResource(ApplicationDbContext context) : base(context) {}
public override IList<uint> PerPageOptions()
{
return [2, 4];
}
public override IList<IFieldBuilder> Fields()
{
return
[
TextField.Make("Name", nameof(Employee.Name)),
EmailField.Make("Email", nameof(Employee.Email),
];
}
}