Implementing OpenAccess data provider

The Locations module implementation will use an OpenAccess data provider. The OpenAccess data provider must inherit and implement the abstract LocationsDataProvider class. To create an OpenAccess data provider 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 OpenAccessLocationsDataProvider.cs and click Add.
  3. Open the file OpenAccessLocationsDataProvider.cs.
  4. Add the following using statements:

    • using Telerik.Sitefinity.Data;
    • using LocationsModule.Model;
    • using Telerik.Sitefinity.Security;
    • using Telerik.Sitefinity.Model;
    • using Telerik.Sitefinity.Data.Linq;
    • using System.Reflection;
    • using Telerik.OpenAccess.Metadata;
    • using Telerik.Sitefinity.Modules.GenericContent.Data;
  5. Make the OpenAccessLocationsDataProvider class inherit the LocationsDataProvider class and implement theIOpenAccessDataProvider interface:

    public class OpenAccessLocationsDataProvider : LocationsDataProvider, IOpenAccessDataProvider
    {
    }
  6. Implement the LocationsDataProvider abstract class in the following way:

    public override LocationItem CreateLocation()
    {
        return this.CreateLocation(Guid.NewGuid());
    }
     
    public override LocationItem CreateLocation(Guid id)
    {
        var location = new LocationItem();
        location.Id = id;
        location.ApplicationName = this.ApplicationName;
        location.Owner = SecurityManager.GetCurrentUserId();
        var dateValue = DateTime.UtcNow;
        location.DateCreated = dateValue;
        location.PublicationDate = dateValue;
        ((IDataItem)location).Provider = this;
     
        // items with empty guid are used in the UI to get a "blank" data item
        // -> i.e. to fill a data item with default values
        // if this is the case, we leave the item out of the transaction
        if (id != Guid.Empty)
        {
            this.GetContext().Add(location);
        }
        return location;
    }
     
    public override LocationItem GetLocation(Guid id)
    {
        if (id == Guid.Empty)
            throw new ArgumentException("Id cannot be Empty Guid");
     
        // Always use this method. Do NOT change it to query. Catch the exception if the Id can be wrong.
        var location = this.GetContext().GetItemById<LocationItem>(id.ToString());
        ((IDataItem)location).Provider = this;
     
        return location;
    }
     
    public override IQueryable<LocationItem> GetLocations()
    {
        var appName = this.ApplicationName;
     
        var query =
            SitefinityQuery
            .Get<LocationItem>(this, MethodBase.GetCurrentMethod())
            .Where(b => b.ApplicationName == appName);
     
        return query;
    }
     
    public override void DeleteLocation(LocationItem location)
    {
        var scope = this.GetContext();
     
        this.ClearLifecycle(location, this.GetLocations());
        if (scope != null)
        {
            scope.Remove(location);
        }
    }
  7. Implement the IOpenAccessDataProvider members in the following way:

    public OpenAccessProviderContext Context
    {
        get;
        set;
    }
     
    public MetadataSource GetMetaDataSource(IDatabaseMappingContext context)
    {
        return new LocationsFluentMetadataSource(context);
    }

    The GetMetaDataSource method returns an instance of the ContentBaseMetadataSource implementation that defines the fluent mappings for your module.

    NOTE: You will implement the fluent mappings later in this tutorial. For more information, see Implementing fluent mappings.

  8. Mark the OpenAccessLocationsDataProvider class with the [ContentProviderDecorator(typeof(OpenAccessContentDecorator))]attribute.

    [ContentProviderDecorator(typeof(OpenAccessContentDecorator))]
    public class OpenAccessLocationsDataProvider : LocationsDataProvider, IOpenAccessDataProvider

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