Implementing fluent mappings

The fluent mappings map your model classes to database tables. To implement the fluent mappings for your data provider, you must create the following classes:

  1. ProductsFluentMapping
  2. ProductsFluentMetadataSource

Creating the ProductsFluentMapping class

The ProductsFluentMapping class defines the mapping for the ProductItem model class. To create the class, perform the following:

  1. From the context menu of the folder Data » Implementation in the ProductsModule project, click Add » Class.
  2. Name the class file ProductsFluentMapping.cs and click Add.
  3. Open the file ProductsFluentMapping.cs.
  4. Add the following using statements:

    using ProductsModule.Model;
    using Telerik.OpenAccess;
    using Telerik.OpenAccess.Metadata.Fluent;
    using Telerik.OpenAccess.Metadata.Fluent.Advanced;
    using Telerik.Sitefinity;
    using Telerik.Sitefinity.Model;
  5. Make the ProductsFluentMapping class inherit the OpenAccessFluentMappingBase class:

    public class ProductsFluentMapping : OpenAccessFluentMappingBase
    {
    }
  6. Create the following constructor for the class:

    public ProductsFluentMapping(IDatabaseMappingContext context)
        : base(context)
    {
    }
  7. Implement the OpenAccessFluentMappingBase abstract class.

    To implement it, you must override the GetMappings abstract method. In it, you must create the mappings. Following is the code for the method:

    public override IList<MappingConfiguration> GetMapping()
    {
        var mappings = new List<MappingConfiguration>();
        MapItem(mappings);
        MapUrlData(mappings);
        return mappings;
    }
     
    private void MapItem(IList<MappingConfiguration> mappings)
    {
        var itemMapping = new MappingConfiguration<ProductItem>();
        itemMapping.HasProperty(p => p.Id).IsIdentity();
        itemMapping.MapType(p => new { }).ToTable("custom_products");
        itemMapping.HasProperty(p => p.Price);
        itemMapping.HasProperty(p => p.QuantityInStock);
        itemMapping.HasAssociation<Telerik.Sitefinity.Security.Model.Permission>(p => p.Permissions);
        itemMapping.HasProperty(p => p.InheritsPermissions);
        itemMapping.HasAssociation<Telerik.Sitefinity.Security.Model.PermissionsInheritanceMap>(p => p.PermissionChildren);
        itemMapping.HasProperty(p => p.CanInheritPermissions);
        itemMapping.HasAssociation(p => p.Urls).WithOppositeMember("parent", "Parent").ToColumn("content_id").IsDependent().IsManaged();
        itemMapping.HasAssociation<Telerik.Sitefinity.Workflow.Model.Tracking.ApprovalTrackingRecordMap>(p => p.ApprovalTrackingRecordMap);
     
        //map language data & published translations
        CommonFluentMapping.MapILifecycleDataItemFields<ProductItem>(itemMapping, this.Context);
     
        mappings.Add(itemMapping);
    }
     
    private void MapUrlData(IList<MappingConfiguration> mappings)
    {
        var urlDataMapping = new MappingConfiguration<ProductItemUrlData>();
        urlDataMapping.MapType(p => new { }).Inheritance(InheritanceStrategy.Flat).ToTable("sf_url_data");
        mappings.Add(urlDataMapping);
    }

Creating the ProductsFluentMetadataSource class

The ProductsFluentMetadataSource class wraps the mappings and exposes them to the OpenAccess data provider. To create the class, perform the following:

  1. From the context menu of the folder Data » Implementation in the ProductsModule project, click Add » Class.
  2. Name the class file ProductsFluentMetadataSource.cs and click Add.
  3. Open the file ProductsFluentMetadataSource.cs.
  4. Add the following using statements:

    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Modules.GenericContent.Data;
  5. Make the ProductsFluentMetadataSource class inherit the ContentBaseMetadataSource class:

    public class ProductsFluentMetadataSource : ContentBaseMetadataSource
    {
    }
  6. Create the following constructors for the class:

    public ProductsFluentMetadataSource()
        : base(null)
    { }
     
     
    public ProductsFluentMetadataSource(IDatabaseMappingContext context)
        : base(context)
    {
    }
  7. Override the BuildCustomMappings method.

    In it, create an instance of the ProductsFluentMapping class and add the instance to the default set of mappings. Following is the code for the method:

    protected override IList<IOpenAccessFluentMapping> BuildCustomMappings()
    {
        var sitefinityMappings = base.BuildCustomMappings();
        sitefinityMappings.Add(new ProductsFluentMapping(this.Context));
        return sitefinityMappings;
    }

To use the mappings in the data provider, you must return an instance of the ProdusFluentMetadataSource in the GetMetaDataSource method of the provider. For more information, see Implementing OpenAccess data provider.

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK