Virtual Columns and Local Binding Properties
EDIT - 2008-02-29: CreateExtendedProperties has been deprecated as of EntitySpaces 2007.1.1015.0. The original text of this article has been retained below, under the "CreateExtendedProperties" header, for those using older versions of EntitySpaces. The examples immediately following apply to all versions since then, including the latest Developer and Trial versions. We highly recommend upgrading to the latest version to take advantage of these, and many other, enhancements.
CreateExtendedProperties is no longer necessary, and has been deprecated. It is replaced by virtual columns and GetLocalBindingProperties().
Virtual Columns: A virtual column is any column that is brought back from a query, a stored procedure, or any other data fetching mechanism that is not in your core table or view. For instance, if you use our new "join" syntax to bring back any extra columns from other tables, these extra columns are referred to as virtual columns. All virtual columns are now bind-able at runtime by default. For instance, if you bring back an extra column named "FullName", that column will exist as a "virtual property" for binding purposes only, and can be seen automatically by whatever you are binding to (not via intellisense though).
The FullName property below is only added to GetLocalBindingProperties() because we want it to show up at design time. Since it is returned in the underlying DataTable, it will always be available at runtime.
Local Binding Properties: All columns added within GetLocalBindingProperties() will be available at design time. Local Properties not brought in as Virtual Columns must be added to GetLocalBindingProperties() to make them available for binding at runtime.
The FakeColumn property below is backed by a private string property "fake", and must be added in GetLocalBindingProperties() if you want to make it available during runtime. The reason you must do this is that it is not contained in the underlying DataTable, and EntitySpaces knows nothing about it. Of course, adding it here also means it will be available at design time.
Code:
namespace BusinessObjects
{
public partial class Employee : esEmployee
{
protected override List<esPropertyDescriptor> GetLocalBindingProperties()
{
List<esPropertyDescriptor> props = new List<esPropertyDescriptor>();
props.Add(new esPropertyDescriptor(this, "FakeColumn", typeof(string)));
props.Add(new esPropertyDescriptor(this, "FullName", typeof(string)));
return props;
}
public string FakeColumn
{
get { return fake; }
set { fake = value; }
}
public string FullName
{
get { return (string)this.GetColumn("FullName"); }
set { this.SetColumn("FullName", value); }
}
private string fake = "wow man";
}
}
The FullName field is brought back by the query below, and can be bound just as if it were a true column in your database.
Code:
EmployeesCollection emps = new EmployeesCollection();
emps.Query.Select
(
emps.Query.EmployeeID,
emps.Query.LastName,
emps.Query.FirstName,
"<[LastName] + ', ' + [FirstName] as FullName>"
);
emps.Query.Load();
bindingSource1.DataSource = emps;
dataGridView1.DataSource = bindingSource1;
CreateExtendedProperties (deprecated as of EntitySpaces 2007.1.1015.0)
EDIT - 2008-02-29: CreateExtendedProperties has been deprecated as of EntitySpaces 2007.1.1015.0. The original text of this article has been retained below for those using older versions of EntitySpaces. The examples under the "Virtual Columns and Local Binding Properties" header above, apply to all versions since then, including the latest Developer and Trial versions. We highly recommend upgrading to the latest version to take advantage of those enhancements.
Notice the override of CreateExtendedProperties in the Collection class. The two extended properties live in the Employee Entity class.
Code:
public partial class EmployeesCollection : esEmployeesCollection
{
protected override void CreateExtendedProperties(esColumnMetadataCollection extendedProps)
{
if (null == extendedProps["NoDataColumn"])
{
esColumnMetadata col = new esColumnMetadata("NoDataColumn", this.Meta.Columns.Count + 1, Type.GetType("System.String"));
col.PropertyName = "NoDataColumn";
col.IsTransient = true;
extendedProps.Add(col);
col = new esColumnMetadata("FullName", this.Meta.Columns.Count + 1, Type.GetType("System.String"));
col.PropertyName = "FullName";
extendedProps.Add(col);
}
}
}
Next we add the two extended properties to our class, one is backed by a value in the underlying DataTable and the other one is not.
Code:
public partial class Employees : esEmployees
{
public string NoDataColumn
{
get { return _noDataColumn; }
set { _noDataColumn = value; }
}
public string FullName
{
get { return base.GetSystemString("FullName"); }
}
private string _noDataColumn = "FakeValue";
}
The NoDataColumn is backed by a private string property. The FullName field is brought back by the query blow and can be bound to just as if it were a true column in your database.
Code:
private void btnLoadAll_Click(object sender, EventArgs e)
{
this.MyEmployeesCollection = new EmployeesCollection();
EmployeesQuery q = this.MyEmployeesCollection.Query;
q.Select(q.EmployeeID, q.FirstName, q.LastName, q.Title, "<[LastName] + ', ' + [FirstName] as FullName>");
q.Load();
this.dataGridView1.DataSource = this.MyEmployeesCollection;
this.dataGridView1.Invalidate(true);
this.dataGridView1.Refresh();
}