Skip to content

Instantly share code, notes, and snippets.

@mskutta
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mskutta/c110c95d5bbd9240ca1d to your computer and use it in GitHub Desktop.
Save mskutta/c110c95d5bbd9240ca1d to your computer and use it in GitHub Desktop.
EvaluateResult Complete Code
public class EvaluateResult : PublishItemProcessor
{
public override void Process(PublishItemContext context)
{
Assert.ArgumentNotNull(context, "context");
// We want to rely on smart publishing so we don't send unnecessary data. Deletes are a weird sceneiro.
// If a Republish is performed, we need the delete actions to come through. We cant rely on the Result.Operation for this.
if ((context.Action != PublishAction.DeleteTargetItem || context.PublishOptions.CompareRevisions) &&
(context.Result.Operation == PublishOperation.Skipped ||
context.Result.Operation == PublishOperation.None))
return;
// Note on Deletes... The Operation of delete will only come though once for one of the published languages.
// The other languages will have a result of none. As a result, we only publish one Delete message regardless of the number of published languages
// Note on HideVersion = true... The operation of a smart publish will always be PublishOperation.Updated. The causes unnecessary processing.
// Dont publish a message if we are not interested in this item based on the base template. This check is faster than performing a service bus publish and checking later.
// For deletes the VersionToPublish is the parent, we need to get the item from the EvaluateAction step
var item = (Item)context.CustomData["Item"] ?? context.VersionToPublish;
if (item == null)
return;
var template = TemplateManager.GetTemplate(item);
if (!_templateIdsToPublish.Any(x => template.ID == new ID(x)))
return;
// Don't publish messages for standard values
if (item.ParentID == item.TemplateID)
return;
// Convert item to model object / entity / dto
var entity = _mapper.Map(item); // This is dependent on the mapper you decide to use.
if (entity == null)
return;
var entityType = entity.GetType();
// Create the message we want to send on the service bus
var operation = (context.Action == PublishAction.DeleteTargetItem && !context.PublishOptions.CompareRevisions) ? PublishOperation.Deleted : context.Result.Operation;
if (operation == PublishOperation.Created)
messageType = typeof(CreatedEvent<>).MakeGenericType(entityType);
if (operation == PublishOperation.Updated)
messageType = typeof(UpdatedEvent<>).MakeGenericType(entityType);
if (operation == PublishOperation.Deleted)
messageType = typeof(DeletedEvent<>).MakeGenericType(entityType);
if (messageType == null)
return;
// Add the entity to the message
var publishEvent = Activator.CreateInstance(messageType);
((IEvent)publishEvent).Id = context.ItemId.Guid;
var property = messageType.GetProperty("Entity");
if (property != null)
property.SetValue(publishEvent, entity, null);
// Publish the event on the service bus
_bus.Publish(publishEvent); // This is dependent on the service bus you decide to use.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment