Core EAV
Virtufin.Core's Entity-Attribute-Value model: an entity is named by an identifier, an
attribute names one typed value of it, and value providers supply the values. One
provider may serve attributes of several value types. A registry asks its providers in
turn; combinators compose providers. Everything here is in the Virtufin.Core namespace.
The types
classDiagram
class IEntityIdentifier {
<<interface>>
}
class EntityIdentifier~I~
class IAttribute~V~ {
<<interface>>
+Name object
+ValueType Type
}
class Attribute~V, I~ {
<<struct>>
}
class IEntityAttributeValueTypeSpecifier {
<<interface>>
+IsTypeMember(id, attribute, value) bool
+IsTypeMemberForAnyValue(id, attribute) bool
}
class IValueProvider {
<<interface>>
+TypeSpecifier
+GetValue~V~(id, attribute) V?
}
class IObservableProvider {
<<interface>>
+TypeSpecifier
+GetObservable~V~(id, attribute) IObservable~V~?
}
class IRegistry {
<<interface>>
+GetValue~V~(id, attribute) V?
+GetObservable~V~(id, attribute) IObservable~V~?
}
class Registry {
+AddValueProvider(provider)
+AddObservableProvider(provider)
}
EntityIdentifier --|> IEntityIdentifier
Attribute --|> IAttribute
IRegistry <|-- Registry
Registry --> IValueProvider
Registry --> IObservableProvider
IValueProvider --> IEntityAttributeValueTypeSpecifier
| Type | What it is |
|---|---|
IEntityIdentifier |
Names an entity; equality is all it needs (IEquatable<IEntityIdentifier>). |
EntityIdentifier<I> |
An identifier over a value I (string, Guid, …). Two identifiers are equal when they are of the same type and their values are equal. Virtufin.Base.EntityIdentifier adds checked kinds (ISIN, 12 characters; CUSIP, 9; UUID). |
IAttribute |
An attribute without its value type: Name and ValueType. Providers key values by name. |
IAttribute<V> |
Names a value of type V (V : struct); its ValueType is typeof(V). |
Attribute<V, I> |
A struct attribute named by a value I: new Attribute<double, string>("last-trade"). Equal when the type and the name are. Virtufin.Base.Attribute.Price is Attribute<Cash, string>("Price"). |
IEntity |
Anything with an Identifier. |
IEntityAttributeValueTypeSpecifier |
Says which entity, attribute (and value) a provider answers for: IsTypeMember, IsTypeMemberForAnyValue. Attributes are matched by name. |
EntityAttributeValueTypeSpecifier<V> |
A specifier whose values are all V, built from an attribute (any entity), an attribute and one entity or a list of them, or predicates over entity, attribute, value and registry. A value of another type throws AttributeTypeMismatchException. |
EntityAttributeTypeSpecifier |
A specifier over entities and attributes only, for providers of several value types: everything (new()) or a predicate. AnyOf(specifiers) is their union. |
IValueProvider |
Gives a value: TypeSpecifier and V? GetValue<V>(IEntityIdentifier, IAttribute<V>). |
DictionaryValueProvider |
Values kept per (entity, attribute name), of any value types; Add(id, attribute, value) (chains). |
FunctionValueProvider<V> |
Values of type V computed by a function of entity, attribute and registry; or one constant value for an attribute (new FunctionValueProvider<double>(attribute, 3.4)), a fallback. |
IObservableProvider, ObservableProvider |
The same for value streams: IObservable<V>? GetObservable<V>(id, attribute). |
ITaskProvider, TaskProvider |
The same for tasks: Task<V>? GetTask<V>(id, attribute). |
Providers |
Combinators: FirstOf(providers) for values, streams and tasks. |
AttributeTypeMismatchException |
An attribute was requested with another value type than its provider holds; an InvalidCastException with RequestedType and ProvidedType. |
IRegistry |
GetValue<V>(id, attribute) and GetObservable<V>(id, attribute). |
Registry |
The registry: AddValueProvider, AddObservableProvider. |
Missing values and type mismatches
A provider answers null when it has no value for the entity and attribute. When it holds
the attribute's name with another value type (a double "last-trade" asked for as an
int), it throws AttributeTypeMismatchException, whose message names both types: a
mismatch is a programming error, never "no value".
Combining providers
Providers.FirstOf(p1, p2, …) is a provider that asks p1, p2, … in order and answers
with the first value that is not null; null when none has one. A mismatch in any
provider it asks propagates. The same overloads take observable and task providers, and an
IEnumerable, which is enumerated on every lookup.
How the registry answers
Registry.GetValue<V>(id, attribute) asks its value providers, newest first
(Providers.FirstOf over them): the first whose type specifier accepts the entity and
attribute and that has a value answers. Without
one, the result is null. So a provider added last overrides earlier ones, and a provider
added first serves as the fallback. GetObservable<V> does the same over the observable
providers.
GetValue returns V?: always check HasValue.
Usage
using Virtufin.Core;
var lastTrade = new Attribute<double, string>("last-trade");
var btc = new EntityIdentifier<string>("BTC_USDT");
var eth = new EntityIdentifier<string>("ETH_USDT");
var registry = new Registry();
// Added first, asked last: 0 for any entity without its own value.
registry.AddValueProvider(new FunctionValueProvider<double>(lastTrade, 0.0));
// Added last, asked first: the values we know.
registry.AddValueProvider(new DictionaryValueProvider(
new EntityAttributeValueTypeSpecifier<double>(lastTrade))
.Add(btc, lastTrade, 64_250.5));
double? btcPrice = registry.GetValue(btc, lastTrade); // 64250.5
double? ethPrice = registry.GetValue(eth, lastTrade); // 0 (the fallback)
// One provider, several value types; a combinator with a fallback.
var volume = new Attribute<long, string>("volume");
var quotes = Providers.FirstOf(
new DictionaryValueProvider(new EntityAttributeTypeSpecifier())
.Add(btc, lastTrade, 64_250.5)
.Add(btc, volume, 1_200L),
new FunctionValueProvider<double>(lastTrade, 0.0));
long? btcVolume = quotes.GetValue(btc, volume); // 1200
quotes.GetValue(btc, new Attribute<int, string>("volume"));
// throws AttributeTypeMismatchException: requested Int32, held Int64
Why the library models its domain this way: Design decisions.