Sitecore Hackathon 2015 Part II – EntityService in Sitecore.Services.Client

Continued from Part I outlining how Entity Service can be consumed by SPEAK.

EntityService in Sitecore.Services.Client

We opted for EntityService as opposed to ItemService as it allowed us to serve up custom, lean Models of Items and Fields. Containing only properties required for the Module providing we implement EntityIdenitity in the class definition.

namespace DevToolKit.Models
{
public class ItemModel : Sitecore.Services.Core.Model.EntityIdentity
{
public string itemId { get; set; }
public string Name { get; set; }
public List<FieldModel> Fields { get; set; }
}
}

view raw
ItemModel.cs
hosted with ❤ by GitHub

One issue we did lose development time over was defining the itemId property. Without it being defined and exact casing it breaks the integration between SCC and the PageCode. Naming as such did piss off Resharper.

namespace DevToolKit.Models
{
public class FieldModel
{
public string Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public string StandardValue { get; set; }
public string TemplateName { get; set; }
public string TemplateId{ get; set; }
public bool StandardField { get; set; }
public bool RevertToStandardValue { get; set; }
public int SortOrder { get; set; }
public int SectionSortOrder { get; set; }
}
}

view raw
FieldModel.cs
hosted with ❤ by GitHub

To ensure the developer is presented with enough information I implemented the Field’s Template Name and Id, for quick navigation, the fields current Value and it’s Standard Value. To ensure the fields were ordered to match that in the Content Editor, the Model holds the properties SectionSortOrder and SortOrder. Finally, exposing a property defining if the Field is a Standard Field means these fields can be hidden from view if so desired.

One of the good things of SSC is that the Controller can be kept lean by implementing a repository pattern based on Sitecore’s IRepository interface. Thus keeping the useful Create, Read, Update, Delete functions in a highly accessible area of the Solution Architecture.

namespace DevToolKit.Controllers
{
[ValidateAntiForgeryToken]
[ServicesController]
public class SitecoreItemController : EntityService<ItemModel>
{
public SitecoreItemController(IRepository<ItemModel> repository)
: base(repository)
{
}
public SitecoreItemController()
: this(new SitecoreItemRepository())
{
}
}
}

Even if not all functions of the IRepository are required, they must be implemented.

namespace DevToolKit.Repositories
{
public class SitecoreItemRepository : Sitecore.Services.Core.IRepository<ItemModel>
{
private IDataAccess _dataAccess = new DataAccess.DataAccess();
private ISitecoreItemMapper _sitecoreItemMapper = new SitecoreItemMapper();
public void Add(ItemModel entity)
{
throw new NotImplementedException();
}
public void Delete(ItemModel entity)
{
throw new NotImplementedException();
}
public bool Exists(ItemModel entity)
{
Assert.IsNotNull(entity, "entity is required");
return (_dataAccess.GetItem(entity.Id) != null);
}
public ItemModel FindById(string id)
{
Assert.IsNotNullOrEmpty(id, "id is required");
Item item = _dataAccess.GetItem(id);
if (item == null) return new ItemModel();
var sItem = _sitecoreItemMapper.MapToEntity(item);
return sItem;
}
public IQueryable<ItemMode> GetAll()
{
throw new NotImplementedException();
}
public void Update(ItemModel entity)
{
Assert.IsNotNull(entity,"Entity can not be null");
Assert.IsNotNullOrEmpty(entity.Id, "Id can not be null");
_dataAccess.UpdateItem(entity);
}
}
}

For an intro to EntityService and Sitecore.Services.Client Mike has written a great post about it

< Part I – Introduction and Sitecore SPEAK

Part III – Dynamically Setting with Standard Values and Conclusion >

2 thoughts on “Sitecore Hackathon 2015 Part II – EntityService in Sitecore.Services.Client

  1. Pingback: Sitecore Hackathon 2015 Part I – Introduction and Sitecore SPEAK | Exercising Sitecore

  2. Pingback: Sitecore Hackathon 2015 Part III – Dynamically Setting with Standard Values and Conclusion | Exercising Sitecore

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s