Creating the OpenAccess data provider

The OpenAccess job applications provider, described in this section, implements the required methods specified in the abstract provider class using Telerik’s OpenAccess ORM.

To implement the OpenAccessJobsDataProvider class, perform the following:

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

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

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

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

    using System.Linq;
    using System.Reflection;
    using Jobs.Model;
    using Telerik.OpenAccess;
    using Telerik.OpenAccess.Metadata;
    using Telerik.Sitefinity.Data;
    using Telerik.Sitefinity.Data.Linq;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Modules.GenericContent.Data;
    using Telerik.Sitefinity.Security;

  5. Change the class definition to:

    [ContentProviderDecorator(typeof(OpenAccessContentDecorator))]
    public class OpenAccessJobsDataProvider : JobsDataProviderBase, IOpenAccessDataProvider
    {
     
    }

  6. Implement the following properties:

    public Database Database
    {
        get;
        set;
    }
     
    public bool UseImplicitTransactions
    {
        get { return true; }
    }
     
    public TransactionMode TransactionConcurrency
    {
        get
        {
            return TransactionMode.PESSIMISTIC_EXPLICIT;
        }
    }

  7. Implement the JobsDataProviderBase by pasting the following code:

    public override JobApplication CreateJobApplication()
    {
        return this.CreateJobApplication(Guid.NewGuid());
    }
     
    public override JobApplication CreateJobApplication(Guid id)
    {
        var dateValue = DateTime.UtcNow;
     
        var item = new JobApplication()
        {
            Id = id,
            ApplicationName = this.ApplicationName,
            Owner = SecurityManager.GetCurrentUserId(),
            DateCreated = dateValue,
            PublicationDate = dateValue
        };
     
        ((IDataItem)item).Provider = this;
     
        if (id != Guid.Empty)
        {
            this.GetContext().Add(item);
        }
     
        return item;
    }
     
    public override IQueryable<JobApplication> GetJobApplications()
    {
        var appName = this.ApplicationName;
     
        var query =
            SitefinityQuery
            .Get<JobApplication>(this, MethodBase.GetCurrentMethod())
            .Where(b => b.ApplicationName == appName);
     
        return query;
    }
     
    public override JobApplication GetJobApplication(Guid id)
    {
        if (id == Guid.Empty)
            throw new ArgumentNullException("id");
     
        var item = this.GetContext().GetItemById<JobApplication>(id.ToString());
        ((IDataItem)item).Provider = this;
        return item;
    }
     
    public override void DeleteJobApplication(JobApplication application)
    {
        var context = this.GetContext();
        if (context != null)
        {
            context.Remove(application);
        }
    }

  8. Define the OpenAccess context by pasting the following code:

    public OpenAccessProviderContext Context
    {
        get;
        set;
    }

  9. Define the meta data source by pasting the following code:

    public MetadataSource GetMetaDataSource(IDatabaseMappingContext context)
    {
        return new JobsFluentMetadataSource(context);
    }

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