Defining the abstract data provider

An abstract data provider class defines required methods. To operate with the custom data model, inheritors must implement these methods.

To create an abstract data provider class for your custom JobApplication model, perform the following:

  1. From the context menu of the solution, click Add » New Item...

  2. In the left pane, select Visual C# » Code.

  3. Click Class and in the Name input field, enter JobsDataProviderBase.

  4. Open the file JobsDataProviderBase.cs and add the following namespaces:

    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using Jobs.Model;
    using Telerik.Sitefinity.GenericContent.Model;
    using Telerik.Sitefinity.Modules.GenericContent;
  5. Change the class definition to:

    public abstract class JobsDataProviderBase : ContentDataProviderBase
    {
     
    }
  6. Define the root key by pasting the following code:

    public override string RootKey
    {
        get
        {
            return "JobsDataProvider";
        }
    }
  7. Define the abstract data provider methods by pasting the following code:

    public abstract JobApplication CreateJobApplication();
     
    public abstract JobApplication CreateJobApplication(Guid id);
     
    public abstract IQueryable<JobApplication> GetJobApplications();
     
    public abstract JobApplication GetJobApplication(Guid id);
     
    public abstract void DeleteJobApplication(JobApplication application);
  8. Implement ContentDataProviderBase by pasting the following code:

    public override object CreateItem(Type itemType, Guid id)
    {
        if (itemType == null)
            throw new ArgumentNullException("itemType");
     
        if (itemType == typeof(JobApplication))
            return this.CreateJobApplication(id);
     
        throw GetInvalidItemTypeException(itemType, this.GetKnownTypes());
    }
     
    public override object GetItem(Type itemType, Guid id)
    {
        if (itemType == null)
            throw new ArgumentNullException("itemType");
     
        if (itemType == typeof(JobApplication))
            return this.GetJobApplication(id);
     
        return base.GetItem(itemType, id);
    }
     
    public override void DeleteItem(object item)
    {
        if (item == null)
            throw new ArgumentNullException("item");
     
        var itemType = item.GetType();
     
        if (itemType == typeof(JobApplication))
        {
            this.DeleteJobApplication((JobApplication)item);
            return;
        }
     
        throw GetInvalidItemTypeException(item.GetType(), this.GetKnownTypes());
    }
     
    public override IEnumerable GetItems(Type itemType, string filterExpression, string orderExpression, int skip, int take, ref int? totalCount)
    {
        if (itemType == null)
            throw new ArgumentNullException("itemType");
     
        if (itemType == typeof(Comment))
        {
            return new List<Comment>();
        }
     
        if (itemType == typeof(JobApplication))
            return SetExpressions(this.GetJobApplications(), filterExpression, orderExpression, skip, take, ref totalCount);
     
        throw GetInvalidItemTypeException(itemType, this.GetKnownTypes());
    }
     
    public override Type[] GetKnownTypes()
    {
        return new[] { typeof(JobApplication) };
    }
     
    public override Type GetParentTypeFor(Type contentType)
    {
        return null;
    }
     
    public override IEnumerable GetItemsByTaxon(Guid taxonId, bool isSingleTaxon, string propertyName, Type itemType, string filterExpression, string orderExpression, int skip, int take, ref int? totalCount)
    {
        return null;
    }
     
    public override Type GetUrlTypeFor(Type itemType)
    {
        return null;
    }

First, you make the class inherit from ContentDataProviderBase, the default provider base class for content-based models. Then, you specify the methods required to perform CRUD operations on your JobApplications model. The methods CreateItem, GetItem, GetItems,DeleteItem are required. The method GetKnownTypes() returns the model types, which the provider can operate on - in this caseJobApplication.

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