Implementing OpenAccess data provider

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

    using System.Reflection;
    using ProductsModule.Model;
    using Telerik.OpenAccess.Metadata;
    using Telerik.Sitefinity.Data;
    using Telerik.Sitefinity.Data.Linq;
    using Telerik.Sitefinity.Lifecycle;
    using Telerik.Sitefinity.Localization;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Modules.GenericContent.Data;
    using Telerik.Sitefinity.Security;
    using Telerik.Sitefinity.Security.Model;
  5. Make the OpenAccessDataProvider class inherit the ProductsDataProvider class and implement the IOpenAccessDataProvider interface:

    public class OpenAccessDataProvider : ProductsDataProvider, IOpenAccessDataProvider
    {
    }
  6. Implement the ProductsDataProvider abstract class in the following way:

    public override ProductItem CreateProduct()
    {
        return this.CreateProduct(Guid.NewGuid());
    }
     
    public override ProductItem CreateProduct(Guid id)
    {
        var product = new ProductItem();
        product.Id = id;
        product.ApplicationName = this.ApplicationName;
        product.Owner = SecurityManager.GetCurrentUserId();
        var dateValue = DateTime.UtcNow;
        product.DateCreated = dateValue;
        product.PublicationDate = dateValue;
        ((IDataItem)product).Provider = this;
     
        // news permissions inherit form the security root
        var securityRoot = this.GetSecurityRoot();
        if (securityRoot != null)
        {
            this.providerDecorator.CreatePermissionInheritanceAssociation(securityRoot, product);
        }
        else
        {
            var msg = Res.Get<SecurityResources>().NoSecurityRoot;
            msg = string.Format(msg, typeof(ProductItem).AssemblyQualifiedName);
            throw new InvalidOperationException(msg);
        }
     
        // 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(product);
        }
     
        return product;
    }
     
    public override ProductItem GetProduct(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 newsItem = this.GetContext().GetItemById<ProductItem>(id.ToString());
        ((IDataItem)newsItem).Provider = this;
        return newsItem;
    }
     
    public override IQueryable<ProductItem> GetProducts()
    {
        var appName = this.ApplicationName;
     
        var query =
            SitefinityQuery
            .Get<ProductItem>(this, MethodBase.GetCurrentMethod())
            .Where(b => b.ApplicationName == appName);
     
        return query;
    }
     
    public override void DeleteProduct(ProductItem product)
    {
        var scope = this.GetContext();
        this.ClearContentLinks(product);
     
        //remove the item from the parent list of inheritors
        var securityRoot = this.GetSecurityRoot();
        if (securityRoot != null)
        {
            List<PermissionsInheritanceMap> parentInheritors = securityRoot.PermissionChildren.Where(c => c.ChildObjectId == product.Id).ToList();
            for (int inheritor = 0; inheritor < parentInheritors.Count(); inheritor++)
            {
                securityRoot.PermissionChildren.Remove(parentInheritors[inheritor]);
            }
        }
     
        //remove the relevant permissions
        this.providerDecorator.DeletePermissions(product);
        this.ClearLifecycle(product, this.GetProducts());
        if (scope != null)
        {
            scope.Remove(product);
        }
     
        this.DeleteItemComments(product.GetType(), product.Id);
    }
     
    public override Telerik.Sitefinity.Lifecycle.LanguageData CreateLanguageData()
    {
        return this.CreateLanguageData(Guid.NewGuid());
    }
     
    public override Telerik.Sitefinity.Lifecycle.LanguageData CreateLanguageData(Guid id)
    {
        var languageData = new LanguageData(this.ApplicationName, id);
        ((IDataItem)languageData).Provider = this;
     
        if (id != Guid.Empty)
        {
            this.GetContext().Add(languageData);
        }
        return languageData;
    }
     
    public override Telerik.Sitefinity.Lifecycle.LanguageData GetLanguageData(Guid id)
    {
        if (id == Guid.Empty)
            throw new ArgumentException("Argument 'id' cannot be empty GUID.");
     
        var languageData = this.GetContext().GetItemById<LanguageData>(id.ToString());
        ((IDataItem)languageData).Provider = this;
        return languageData;
    }
     
    public override IQueryable<Telerik.Sitefinity.Lifecycle.LanguageData> GetLanguageData()
    {
        var appName = this.ApplicationName;
        return SitefinityQuery.Get<LanguageData>(this).Where(c => c.ApplicationName == appName);
    }
  7. Implement the IOpenAccessDataProvider members in the following way:

    public OpenAccessProviderContext Context
    {
        get;
        set;
    }
     
    public MetadataSource GetMetaDataSource(IDatabaseMappingContext context)
    {
        return new ProductsFluentMetadataSource(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 OpenAccessDataProvider class with the ContentProviderDecorator attribute.

    [ContentProviderDecorator(typeof(OpenAccessContentDecorator))]
    public class OpenAccessDataProvider : ProductsDataProvider, 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