Observables¶
Observables can be subscribed to and can potentially emit values. They can be completed, after which no more values will be emitted. On error they will emit an Exception and afterwards can no longer emit a value or be completed.
Observables could emit multiple values, however the observables from FluentRestBuilder will only emit one value and complete afterwards or emit an exception in an error case.
Observables can be awaited. This will return the last emitted value on completion or throw the received exception in an error case.
SingleObservable<T>¶
This is an observable that emits the value that it was given on instancing and completes.
public SingleObservable(T value, IServiceProvider serviceProvider)
Examples
new SingleObservable<string>("example", serviceProvider)
// Alternatively
Observable.Single("example", serviceProvider)
AsyncSingleObservable<T>¶
This observable can be instantiated by providing a callback, which is only executed once the observable is subscribed to.
public AsyncSingleObservable(Func<Task<T>> valueTaskFactory, IServiceProvider serviceProvider)
public AsyncSingleObservable(Func<T> valueFactory, IServiceProvider serviceProvider)
public AsyncSingleObservable(Lazy<T> lazyValue, IServiceProvider serviceProvider)
Examples
new AsyncSingleObservable<string>(() => "example", serviceProvider)
new AsyncSingleObservable<string>(async () => await AsynchronousTask(), serviceProvider)
// Alternatively
Observable.AsyncSingle(() => "example", serviceProvider)
Observable.AsyncSingle(async () => await AsynchronousTask(), serviceProvider)
Controller Integration¶
FluentRestBuilder provides extension methods that can be used to create an observable in a controller. These use the service provider from the controller, so
FluentRestBuilder
public static IProviderObservable<TSource> CreateSingle<TSource>(
this ControllerBase controller, TSource value)
public static IProviderObservable<TSource> CreateAsyncSingle<TSource>(
this ControllerBase controller, Func<Task<TSource>> valueFactory)
public static IProviderObservable<TSource> CreateAsyncSingle<TSource>(
this ControllerBase controller, Func<TSource> valueFactory)
public static IProviderObservable<TSource> CreateAsyncSingle<TSource>(
this ControllerBase controller, Lazy<TSource> valueFactory)
FluentRestBuilder.EntityFrameworkCore
In order for these to work, the entity framework DbContext
has
to be registered for FluentRestBuilder.
See Getting Started for an example.
public static IProviderObservable<TSource> CreateEntitySingle<TSource>(
this ControllerBase controller, Expression<Func<TSource, bool>> predicate)
public static IProviderObservable<TSource> CreateEntitySingle<TSource>(
this ControllerBase controller, params object[] keyValues)
public static IProviderObservable<IQueryable<TSource>> CreateQueryableSingle<TSource>(
this ControllerBase controller)