Beef, and the underlying CoreEx capabilities, need to be set up correctly using Dependency Injection (DI) at startup to function correctly. Given Beef exists primarily to industralize the development of APIs, it is logical to expect this setup to occur during App startup in ASP.NET Core. For example, see My.Hr sample Startup.cs
.
There are a number of core services that need to be configured for CoreEx to function.
// Add the core services.
services.AddSettings<HrSettings>()
.AddExecutionContext()
.AddReferenceDataOrchestrator()
.AddJsonSerializer()
.AddWebApi()
.AddReferenceDataContentWebApi()
.AddJsonMergePatch()
.AddRequestCache()
.AddValidationTextProvider()
.AddValidators<EmployeeManager>();
These are as follows:
Service | Description |
---|---|
AddSettings |
Adds the SettingsBase singleton service; alternatively, where not specifically implemented use AddDefaultSettings . |
AddExecutionContext |
Adds the scoped service to create an ExecutionContext ; the creation can be overridden by specifying the underlying executionContextFactory parameter. |
AddReferenceDataOrchestrator |
Adds the ReferenceDataOrchestrator singleton service to manage the centralized Reference Data orchestration and caching. See also CoreEx.RefData. |
AddJsonSerializer |
Adds the scoped service that implements IJsonSerializer , being either the CoreEx.Text.Json.JsonSerializer or CoreEx.Newtonsoft.Json.JsonSerializer . Also, registers the corresponding IReferenceDataContentJsonSerializer which is an alternate IJsonSerializer designed to serialize (emit) the contents of an IReferenceData . |
AddWebApi |
Adds the WebApi scoped service used by the likes of the ASP.NET Controllers to orchestrate the underlying request operation logic in a standardized/consistent manner. See also CoreEx.WebApis. |
AddReferenceDataContentWebApi |
Adds the ReferenceDataContentWebApi scoped service that uses the specialized IReferenceDataContentJsonSerializer . |
AddJsonMergePatch |
Adds the IJsonMergePatch singleton service using the JsonMergePatch that is resposible for enabling the JSON Merge Patch (application/merge-patch+json ) functionality. |
AddRequestCache |
Adds the IRequestCache scoped service using the RequestCache for short-lived caching used by the likes of the data-service-layer to reduce data-layer chattiness. |
AddValidationTextProvider |
Adds the ITextProvider singleton service using the ValidationTextProvider . See also CoreEx.Validation. |
AddValidators |
Adds all the IValidatorEx types from the specified Assembly as scoped services using reflection. Individual validators can be added singularly using AddValidator . |
As a minumum the AddDatabase
is needed to add the IDatabase
scoped service. This is required to enable the specified Database
instance to be registered.
Where using CoreEx.EntityFramework then the corresponding AddDbContext
(standard Microsoft Entity Framework requirement) is required. As is the AddEfDb
, that adds the IEfDb
scoped service that enables the underlying extended EF capabilities.
Example as follows.
// Add the beef database services (scoped per request/connection).
services.AddDatabase(sp => new HrDb(() => new SqlConnection(sp.GetRequiredService<HrSettings>().DatabaseConnectionString), p.GetRequiredService<ILogger<HrDb>>()));
// Add the beef entity framework services (scoped per request/connection).
services.AddDbContext<HrEfDbContext>();
services.AddEfDb<HrEfDb>();
The CoreEx.Events provides an agnostic, flexible, pluggable, approach to the publishing (formatting, serializing and sending) of EventData
objects.
There are the likes of the NullEventPublisher
and NullEventSender
that can be used for initial development that simplly swallow/discard the events on send.
Example as follows. The event outbox capabilities where required are described further here within the My.Hr sample.
// Add event publishing services.
services.AddNullEventPublisher();
// Add transactional event outbox services.
services.AddScoped<IEventSender>(sp =>
{
var eoe = new EventOutboxEnqueue(sp.GetRequiredService<IDatabase>(), p.GetRequiredService<ILogger<EventOutboxEnqueue>>());
//eoe.SetPrimaryEventSender(/* the primary sender instance; i.e. service bus */); // This is ptional.
return eoe;
});
The CoreEx.Mapping.Mapper provides an alternative to AutoMapper (which can still be used). Designed and implemented as a simple (explicit) IMapper
capability that enables the key Map
, Flatten
and Expand
mapping capabilities. This is no reflection/compiling magic, just specified C# mapping code which executes very fast (and is easily debuggable).
The AddMappers
adds all the IMapper<TSource, TDestination>
types from the specified Assembly
into a singleton Mapper
service using reflection.
See CoreEx.AutoMapper
extensions to implement IMapper
via the AutoMapperWrapper
. The IMapper
is intended to decouple CoreEx from any specific implementation.