Data Access has been discontinued. Please refer to this page for more information.

Struct Support

Telerik Data Access Fluent Mapping API provides built-in support for struct objects. Suppose, you have a class with a struct property like the one shown below:

public struct Category
{
   public string Name{get;set;}
   public int Version{get;set;}
}

public class Product
{
   public int Id{get;set;}
   public string Name{get;set;}
   public Category Category{get;set;}
}
Public Structure Category
    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
    Private _version As Integer
    Public Property Version() As Integer
        Get
            Return _version
        End Get
        Set(ByVal value As Integer)
            _version = value
        End Set
    End Property
End Structure

Public Class Product
    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 _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
    Private _category As Category
    Public Property Category() As Category
        Get
            Return _category
        End Get
        Set(ByVal value As Category)
            _category = value
        End Set
    End Property
End Class

You can configure structs by using the generic StructConfiguration<T> class.

StructConfiguration<Category> structConfiguration = new StructConfiguration<Category>();
structConfiguration.MapType( x => new
{
   CategoryName = x.Name,
   CategoryVersion = x.Version
} );
Dim _structConfiguration As New StructConfiguration(Of Category)()
_structConfiguration.MapType(Function(x) New With {Key .CategoryName = x.Name,
               Key .CategoryVersion = x.Version})
_structConfiguration.FieldNamingRules.AddPrefix = "_"

This struct configuration can then be applied to a property:

MappingConfiguration<Product> productConfiguration = new MappingConfiguration<Product>();
productConfiguration.MapType( p => new
                                       {
                                           ProductId = p.Id,
                                           ProductName = p.Name
                                       } ).ToTable( "Products" );
productConfiguration.HasProperty( p => p.Id ).IsIdentity( KeyGenerator.Autoinc );

productConfiguration.HasStruct( p => p.Category ).WithMapping( structConfiguration );
Dim productConfiguration As New MappingConfiguration(Of Product)()
productConfiguration.MapType(Function(p) New With {Key .ProductId = p.ID,
               Key .ProductName = p.Name}).ToTable("Products")
productConfiguration.HasProperty(Function(p) p.ID).IsIdentity(KeyGenerator.Autoinc)
productConfiguration.FieldNamingRules.AddPrefix = "_"

productConfiguration.HasStruct(Function(p) p.Category).WithMapping(_structConfiguration)

Other possible approaches are:

  • Directly configure the struct by using the configuration object for the parent class and the MapType method:

    productConfiguration.HasStruct(p => p.Category).MapType(c => new
    {
       CategoryName = c.Name,
       CategoryVersion = c.Version
    });
    
    productConfiguration.HasStruct(Function(p) p.Category).MapType(Function(c) New With {
        Key .CategoryName = c.Name,
        Key .CategoryVersion = c.Version})
    
  • Directly configure the struct by using the HasProperty and HasColumn methods:

    productConfiguration.HasStruct( p => p.Category ).HasProperty( c => c.Name ).
        ToColumn( "CategoryName" );
    productConfiguration.HasStruct( p => p.Category ).HasProperty( c => c.Version ).
        ToColumn( "CategoryVersion" );
    
    productConfiguration.HasStruct(Function(p) p.Category).HasProperty(Function(c) c.Name). _
        ToColumn("CategoryName")
    productConfiguration.HasStruct(Function(p) p.Category).HasProperty(Function(c) c.Version). _
        ToColumn("CategoryVersion")
    

Relational Side

Each class that has a struct (or a graph of struct objects) receives extra columns in its table for each primitive property of the struct (or nested structs). You can have two or more structs of the same type defined in different properties, however then you cannot reuse the StructConfiguration object for them (can’t map them to the same columns). You can reause the StructConfiguration object in different classes though.

You can implement nested structs, however associations to and from structs are not currently supported.

So, the previous Product/Category configuration will be represented in the following way in the database: