Create an EntityService

Abstract

Use an EntityService to access business objects.

You use an EntityService to interact with a custom business object.

Note

Sitecore does not provide any business objects. It is up to you to implement the business objects for the EntityService, but the EntityService implementation provides scaffolding that makes it easier for you to implement the business objects you need.

You can use the EntityService from the client-side JavaScript or through the RESTful API.

This topic describes how to:

To create an EntityService:

  1. Create a class that represents your business object. You must derive this class from Sitecore.Services.Model.EntityIdentity. Add the validation attributes you need.

  2. Create a class for persisting your business object. This class must implement the Sitecore.Services.Core.IRepository<T> interface.

  3. Create a controller class that you derive from Sitecore.Services.Infrastructure.Sitecore.Services.EntityService where T is the business object type that you created in step 1. You must decorate this class with the [ServicesController] attribute and it must have a constructor with no parameters.

    This example shows a services controller definition:

    [ServicesController]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class ExampleController : EntityService<SimpleData>
    {
        public ExampleController(IRepository<SimpleData> entityRepository)
            : base(entityRepository)
        {
        }
        public ExampleController()
            : this(new ExampleRepository())
        {
        }
    }
    

    The EnableCors attribute is optional.

You can separate concerns between the controller class (that delivers the business object to the server over HTTP) and the repository class (that implements the persistence logic to store the business object).

Note

You do not need to tie the repository class to Sitecore. It is possible to define an EntityService that uses the JavaScript and HTTP plumbing code to move business objects to the server, and then stores these objects in a third-party database or other subsystem.

The Sitecore.Services.Contrib project provides a base class you can use when you want to store business objects in the Sitecore content tree. This class gives you a starting point for implementing your own custom repository classes.

To store a business object in Sitecore:

  1. Create your custom repository class, and derive it from Sitecore.Services.Contrib.Data.SitecoreItemRepository<T>.

  2. Implement the following abstract and virtual methods to provide the code to map between your business object properties and the fields of an item:

    protected abstract T CreateModelFrom(Item item); protected abstract string GetItemName(T entity); protected virtual void UpdateFields(T entity, Item item)
    

The Sitecore.Services.Contrib.Data.SitecoreItemRepository<T> class operates on Sitecore items in the master database.

You specify the Sitecore template type and content root to store the items under as constructor parameters when creating the Sitecore.Services.Contrib.Data.SitecoreItemRepository<T> derived type, like this:

protected override Category CreateModelFrom(Item item) protected override string GetItemName(T entity) protected override void UpdateFields(T entity, Item item)
where T : Sitecore.Services.Core.Model.EntityIdentity
Member of 
  • You can add custom action methods to your controller class. They are selected when requests match the {namespace}/{controller}/{id}/{action} route.