ScopeProperties - denmitchell/EDennis.AspNetCore.Base GitHub Wiki
ScopeProperties is a class that is used to transmit request-scoped data to repos and API clients. For example, ScopeProperties can transmit the user name associated with a request to a repo. As another example, ScopeProperties can transmit request headers to API clients using ScopeProperties.
Properties
ScopeProperties exposes two public properties.
| Property | Description |
|---|---|
public string User { get; set; } |
The user name associated with the request |
public Dictionary<string, object> OtherProperties { get; set; } |
A container for other request-scoped data |
IHttpContextAccessor as an Alternative
Microsoft does provide an alternative means of transmitting request-scoped data to custom components -- namely, IHttpContextAccessor. The developer can setup IHttpContextAccessor for injection into a custom component by using the AddHttpContextAccessor IServiceCollection extension method IHttpContextAccessor. The HttpContext provides an Items property that can be used to attach any data to the request.
Although IHttpContextAccessor works just fine in many scenarios, to use it, your library has to include a dependency on Microsoft.AspNetCore.Http, which may be fine for API clients, but may not be desireable for repo classes. Also, in order to unit test classes that inject IHttpContextAccessor, you would have to resort to using something like Moq to mock the injected IHttpContextAccessor object. Because ScopeProperties is a simple POCO class, it has no additional dependencies and it does not require a mocking framework to mock an injected object.