Last active
August 29, 2015 14:22
-
-
Save mskutta/7db4b97b9a740212cfe5 to your computer and use it in GitHub Desktop.
EvaluateResult Service Bus Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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