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. LocationsFluentMapping
  2. LocationsFluentMetadataSource

Creating the LocationsFluentMapping class

The LocationsFluentMapping class defines the mapping for the LocationItem model class. To create the class, perform the following:

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

    • using Telerik.Sitefinity;
    • using Telerik.OpenAccess.Metadata.Fluent;
    • using Telerik.OpenAccess.Metadata.Fluent.Advanced;
    • using Telerik.Sitefinity.Model;
    • using LocationsModule.Model;
    • using Telerik.OpenAccess;
  5. Make the LocationsFluentMapping class inherit the OpenAccessFluentMappingBase class:

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

    public LocationsFluentMapping(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()
    {
        // initialize and return mappings
        var mappings = new List<MappingConfiguration>();
        MapItem(mappings);
        MapUrlData(mappings);
        return mappings;
    }
     
    private void MapItem(IList<MappingConfiguration> mappings)
    {
        // initialize mapping
        var itemMapping = new MappingConfiguration<LocationItem>();
        itemMapping.HasProperty(p => p.Id).IsIdentity();
        itemMapping.MapType(p => new { }).ToTable("sf_propertyitems");
     
        // add properties
        itemMapping.HasProperty(p => p.Address);
        itemMapping.HasProperty(p => p.City);
        itemMapping.HasProperty(p => p.Region);
        itemMapping.HasProperty(p => p.PostalCode);
     
        // map urls table
        itemMapping.HasAssociation(p => p.Urls).WithOppositeMember("parent", "Parent").ToColumn("content_id").IsDependent().IsManaged();
        mappings.Add(itemMapping);
    }
     
    private void MapUrlData(IList<MappingConfiguration> mappings)
    {
        var urlDataMapping = new MappingConfiguration<LocationItemUrlData>();
        urlDataMapping.MapType(p => new { }).Inheritance(InheritanceStrategy.Flat).ToTable("sf_url_data");
        mappings.Add(urlDataMapping);
    }

Creating the LocationsFluentMetadataSource class

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

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

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

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

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

    In it create an instance of the LocationsFluentMapping 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 LocationsFluentMapping(this.Context));
        return sitefinityMappings;
    }

To use the mappings in the data provider, you must return an instance of the LocationsFluentMetadataSource in the GetMetaDataSourcemethod 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