Use Flux
Use IFluxContext in your app
To use Flux in your application, you need to resolve the IFluxContext
from your DI container. Normally, this is done by injecting it into your class's constructor.
public class YourClass
{
private readonly IFluxContext _fluxContext;
public YourClass(IFluxContext fluxContext)
{
_fluxContext = fluxContext;
}
}
You can also retrieve it from the IServiceProvider
instance.
var fluxContext = serviceProvider.GetRequiredService<IFluxContext>();
Once you have the IFluxContext
instance, you can use it to interact with your external services.
Working with a specific external service
If you have multiple external services configured, you can specify which one you want to use by calling the Service
method on the IFluxContext
instance.
var externalService1 = _fluxContext.Service("service1");
Understanding Sets
Flux uses a concept of Sets to represent an external collection of data of a specific type. For external WebApis, this is usually a collection of models that you can retrieve using the same endpoint.
Let's say the external WebAPI uses REST and looks something like this:
GET https://test.com/students // returns either all students, or a page of students
POST https://test.com/students // creates a new student
GET https://test.com/students/1 // returns a student with id 1
PUT https://test.com/students/1 // updates a student with id 1
DELETE https://test.com/students/1 // deletes a student with id 1
In Flux, this collection of endpoints will be represented by a Set registered for type Student
. You can retrieve this Set's context by calling the Set
method on the IFluxContext
instance.
// resolve the Set directly
var studentSet = _fluxContext.Set<Student>();
// Or explicitly specify the external service you are working with
var studentSet = _fluxContext.Service("service1").Set<Student>();
ℹ️ Selecting a specific service can be useful in situations where you have multiple external services configured with the same model types.
Using external services
Once you have retrieved the Set's context, you can use it to interact with the external service.
var studentSet = _fluxContext.Set<Student>();
var student = await studentSet.GetAsync(1); // Will make an http request to https://test.com/students/1
var list = await studentSet.GetAllAsync(); // Will make an http request to https://test.com/students
var page = await studentSet.GetPageAsync(0, 10); // Will make an http request to https://test.com/students?offset=0&limit=10
Working with multiple Sets of the same type
You can configure multiple Sets for the same model type simultaneously. This can be useful if you need to work with the same model type, but with different endpoints.
For example, if you have a WebApi that exposes a collection of books, but you need to work with two different endpoints for this collection, you can configure two Sets for the same model type:
flux.AddService("service1")
.UsingRest("https://test.com")
.AddSet<Book>("books1")
.WithEndpoint("books")
.AddSet<Book>("books2")
.WithEndpoint("additional-endpoint-for-books");
You can then retrieve the Set's context by specifying the name of the Set you want to be working with.
// resolve external service implicitly:
var books1Set = _fluxContext.Set<Book>("books1");
var books2Set = _fluxContext.Set<Book>("books2");
// or explicitly specify the external service:
var books1Set = _fluxContext.Service("service1").Set<Book>("books1");
var books2Set = _fluxContext.Service("service1").Set<Book>("books2");