Now that we've gone through a simple example, let's dive into the details to understand what's going on.
First, let's review our Product class.
using System;
using Adapdev.Text;
using Elementary.Attributes;
namespace Elementary.Northwind.Samples.Attributes.SimpleExample
{
[ElementaryClass]
public class Product
{
[Persist]
public int CategoryID = 0;
[Persist]
public int ProductID = 0;
[Persist]
public string ProductName = "";
[Persist]
public int SupplierID = 0;
public override string ToString()
{
return StringUtil.ToString(this);
}
}
}
It doesn't get much simpler than this. Obviously, in a real-world scenario you'd want to encapuslate these fields in properties and possibly add business logic and child properties. Elementary can handle all of that, but for now this will suffice.
The [ElementaryClass] attribute tells Elementary to load this class into it's engine. You'll notice that we didn't specify the table name or any columns. Elementary is intelligent enough to infer a lot of that. In the case of the table, it's Products (plural) in the database but the class is Product (singular) - Elementary was able to match those. In the case of the fields, since they match the column names, no additional information is needed. It is possible to specify a table and columns. We could do so in the following manner:
using System;
using Adapdev.Text;
using Elementary.Attributes;
namespace Elementary.Northwind.Samples.Attributes.SimpleExample
{
[ElementaryClass(Table="Products")]
public class Product
{
[Persist(Column="CategoryId")]
public int CategoryID = 0;
[Persist(Column="ProductID")]
public int ProductID = 0;
[Persist(Column="ProductName")]
public string ProductName = "";
[Persist(Column="SupplierID")]
public int SupplierID = 0;
public override string ToString()
{
return StringUtil.ToString(this);
}
}
}
In most cases, however, inference will work well enough.