re-added parameter check to ensure parent id is not 0. If adding hierarichal data, then use the

other overload accepting an entity. Converts over the Created/Creating events to the new structure for
ContentService and MediaService
This commit is contained in:
Shannon Deminick
2012-12-16 06:28:43 +05:00
parent 5e941a1c3a
commit dad5326281
5 changed files with 37 additions and 34 deletions

View File

@@ -1,15 +1,27 @@
namespace Umbraco.Core.Events
{
public class NewEventArgs : System.ComponentModel.CancelEventArgs
public class NewEventArgs<TEntity> : CancellableObjectEventArgs<TEntity>
{
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;
}
/// <summary>
/// Gets or Sets the Alias.
/// </summary>
public string Alias { get; set; }
public string Alias { get; private set; }
/// <summary>
/// Gets or Sets the Id of the parent.
/// </summary>
public int ParentId { get; set; }
public int ParentId { get; private set; }
}
}

View File

@@ -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");

View File

@@ -33,6 +33,8 @@ namespace Umbraco.Core.Models
protected ContentTypeBase(int parentId)
{
Mandate.ParameterCondition(parentId != 0, "parentId");
_parentId = new Lazy<int>(() => parentId);
_allowedContentTypes = new List<ContentTypeSort>();
_propertyGroups = new PropertyGroupCollection();
@@ -40,6 +42,8 @@ namespace Umbraco.Core.Models
protected ContentTypeBase(IContentTypeBase parent)
{
Mandate.ParameterNotNull(parent, "parent");
_parentId = new Lazy<int>(() => parent.Id);
_allowedContentTypes = new List<ContentTypeSort>();
_propertyGroups = new PropertyGroupCollection();

View File

@@ -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<IContent>(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<IContent>(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
/// <summary>
/// Occurs before Create
/// </summary>
public static event EventHandler<NewEventArgs> Creating;
public static event TypedEventHandler<IContentService, NewEventArgs<IContent>> Creating;
/// <summary>
/// Occurs after Create
/// </summary>
public static event EventHandler<NewEventArgs> Created;
public static event TypedEventHandler<IContentService, NewEventArgs<IContent>> Created;
/// <summary>
/// Occurs before Copy

View File

@@ -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<IMedia>(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<IMedia>(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
/// <summary>
/// Occurs before Create
/// </summary>
public static event EventHandler<NewEventArgs> Creating;
public static event TypedEventHandler<IMediaService, NewEventArgs<IMedia>> Creating;
/// <summary>
/// Occurs after Create
/// </summary>
public static event EventHandler<NewEventArgs> Created;
public static event TypedEventHandler<IMediaService, NewEventArgs<IMedia>> Created;
/// <summary>
/// Occurs before Content is moved to Recycle Bin