Mixing CLR Types and Artificial Properties
The purpose of this tutorial is to show you how to add artificial properties to CLR types.
The following example shows how to create two additional artificial properties (Age and Picture) for the Person class. First, you need to create a new instance of the generic MappingConfiguration<T> class, by using the Person class. Next, use the well-known HasArtificialPrimitiveProperty<T>, HasArtificialStringProperty and HasArtificialProperty<T> methods.
All methods for configuring artificial types are located in the Telerik.OpenAccess.Metadata.Fluent.Artificial namespace.
In the following example, two additional properties Age and Picture are added to the Person type.
public class FluentModelMetadataSource : FluentMetadataSource
{
protected override IList<MappingConfiguration> PrepareMapping()
{
List<MappingConfiguration> configurations = new List<MappingConfiguration>();
MappingConfiguration<Person> personConfiguraiton = new MappingConfiguration<Person>();
personConfiguraiton.MapType( p => new
{
PersonId = p.Id,
LastName = p.LastName,
FirstName = p.FirstName,
Contact = p.Address
} ).ToTable( "People" );
personConfiguraiton.HasProperty( p => p.Id ).IsIdentity( KeyGenerator.Autoinc );
personConfiguraiton.HasArtificialPrimitiveProperty<int>( "Age" );
personConfiguraiton.HasArtificialProperty<byte[]>( "Picture" );
configurations.Add( personConfiguraiton );
return configurations;
}
}
Public Class FluentModelMetadataSource
Inherits FluentMetadataSource
Protected Overrides Function PrepareMapping() As IList(Of MappingConfiguration)
Dim configurations As New List(Of MappingConfiguration)()
Dim personConfiguraiton As New MappingConfiguration(Of Person)()
personConfiguraiton.MapType(Function(p) New With {Key .PersonId = p.Id,
Key .LastName = p.LastName,
Key .FirstName = p.FirstName,
Key .Contact = p.Address}).ToTable("People")
personConfiguraiton.HasProperty(Function(p) p.Id).IsIdentity(KeyGenerator.Autoinc)
personConfiguraiton.FieldNamingRules.AddPrefix = "_"
personConfiguraiton.HasArtificialPrimitiveProperty(Of Integer)("Age")
personConfiguraiton.HasArtificialProperty(Of Byte())("Picture")
configurations.Add(personConfiguraiton)
Return configurations
End Function
End Class
Person Class
public class Person
{
public int Id {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string Address {get;set;}
}
Public Class Person
Private _id As Integer
Public Property Id() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Private _firstName As String
Public Property FirstName() As String
Get
Return _firstName
End Get
Set(ByVal value As String)
_firstName = value
End Set
End Property
Private _lastName As String
Public Property LastName() As String
Get
Return _lastName
End Get
Set(ByVal value As String)
_lastName = value
End Set
End Property
Private _address As String
Public Property Address() As String
Get
Return _address
End Get
Set(ByVal value As String)
_address = value
End Set
End Property
End Class