Entity Links
A page that lists records of many types, e.g. the audit log, needs a way to open a record from its type and ID: the dialog of its row, a page of its own, or nothing when the user can't see it. Entity links resolve that on the server, for many records at once, and the providers that decide it can be added by the application.
Resolving Links
IEntityLinkResolver, which services.AddEntityLinks() registers, gives the links of some records for the user of a
request:
var links = linkResolver.Resolve([
new EntityReference("Northwind.Order", "10248"),
new EntityReference("Administration.User", "5", Label: "nancy")
], requestContext);
A link is null for a record that the user can't open. An EntityLink has:
| Property | |
|---|---|
DialogType |
The dialog to open, e.g. Northwind.Order. |
DialogId |
The ID to load in it. |
DialogTab |
A tab to show, if any. |
Url |
An address to go to instead, or when the dialog can't be loaded. |
Title |
A title for the link. |
The default provider gives the dialog of the row whose entity type the reference has, when the user has its read
permission, and the address of its page by convention, e.g. ~/Northwind/Order#edit/10248.
Adding Providers
A provider can give a link of its own, e.g. a page instead of a dialog, or none for some records. Any number of them can
be registered; for each record, the first that gives a link, by Order, wins:
public class CustomerLinkProvider : IEntityLinkProvider
{
public int Order => -10;
public void Resolve(IReadOnlyList<EntityReference> references, EntityLink?[] links, IRequestContext context)
{
for (var i = 0; i < references.Count; i++)
{
if (links[i] == null && references[i].EntityType == "Northwind.Customer")
links[i] = new EntityLink { Url = "~/Northwind/CustomerDetails/" + references[i].EntityId };
}
}
}
services.TryAddEnumerable(ServiceDescriptor.Singleton<IEntityLinkProvider, CustomerLinkProvider>());
Opening a Link
On the client, openEntityLink(link) of @serenity-is/extensions opens the dialog of a link when its type can be loaded,
else goes to its address, and canOpenEntityLink(link) tells whether it can do either.
The Audit Log opens the records of its events with them.