diff --git a/src/Umbraco.Core/Events/NewEventArgs.cs b/src/Umbraco.Core/Events/NewEventArgs.cs index d74cb24b5c..135caa78f6 100644 --- a/src/Umbraco.Core/Events/NewEventArgs.cs +++ b/src/Umbraco.Core/Events/NewEventArgs.cs @@ -1,15 +1,27 @@ namespace Umbraco.Core.Events { - public class NewEventArgs : System.ComponentModel.CancelEventArgs + public class NewEventArgs : CancellableObjectEventArgs { + public NewEventArgs(TEntity entity, bool canCancel, string @alias, int parentId) : base(entity, canCancel) + { + Alias = alias; + ParentId = parentId; + } + + public NewEventArgs(TEntity entity, string @alias, int parentId) : base(entity) + { + Alias = alias; + ParentId = parentId; + } + /// /// Gets or Sets the Alias. /// - public string Alias { get; set; } + public string Alias { get; private set; } /// /// Gets or Sets the Id of the parent. /// - public int ParentId { get; set; } + public int ParentId { get; private set; } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Models/ContentBase.cs b/src/Umbraco.Core/Models/ContentBase.cs index 9729fa8edd..fd4692cb33 100644 --- a/src/Umbraco.Core/Models/ContentBase.cs +++ b/src/Umbraco.Core/Models/ContentBase.cs @@ -27,7 +27,7 @@ namespace Umbraco.Core.Models protected ContentBase(int parentId, IContentTypeComposition contentType, PropertyCollection properties) { - //Mandate.ParameterCondition(parentId != 0, "parentId"); + Mandate.ParameterCondition(parentId != 0, "parentId"); Mandate.ParameterNotNull(contentType, "contentType"); Mandate.ParameterNotNull(properties, "properties"); diff --git a/src/Umbraco.Core/Models/ContentTypeBase.cs b/src/Umbraco.Core/Models/ContentTypeBase.cs index 22a1c65ad9..d44b132832 100644 --- a/src/Umbraco.Core/Models/ContentTypeBase.cs +++ b/src/Umbraco.Core/Models/ContentTypeBase.cs @@ -33,6 +33,8 @@ namespace Umbraco.Core.Models protected ContentTypeBase(int parentId) { + Mandate.ParameterCondition(parentId != 0, "parentId"); + _parentId = new Lazy(() => parentId); _allowedContentTypes = new List(); _propertyGroups = new PropertyGroupCollection(); @@ -40,6 +42,8 @@ namespace Umbraco.Core.Models protected ContentTypeBase(IContentTypeBase parent) { + Mandate.ParameterNotNull(parent, "parent"); + _parentId = new Lazy(() => parent.Id); _allowedContentTypes = new List(); _propertyGroups = new PropertyGroupCollection(); diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index 7143c6ae8b..9a3e3f6b92 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -84,23 +84,17 @@ namespace Umbraco.Core.Services if (contentType == null) throw new Exception(string.Format("ContentType matching the passed in Alias: '{0}' was null", contentTypeAlias)); - IContent content = null; + var content = new Content(parentId, contentType); - var e = new NewEventArgs { Alias = contentTypeAlias, ParentId = parentId }; - if (Creating != null) - Creating(content, e); + if (Creating.IsRaisedEventCancelled(new NewEventArgs(content, contentTypeAlias, parentId), this)) + return content; + + SetUser(content, userId); + SetWriter(content, userId); - if (!e.Cancel) - { - content = new Content(parentId, contentType); - SetUser(content, userId); - SetWriter(content, userId); + Created.RaiseEvent(new NewEventArgs(content, false, contentTypeAlias, parentId), this); - if (Created != null) - Created(content, e); - - Audit.Add(AuditTypes.New, "", content.CreatorId, content.Id); - } + Audit.Add(AuditTypes.New, "", content.CreatorId, content.Id); return content; } @@ -1310,12 +1304,12 @@ namespace Umbraco.Core.Services /// /// Occurs before Create /// - public static event EventHandler Creating; + public static event TypedEventHandler> Creating; /// /// Occurs after Create /// - public static event EventHandler Created; + public static event TypedEventHandler> Created; /// /// Occurs before Copy diff --git a/src/Umbraco.Core/Services/MediaService.cs b/src/Umbraco.Core/Services/MediaService.cs index 3f558de1fc..9da0192bf8 100644 --- a/src/Umbraco.Core/Services/MediaService.cs +++ b/src/Umbraco.Core/Services/MediaService.cs @@ -66,21 +66,14 @@ namespace Umbraco.Core.Services var media = new Models.Media(parentId, mediaType); - var e = new NewEventArgs { Alias = mediaTypeAlias, ParentId = parentId }; + if (Creating.IsRaisedEventCancelled(new NewEventArgs(media, mediaTypeAlias, parentId), this)) + return media; - if (Creating != null) - Creating(media, e); + SetUser(media, userId); - if (!e.Cancel) - { - - SetUser(media, userId); + Created.RaiseEvent(new NewEventArgs(media, false, mediaTypeAlias, parentId), this); - if (Created != null) - Created(media, e); - - Audit.Add(AuditTypes.New, "", media.CreatorId, media.Id); - } + Audit.Add(AuditTypes.New, "", media.CreatorId, media.Id); return media; } @@ -457,12 +450,12 @@ namespace Umbraco.Core.Services /// /// Occurs before Create /// - public static event EventHandler Creating; + public static event TypedEventHandler> Creating; /// /// Occurs after Create /// - public static event EventHandler Created; + public static event TypedEventHandler> Created; /// /// Occurs before Content is moved to Recycle Bin