ApFramework 1.0.1
dotnet add package ApFramework --version 1.0.1
NuGet\Install-Package ApFramework -Version 1.0.1
<PackageReference Include="ApFramework" Version="1.0.1" />
paket add ApFramework --version 1.0.1
#r "nuget: ApFramework, 1.0.1"
// Install ApFramework as a Cake Addin #addin nuget:?package=ApFramework&version=1.0.1 // Install ApFramework as a Cake Tool #tool nuget:?package=ApFramework&version=1.0.1
ApFramework abstracts out cross cutting concerns like authorization, validation, logging, performance monitoring etc. hence keeps code clean without polluting other layers of code.
Additionally it gives predefined functionality to add validation and authorization to processing requests.
Key Features:
- Lightweight async framework.
- RequestAuthorization
- RequestValidation
- Pipeline to inject / add additional actions
- Composite pattern - Requests can be composed by adding Sub-Requests to Parent-Request. All authorizations and validations executed before executing child main action.
To begin lets assume we have a function to Save Empoyee.
async Task<bool> EmployeeSaverFunction(EmployeeModel employee)
{
// Save employee
}
Let's wrap this in a Request and pass it to RequestProcessor.
var saveEmployeeRequest = new Request<bool>(SaveEmployee);
saveEmployeeRequest.RequestBag.Add("EmplooyeeModel", employeeModel);
await requestProcessor.ProcessRequestAsync(saveEmployeeRequest);
// Wrapping function:
private async Task<bool> SaveEmployee(
IRequest request
,IDictionary<string, object> contextBag
,CancellationToken cancellationToken)
{
var model = request.RequestBag["EmplooyeeModel"] as EmployeeModel;
return await EmployeeSaverFunction(model);
}
We can add any number of objects to request bag that can be used by wrapping delegate to use as parameters or context sharing.
How about validating request.
We can add Validator delegate to request which will be called by RequestProcessor before processing Request. If validation fails then validation exception is thrown.
Create Validator
async Task<IRequestValidationResult> ValidateSaveEmployeeRequest(
IRequest request,
IDictionary<string, object> contextBag,
CancellationToken cancellationToken
)
{
var model = request.RequestBag["EmplooyeeModel"] as EmployeeModel;
return await ValidationFactory.ValidateSaveEmployeeRequest(model);
}
Add validator to request.
try
{
var saveEmployeeRequest = new Request<bool>(SaveEmployee, ValidateSaveEmployeeRequest);
.....
}
catch(RequestValidationEception requestValidationException)
{
HandleValidationException(requestValidationException);
}
How about Request Authorization.
Similar to request valdation we can add function to authorize request, Failed request authorization will throw RequestAuthorizationException
Great, but I want to add Global actions which are called at various stages of request processing.
Alright, lets take an example where I wish to log performance of each request processed. So we will register a function that Request Processor will call at start of processing request. This can be called at starting point of application like startup.cs to register global actions.
ApFrameworkHelper.AddApFrameworkGloablAction(StartPerformanceLog, AddtionalActionSequence.First);
Then we add a couple of actions at end.
ApFrameworkHelper.AddApFrameworkGloablAction(StopPerformanceLog, AddtionalActionSequence.Last);
ApFrameworkHelper.AddApFrameworkGloablAction(SavePerformanceLog, AddtionalActionSequence.Last);
We can inject actions at:
public enum AddtionalActionSequence
{
First = 0,
Last = 1,
BeforeAuthorization = 2,
AfterAuthorization = 3,
BeforeValidation = 4,
AfterValidation = 5,
BeforeRequestProcessing = 6,
AfterRequestProcessing = 7
}
Sample implementation of Performance logging functions.
async Task StartPerformanceLog(IRequest request, IDictionary<string, object> contextBag, CancellationToken cancellationToken)
{
var stopWatch = Stopwatch.StartNew();
contextBag.Add("GlobalPerfromanceStopWatch", stopWatch);
await Task.CompletedTask;
}
async Task StopPerformanceLog(IRequest request, IDictionary<string, object> contextBag, CancellationToken cancellationToken)
{
var watch = contextBag["GlobalPerfromanceStopWatch"] as Stopwatch;
watch.Stop();
request.RequestBag.Add("ElapsedTime", watch.ElapsedMilliseconds);
await Task.CompletedTask;
}
async Task SavePerformanceLog(IRequest request, IDictionary<string, object> contextBag, CancellationToken cancellationToken)
{
await SavePerformanceStats(request);
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.5 is compatible. netstandard1.6 was computed. netstandard2.0 was computed. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 1.5
- NETStandard.Library (>= 1.6.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
First cut