From a925b092e75fdbc786a814767eb93986a7dce1c1 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Sat, 15 Dec 2012 21:04:17 +0500 Subject: [PATCH] Created new event structure (currently only implemented for Saving/Saved) and new events CollectionSaving, CollectionSaved. Created new way to raise events and handle their cancellations. Everything is strongly typed so should make working with events much nicer for end users. Also ensures that the Sender is the object raising the event. --- .../Events/CancellableObjectEventArgs.cs | 59 +++++ src/Umbraco.Core/Events/EventExtensions.cs | 49 ++++ .../Events/ICancellableObjectEventArgs.cs | 18 ++ .../Events/PublishingEventArgs.cs | 22 +- src/Umbraco.Core/Events/SaveEventArgs.cs | 13 +- src/Umbraco.Core/Events/TypedEventHandler.cs | 7 + src/Umbraco.Core/Services/ContentService.cs | 226 +++++++++--------- .../Services/ContentTypeService.cs | 199 +++++++-------- src/Umbraco.Core/Services/DataTypeService.cs | 33 ++- src/Umbraco.Core/Services/FileService.cs | 106 ++++---- .../Services/LocalizationService.cs | 70 +++--- src/Umbraco.Core/Services/MacroService.cs | 31 +-- src/Umbraco.Core/Services/MediaService.cs | 60 +++-- src/Umbraco.Core/Umbraco.Core.csproj | 4 + .../Services/ContentServiceTests.cs | 3 +- 15 files changed, 532 insertions(+), 368 deletions(-) create mode 100644 src/Umbraco.Core/Events/CancellableObjectEventArgs.cs create mode 100644 src/Umbraco.Core/Events/EventExtensions.cs create mode 100644 src/Umbraco.Core/Events/ICancellableObjectEventArgs.cs create mode 100644 src/Umbraco.Core/Events/TypedEventHandler.cs diff --git a/src/Umbraco.Core/Events/CancellableObjectEventArgs.cs b/src/Umbraco.Core/Events/CancellableObjectEventArgs.cs new file mode 100644 index 0000000000..b4efcca229 --- /dev/null +++ b/src/Umbraco.Core/Events/CancellableObjectEventArgs.cs @@ -0,0 +1,59 @@ +using System; +using System.Security.Permissions; +using Umbraco.Core.Models; +using Umbraco.Core.Models.EntityBase; + +namespace Umbraco.Core.Events +{ + /// + /// Event args for a strongly typed object that can support cancellation + /// + /// + [HostProtection(SecurityAction.LinkDemand, SharedState = true)] + public class CancellableObjectEventArgs : EventArgs, ICancellableObjectEventArgs + { + private bool _cancel; + + public CancellableObjectEventArgs(T entity, bool canCancel) + { + Entity = entity; + CanCancel = canCancel; + } + + public CancellableObjectEventArgs(T entity) + : this(entity, true) + { + } + + /// + /// Flag to determine if this instance will support being cancellable + /// + public bool CanCancel { get; set; } + + /// + /// If this instance supports cancellation, this gets/sets the cancel value + /// + public bool Cancel + { + get + { + if (!CanCancel) + { + throw new InvalidOperationException("This event argument class does not support cancelling."); + } + return _cancel; + } + set + { + if (!CanCancel) + { + throw new InvalidOperationException("This event argument class does not support cancelling."); + } + _cancel = value; + } + } + + public T Entity { get; private set; } + + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Events/EventExtensions.cs b/src/Umbraco.Core/Events/EventExtensions.cs new file mode 100644 index 0000000000..1914c411be --- /dev/null +++ b/src/Umbraco.Core/Events/EventExtensions.cs @@ -0,0 +1,49 @@ +using System; + +namespace Umbraco.Core.Events +{ + /// + /// Extension methods for cancellable event operations + /// + public static class EventExtensions + { + /// + /// Raises the event and returns a boolean value indicating if the event was cancelled + /// + /// + /// + /// + /// + /// + /// + public static bool IsRaisedEventCancelled( + this TypedEventHandler eventHandler, + TArgs args, + TSender sender) + where TArgs : ICancellableObjectEventArgs + { + if (eventHandler != null) + eventHandler(sender, args); + + return !args.Cancel; + } + + /// + /// Raises the event + /// + /// + /// + /// + /// + /// + public static void RaiseEvent( + this TypedEventHandler eventHandler, + TArgs args, + TSender sender) + where TArgs : EventArgs + { + if (eventHandler != null) + eventHandler(sender, args); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Events/ICancellableObjectEventArgs.cs b/src/Umbraco.Core/Events/ICancellableObjectEventArgs.cs new file mode 100644 index 0000000000..871362d9db --- /dev/null +++ b/src/Umbraco.Core/Events/ICancellableObjectEventArgs.cs @@ -0,0 +1,18 @@ +namespace Umbraco.Core.Events +{ + /// + /// Interface for EventArgs clases to implement when they support cancelling operations + /// + public interface ICancellableObjectEventArgs + { + /// + /// If this instance supports cancellation, this gets/sets the cancel value + /// + bool Cancel { get; set; } + + /// + /// Flag to determine if this instance will support being cancellable + /// + bool CanCancel { get; set; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Events/PublishingEventArgs.cs b/src/Umbraco.Core/Events/PublishingEventArgs.cs index f762695f70..c58e3bac15 100644 --- a/src/Umbraco.Core/Events/PublishingEventArgs.cs +++ b/src/Umbraco.Core/Events/PublishingEventArgs.cs @@ -1,17 +1,17 @@ namespace Umbraco.Core.Events { - public class PublishingEventArgs : System.ComponentModel.CancelEventArgs + public class PublishingEventArgs : System.ComponentModel.CancelEventArgs { - public PublishingEventArgs() - { - IsAllRepublished = false; - } + public PublishingEventArgs() + { + IsAllRepublished = false; + } - public PublishingEventArgs(bool isAllPublished) - { - IsAllRepublished = isAllPublished; - } + public PublishingEventArgs(bool isAllPublished) + { + IsAllRepublished = isAllPublished; + } - public bool IsAllRepublished { get; private set; } - } + public bool IsAllRepublished { get; private set; } + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Events/SaveEventArgs.cs b/src/Umbraco.Core/Events/SaveEventArgs.cs index c3654586dd..6f3921e12c 100644 --- a/src/Umbraco.Core/Events/SaveEventArgs.cs +++ b/src/Umbraco.Core/Events/SaveEventArgs.cs @@ -1,4 +1,15 @@ +using Umbraco.Core.Models.EntityBase; + namespace Umbraco.Core.Events { - public class SaveEventArgs : System.ComponentModel.CancelEventArgs { } + public class SaveEventArgs : CancellableObjectEventArgs + { + public SaveEventArgs(TEntity entity, bool canCancel) : base(entity, canCancel) + { + } + + public SaveEventArgs(TEntity entity) : base(entity) + { + } + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Events/TypedEventHandler.cs b/src/Umbraco.Core/Events/TypedEventHandler.cs new file mode 100644 index 0000000000..a8170190d4 --- /dev/null +++ b/src/Umbraco.Core/Events/TypedEventHandler.cs @@ -0,0 +1,7 @@ +using System; + +namespace Umbraco.Core.Events +{ + [Serializable] + public delegate void TypedEventHandler(TSender sender, TEventArgs e); +} \ No newline at end of file diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index aa41767e32..619ee13165 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; @@ -603,64 +604,56 @@ namespace Umbraco.Core.Services var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateContentRepository(uow)) { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(content, e); + if (!Saving.IsRaisedEventCancelled(new SaveEventArgs(content), this)) + return false; - if (!e.Cancel) + //Check if parent is published (although not if its a root node) - if parent isn't published this Content cannot be published + if (content.ParentId != -1 && content.ParentId != -20 && HasPublishedVersion(content.ParentId) == false) { - //Check if parent is published (although not if its a root node) - if parent isn't published this Content cannot be published - if (content.ParentId != -1 && content.ParentId != -20 && HasPublishedVersion(content.ParentId) == false) - { - LogHelper.Info( - string.Format( - "Content '{0}' with Id '{1}' could not be published because its parent is not published.", - content.Name, content.Id)); - return false; - } - - //Content contains invalid property values and can therefore not be published - fire event? - if (!content.IsValid()) - { - LogHelper.Info( - string.Format( - "Content '{0}' with Id '{1}' could not be published because of invalid properties.", - content.Name, content.Id)); - return false; - } - - //Publish and then update the database with new status - bool published = _publishingStrategy.Publish(content, userId); - - //Since this is the Save and Publish method, the content should be saved even though the publish fails or isn't allowed - SetWriter(content, userId); - repository.AddOrUpdate(content); - uow.Commit(); - - if (published) - { - var xml = content.ToXml(); - var poco = new ContentXmlDto { NodeId = content.Id, Xml = xml.ToString(SaveOptions.None) }; - var exists = uow.Database.FirstOrDefault("WHERE nodeId = @Id", new { Id = content.Id }) != null; - int result = exists - ? uow.Database.Update(poco) - : Convert.ToInt32(uow.Database.Insert(poco)); - - //Save xml to db and call following method to fire event through PublishingStrategy to update cache - if (omitCacheRefresh == false) - _publishingStrategy.PublishingFinalized(content); - } - - if (Saved != null) - Saved(content, e); - - Audit.Add(AuditTypes.Publish, "Save and Publish performed by user", userId == -1 ? 0 : userId, content.Id); - - return published; + LogHelper.Info( + string.Format( + "Content '{0}' with Id '{1}' could not be published because its parent is not published.", + content.Name, content.Id)); + return false; } + //Content contains invalid property values and can therefore not be published - fire event? + if (!content.IsValid()) + { + LogHelper.Info( + string.Format( + "Content '{0}' with Id '{1}' could not be published because of invalid properties.", + content.Name, content.Id)); + return false; + } - return false; + //Publish and then update the database with new status + bool published = _publishingStrategy.Publish(content, userId); + + //Since this is the Save and Publish method, the content should be saved even though the publish fails or isn't allowed + SetWriter(content, userId); + repository.AddOrUpdate(content); + uow.Commit(); + + if (published) + { + var xml = content.ToXml(); + var poco = new ContentXmlDto { NodeId = content.Id, Xml = xml.ToString(SaveOptions.None) }; + var exists = uow.Database.FirstOrDefault("WHERE nodeId = @Id", new { Id = content.Id }) != null; + int result = exists + ? uow.Database.Update(poco) + : Convert.ToInt32(uow.Database.Insert(poco)); + + //Save xml to db and call following method to fire event through PublishingStrategy to update cache + if (omitCacheRefresh == false) + _publishingStrategy.PublishingFinalized(content); + } + + Saved.RaiseEvent(new SaveEventArgs(content, false), this); + + Audit.Add(AuditTypes.Publish, "Save and Publish performed by user", userId == -1 ? 0 : userId, content.Id); + + return published; } } @@ -674,26 +667,21 @@ namespace Umbraco.Core.Services var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateContentRepository(uow)) { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(content, e); + if (Saving.IsRaisedEventCancelled(new SaveEventArgs(content), this)) + return; + + SetWriter(content, userId); - if (!e.Cancel) - { - SetWriter(content, userId); + //Only change the publish state if the "previous" version was actually published + if (content.Published) + content.ChangePublishedState(false); - //Only change the publish state if the "previous" version was actually published - if (content.Published) - content.ChangePublishedState(false); + repository.AddOrUpdate(content); + uow.Commit(); - repository.AddOrUpdate(content); - uow.Commit(); - - if (Saved != null) - Saved(content, e); - - Audit.Add(AuditTypes.Save, "Save Content performed by user", userId == -1 ? 0 : userId, content.Id); - } + Saved.RaiseEvent(new SaveEventArgs(content, false), this); + + Audit.Add(AuditTypes.Save, "Save Content performed by user", userId == -1 ? 0 : userId, content.Id); } } @@ -713,44 +701,46 @@ namespace Umbraco.Core.Services { var containsNew = contents.Any(x => x.HasIdentity == false); - var e = new SaveEventArgs(); - if (Saving != null) - Saving(contents, e); - - if (!e.Cancel) + if (CollectionSaving.IsRaisedEventCancelled(new SaveEventArgs(contents), this)) + return; + + if (containsNew) { - if (containsNew) + foreach (var content in contents) { - foreach (var content in contents) - { - SetWriter(content, userId); - - //Only change the publish state if the "previous" version was actually published - if (content.Published) - content.ChangePublishedState(false); + if (Saving.IsRaisedEventCancelled(new SaveEventArgs(content), this)) + continue; + + SetWriter(content, userId); - repository.AddOrUpdate(content); - uow.Commit(); - } - } - else - { - foreach (var content in contents) - { - if (Saving != null) - Saving(content, e); + //Only change the publish state if the "previous" version was actually published + if (content.Published) + content.ChangePublishedState(false); - SetWriter(content, userId); - repository.AddOrUpdate(content); - } + repository.AddOrUpdate(content); uow.Commit(); + + Saved.RaiseEvent(new SaveEventArgs(content, false), this); } - - if (Saved != null) - Saved(contents, e); - - Audit.Add(AuditTypes.Save, "Bulk Save content performed by user", userId == -1 ? 0 : userId, -1); } + else + { + foreach (var content in contents) + { + if (Saving.IsRaisedEventCancelled(new SaveEventArgs(content), this)) + continue; + + SetWriter(content, userId); + repository.AddOrUpdate(content); + uow.Commit(); + + Saved.RaiseEvent(new SaveEventArgs(content, false), this); + } + } + + CollectionSaved.RaiseEvent(new SaveEventArgs(contents, false), this); + + Audit.Add(AuditTypes.Save, "Bulk Save content performed by user", userId == -1 ? 0 : userId, -1); } } @@ -767,15 +757,14 @@ namespace Umbraco.Core.Services { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateContentRepository(uow)) - { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(contents, e); - - if (!e.Cancel) + { + if (!CollectionSaving.IsRaisedEventCancelled(new SaveEventArgs(contents), this)) { foreach (var content in contents) { + if (Saving.IsRaisedEventCancelled(new SaveEventArgs(content.Value), this)) + continue; + SetWriter(content.Value, userId); //Only change the publish state if the "previous" version was actually published @@ -784,11 +773,12 @@ namespace Umbraco.Core.Services repository.AddOrUpdate(content.Value); uow.Commit(); + + Saved.RaiseEvent(new SaveEventArgs(content.Value, false), this); } - if (Saved != null) - Saved(contents, e); - + CollectionSaved.RaiseEvent(new SaveEventArgs(contents, false), this); + Audit.Add(AuditTypes.Save, "Bulk Save (lazy) content performed by user", userId == -1 ? 0 : userId, -1); } } @@ -1348,12 +1338,22 @@ namespace Umbraco.Core.Services /// /// Occurs before Save /// - public static event EventHandler Saving; - + public static event TypedEventHandler> Saving; + /// /// Occurs after Save /// - public static event EventHandler Saved; + public static event TypedEventHandler> Saved; + + /// + /// Occurs before saving a collection + /// + public static event TypedEventHandler> CollectionSaving; + + /// + /// Occurs after saving a collection + /// + public static event TypedEventHandler> CollectionSaved; /// /// Occurs before Create diff --git a/src/Umbraco.Core/Services/ContentTypeService.cs b/src/Umbraco.Core/Services/ContentTypeService.cs index 4d2627a31b..b13f64052f 100644 --- a/src/Umbraco.Core/Services/ContentTypeService.cs +++ b/src/Umbraco.Core/Services/ContentTypeService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; @@ -114,26 +115,21 @@ namespace Umbraco.Core.Services /// Optional id of the user saving the ContentType public void Save(IContentType contentType, int userId = -1) { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(contentType, e); + if (SavingContentType.IsRaisedEventCancelled(new SaveEventArgs(contentType), this)) + return; + + var uow = _uowProvider.GetUnitOfWork(); + using (var repository = _repositoryFactory.CreateContentTypeRepository(uow)) + { + SetUser(contentType, userId); + repository.AddOrUpdate(contentType); - if (!e.Cancel) - { - var uow = _uowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreateContentTypeRepository(uow)) - { - SetUser(contentType, userId); - repository.AddOrUpdate(contentType); + uow.Commit(); - uow.Commit(); + SavedContentType.RaiseEvent(new SaveEventArgs(contentType, false), this); + } - if (Saved != null) - Saved(contentType, e); - } - - Audit.Add(AuditTypes.Save, string.Format("Save ContentType performed by user"), userId == -1 ? 0 : userId, contentType.Id); - } + Audit.Add(AuditTypes.Save, string.Format("Save ContentType performed by user"), userId == -1 ? 0 : userId, contentType.Id); } /// @@ -143,29 +139,28 @@ namespace Umbraco.Core.Services /// Optional id of the user saving the ContentType public void Save(IEnumerable contentTypes, int userId = -1) { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(contentTypes, e); + if (CollectionSaving.IsRaisedEventCancelled(new SaveEventArgs(contentTypes), this)) + return; + + var uow = _uowProvider.GetUnitOfWork(); + using (var repository = _repositoryFactory.CreateContentTypeRepository(uow)) + { + foreach (var contentType in contentTypes) + { + if (SavingContentType.IsRaisedEventCancelled(new SaveEventArgs(contentType), this)) + continue; - if (!e.Cancel) - { - var uow = _uowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreateContentTypeRepository(uow)) - { - foreach (var contentType in contentTypes) - { - SetUser(contentType, userId); - repository.AddOrUpdate(contentType); - } + SetUser(contentType, userId); + repository.AddOrUpdate(contentType); + uow.Commit(); - uow.Commit(); + SavedContentType.RaiseEvent(new SaveEventArgs(contentType, false), this); + } - if (Saved != null) - Saved(contentTypes, e); - } + CollectionSaved.RaiseEvent(new SaveEventArgs(contentTypes, false), this); + } - Audit.Add(AuditTypes.Save, string.Format("Save ContentTypes performed by user"), userId == -1 ? 0 : userId, -1); - } + Audit.Add(AuditTypes.Save, string.Format("Save ContentTypes performed by user"), userId == -1 ? 0 : userId, -1); } /// @@ -179,28 +174,28 @@ namespace Umbraco.Core.Services /// Optional Id of the User saving the ContentTypes public void Save(IEnumerable> contentTypes, int userId = -1) { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(contentTypes, e); + if (CollectionSaving.IsRaisedEventCancelled(new SaveEventArgs(contentTypes), this)) + return; + + var uow = _uowProvider.GetUnitOfWork(); + using (var repository = _repositoryFactory.CreateContentTypeRepository(uow)) + { + foreach (var content in contentTypes) + { + if (SavingContentType.IsRaisedEventCancelled(new SaveEventArgs(content.Value), this)) + continue; - if (!e.Cancel) - { - var uow = _uowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreateContentTypeRepository(uow)) - { - foreach (var content in contentTypes) - { - content.Value.CreatorId = 0; - repository.AddOrUpdate(content.Value); - uow.Commit(); - } + content.Value.CreatorId = 0; + repository.AddOrUpdate(content.Value); + uow.Commit(); - if (Saved != null) - Saved(contentTypes, e); - } + SavedContentType.RaiseEvent(new SaveEventArgs(content.Value, false), this); + } - Audit.Add(AuditTypes.Save, string.Format("Save (lazy) ContentTypes performed by user"), userId == -1 ? 0 : userId, -1); - } + CollectionSaved.RaiseEvent(new SaveEventArgs(contentTypes, false), this); + } + + Audit.Add(AuditTypes.Save, string.Format("Save (lazy) ContentTypes performed by user"), userId == -1 ? 0 : userId, -1); } /// @@ -337,26 +332,20 @@ namespace Umbraco.Core.Services /// Optional Id of the user saving the MediaType public void Save(IMediaType mediaType, int userId = -1) { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(mediaType, e); + if (SavingMediaType.IsRaisedEventCancelled(new SaveEventArgs(mediaType), this)) + return; + + var uow = _uowProvider.GetUnitOfWork(); + using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow)) + { + SetUser(mediaType, userId); + repository.AddOrUpdate(mediaType); + uow.Commit(); - if (!e.Cancel) - { - var uow = _uowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow)) - { - SetUser(mediaType, userId); - repository.AddOrUpdate(mediaType); - uow.Commit(); + SavedMediaType.RaiseEvent(new SaveEventArgs(mediaType, false), this); + } - - if (Saved != null) - Saved(mediaType, e); - } - - Audit.Add(AuditTypes.Save, string.Format("Save MediaType performed by user"), userId == -1 ? 0 : userId, mediaType.Id); - } + Audit.Add(AuditTypes.Save, string.Format("Save MediaType performed by user"), userId == -1 ? 0 : userId, mediaType.Id); } /// @@ -366,29 +355,29 @@ namespace Umbraco.Core.Services /// Optional Id of the user savging the MediaTypes public void Save(IEnumerable mediaTypes, int userId = -1) { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(mediaTypes, e); + if (CollectionSaving.IsRaisedEventCancelled(new SaveEventArgs(mediaTypes), this)) + return; - if (!e.Cancel) - { - var uow = _uowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow)) - { + var uow = _uowProvider.GetUnitOfWork(); + using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow)) + { - foreach (var mediaType in mediaTypes) - { - SetUser(mediaType, userId); - repository.AddOrUpdate(mediaType); - } - uow.Commit(); + foreach (var mediaType in mediaTypes) + { + if (SavingMediaType.IsRaisedEventCancelled(new SaveEventArgs(mediaType), this)) + return; + + SetUser(mediaType, userId); + repository.AddOrUpdate(mediaType); + uow.Commit(); - if (Saved != null) - Saved(mediaTypes, e); - } + SavedMediaType.RaiseEvent(new SaveEventArgs(mediaType, false), this); + } - Audit.Add(AuditTypes.Save, string.Format("Save MediaTypes performed by user"), userId == -1 ? 0 : userId, -1); - } + CollectionSaved.RaiseEvent(new SaveEventArgs(mediaTypes, false), this); + } + + Audit.Add(AuditTypes.Save, string.Format("Save MediaTypes performed by user"), userId == -1 ? 0 : userId, -1); } /// @@ -569,12 +558,34 @@ namespace Umbraco.Core.Services /// /// Occurs before Save /// - public static event EventHandler Saving; + public static event TypedEventHandler> SavingContentType; /// /// Occurs after Save /// - public static event EventHandler Saved; + public static event TypedEventHandler> SavedContentType; + + /// + /// Occurs before Save + /// + public static event TypedEventHandler> SavingMediaType; + + /// + /// Occurs after Save + /// + public static event TypedEventHandler> SavedMediaType; + + + /// + /// Occurs before saving a collection + /// + public static event TypedEventHandler> CollectionSaving; + + /// + /// Occurs after saving a collection + /// + public static event TypedEventHandler> CollectionSaved; + #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/DataTypeService.cs b/src/Umbraco.Core/Services/DataTypeService.cs index 76bef9efcb..c5b235c2af 100644 --- a/src/Umbraco.Core/Services/DataTypeService.cs +++ b/src/Umbraco.Core/Services/DataTypeService.cs @@ -104,25 +104,20 @@ namespace Umbraco.Core.Services /// Id of the user issueing the save public void Save(IDataTypeDefinition dataTypeDefinition, int userId = -1) { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(dataTypeDefinition, e); + if (Saving.IsRaisedEventCancelled(new SaveEventArgs(dataTypeDefinition), this)) + return; + + var uow = _uowProvider.GetUnitOfWork(); + using (var repository = _repositoryFactory.CreateDataTypeDefinitionRepository(uow)) + { + dataTypeDefinition.CreatorId = userId > -1 ? userId : 0; + repository.AddOrUpdate(dataTypeDefinition); + uow.Commit(); - if (!e.Cancel) - { - var uow = _uowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreateDataTypeDefinitionRepository(uow)) - { - dataTypeDefinition.CreatorId = userId > -1 ? userId : 0; - repository.AddOrUpdate(dataTypeDefinition); - uow.Commit(); + Saved.RaiseEvent(new SaveEventArgs(dataTypeDefinition, false), this); + } - if (Saved != null) - Saved(dataTypeDefinition, e); - } - - Audit.Add(AuditTypes.Save, string.Format("Save DataTypeDefinition performed by user"), userId == -1 ? 0 : userId, dataTypeDefinition.Id); - } + Audit.Add(AuditTypes.Save, string.Format("Save DataTypeDefinition performed by user"), userId == -1 ? 0 : userId, dataTypeDefinition.Id); } /// @@ -212,12 +207,12 @@ namespace Umbraco.Core.Services /// /// Occurs before Save /// - public static event EventHandler Saving; + public static event TypedEventHandler> Saving; /// /// Occurs after Save /// - public static event EventHandler Saved; + public static event TypedEventHandler> Saved; #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs index 468eeca49c..8b95a755cf 100644 --- a/src/Umbraco.Core/Services/FileService.cs +++ b/src/Umbraco.Core/Services/FileService.cs @@ -65,24 +65,19 @@ namespace Umbraco.Core.Services /// public void SaveStylesheet(Stylesheet stylesheet, int userId = -1) { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(stylesheet, e); + if (SavingStylesheet.IsRaisedEventCancelled(new SaveEventArgs(stylesheet), this)) + return; + + var uow = _fileUowProvider.GetUnitOfWork(); + using (var repository = _repositoryFactory.CreateStylesheetRepository(uow)) + { + repository.AddOrUpdate(stylesheet); + uow.Commit(); - if (!e.Cancel) - { - var uow = _fileUowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreateStylesheetRepository(uow)) - { - repository.AddOrUpdate(stylesheet); - uow.Commit(); + SavedStylesheet.RaiseEvent(new SaveEventArgs(stylesheet, false), this); + } - if (Saved != null) - Saved(stylesheet, e); - } - - Audit.Add(AuditTypes.Save, string.Format("Save Stylesheet performed by user"), userId == -1 ? 0 : userId, -1); - } + Audit.Add(AuditTypes.Save, string.Format("Save Stylesheet performed by user"), userId == -1 ? 0 : userId, -1); } /// @@ -156,24 +151,19 @@ namespace Umbraco.Core.Services /// public void SaveScript(Script script, int userId = -1) { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(script, e); + if (SavingScript.IsRaisedEventCancelled(new SaveEventArgs