Relations
At the lowest level in the component hierarchy, Jerrycurl implements a relational engine for your objects that looks and acts a lot like your database and covers two central concepts in the framework:
- Schemas represent the relational structure of your objects.
- Relations combine attributes from a schema with a matching object source and allows selection of data in relational form (i.e. tuples)
Basic example
We start with a simple object declaration.
class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime CreatedOn { get; set; }
}
Fetch its schema from a central store.
var store = new SchemaStore();
var schema = store.GetSchema<Blog>();
Use the schema to lookup attributes and inspect various forms of metadata.
var title = schema.Lookup("Title");
var createdOn = schema.Lookup("CreatedOn");
Console.WriteLine((title.Type, createdOn.Type));
// (String, DateTime)
Create a header from the selected attributes.
var header = new RelationHeader(schema, new[] { title, createdOn });
Instantiate a data source and associate it with a schema.
var data = new Blog() { Title = "My cat blog", CreatedOn = DateTime.Now };
var model = new Model(schema, data);
Create a relation and enumerate its data in relational form (i.e. tuples).
var relation = new Relation(model, header);
using var reader = relation.GetReader();
while (reader.Read())
Console.WriteLine(reader);
// (My cat blog, 05/10/2024 08:43:43)
Mini-language
var relation = store.From(data)
.Select("Title", "CreatedOn");
foreach (var tuple in relation.Body)
Console.WriteLine(tuple);
Schemas
The use of schemas is central
Schemas are defined as a collection of named attributes that can be referred to in the same way you refer to columns in your database or properties in your object model.
They are defined from each object's structure of your objects and follow a simple set of rules:
- Each schema contains N attributes that match the properties defined by the object.
- Each attribute matches a unique property in the object graph, has a unique named using dot-syntax and has a one-to-one relationship with its parent attribute.
-
Attribute of common list types (
IList
,ICollection
etc.) contain a specialItem
attribute and has a many-to-one relationship with its parent.
Relations
The first way to use the relational concepts of Jerrycurl is to create instances of the Relation
class to probe your objects in a relational way and provide a data source for the ORM process.
A relation can be created as a simple two-step project and uses concepts similar to SELECT
and
JOIN
to view your data.