Databases Reference
In-Depth Information
LISTING 11.12
UnleashedEntity Implementation
using System;
using System.Linq;
using System.Web.DynamicData;
using Unleashed.DataAnnotations;
namespace Unleashed.DynamicData
{
public class UnleashedEntity: DynamicEntity
{
protected override void OnLoad(EventArgs e)
{
this.Load += this.InitializeFromMetadata;
base.OnLoad(e);
}
private void InitializeFromMetadata(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.UIHint))
{
MetaTable table = this.FindMetaTable();
var attribute = table.Attributes.OfType<EntityUIHintAttribute>()
.FirstOrDefault();
if (attribute != null)
this.UIHint = attribute.EntityUIHint;
}
}
}
}
The
DynamicEntity
control creates the entity template in its
OnLoad
method; the
UnleashedEntity
overrides it and relies on the
Load
event handler called
InitializeFromMetadata
to initialize the
UIHint
property. Because the
DynamicEntity
control does not offer a
Table
property, the MetaTable object describing the current entity
has to be located by calling the
FindMetaTable
extension method. This method is actually
defined in the
DynamicDataExtensions
class, but calling it with the extension syntax
makes the code a lot shorter.