/// <summary>
 /// Accesses the <see cref="DocumentTypeAttribute"/> applied to a class to find
 /// the document type alias for that class
 /// </summary>
 /// <param name="input">The document type instance to get the alias for</param>
 /// <returns>the document type alias</returns>
 /// <exception cref="CodeFirstException">Thrown if the specified type does not have a <see cref="DocumentTypeAttribute"/> attribute.</exception>
 public static string GetDocumentTypeAlias(this DocumentTypeBase input)
 {
     try
     {
         return(input.GetType().GetCodeFirstAttribute <DocumentTypeAttribute>().Alias);
     }
     catch (Exception e)
     {
         throw new CodeFirstException(input.GetType().Name, e);
     }
 }
示例#2
0
 /// <summary>
 /// Automatically constructs a factory for any inheritor of DocumentTypeBase
 /// </summary>
 /// <param name="values">The document type to create</param>
 /// <returns>a factory for any inheritor of DocumentTypeBase</returns>
 protected virtual IContentFactory ConstructFactory(DocumentTypeBase values)
 {
     values.NodeDetails.Name = _name;
     if (_parentType == null) //Root node
     {
         return(new RootContentFactory(values));
     }
     else //Child node
     {
         return(Activator.CreateInstance(typeof(ChildContentFactory <>).MakeGenericType(_parentType), values) as IContentFactory);
     }
 }
        /// <summary>
        /// Creates AutoContent which specifies a parent document
        /// </summary>
        /// <param name="values">The document property values to use</param>
        /// <param name="parentDocType">The type of the parent document</param>
        public ChildContentFactory(DocumentTypeBase values, Type parentDocType)
            : base(values)
        {
            if(parentDocType == null)
            {
                throw new ArgumentNullException("parentDocType", "ChildContentFactory requires a parent document type");
            }

            //Get the factory type of the parent type
            var attr = parentDocType.GetInitialisedAttribute<ContentFactoryAttribute>();
            if (attr == null)
            {
                throw new InvalidOperationException("Parent type '" + parentDocType.Name + "' specified for Umbraco content '" + values.NodeDetails.Name + "' does not have a [ContentFactory] or [AutoContent] attribute");
            }
            else
            {
                _parentFactory = attr.Factory;
            }
        }
        /// <summary>
        /// Creates AutoContent which specifies a parent document
        /// </summary>
        /// <param name="values">The document property values to use</param>
        /// <param name="parentDocType">The type of the parent document</param>
        public ChildContentFactory(DocumentTypeBase values, Type parentDocType)
            : base(values)
        {
            if (parentDocType == null)
            {
                throw new ArgumentNullException("parentDocType", "ChildContentFactory requires a parent document type");
            }

            //Get the factory type of the parent type
            var attr = parentDocType.GetInitialisedAttribute <ContentFactoryAttribute>();

            if (attr == null)
            {
                throw new InvalidOperationException("Parent type '" + parentDocType.Name + "' specified for Umbraco content '" + values.NodeDetails.Name + "' does not have a [ContentFactory] or [AutoContent] attribute");
            }
            else
            {
                _parentFactory = attr.Factory;
            }
        }
        public IContent ConvertToContent(DocumentTypeBase model, int parentId = -1)
        {
            var contentId = model.NodeDetails.UmbracoId;
            DocumentTypeRegistration registration;

            if (!_documentTypeModule.TryGetDocumentType(model.GetType(), out registration))
            {
                throw new CodeFirstException("Document type not registered. Type: " + model.GetType());
            }

            //Create or update object
            if (contentId == -1)
            {
                return(CreateContent(parentId, model, registration));
            }
            else
            {
                return(UpdateContent(model, registration));
            }
        }
 /// <summary>
 /// Creates AutoContent which specifies a parent document
 /// </summary>
 /// <param name="values">The document property values to use</param>
 public ChildContentFactory(DocumentTypeBase values)
     : base(values, typeof(TparentDocType))
 {
 }
 /// <summary>
 /// <para>Creates an instance of a document type using values supplied as a strongly-typed model.</para>
 /// <para>The RootContentFactory and ChildContentFactory[T] implementations work with the IAutoContent interface
 /// and the [AutoContent] attribute to place the content creation logic into methods on the code-first document type model.
 /// The recommended approach for content creation is to implement IAutoContent on the document type model and apply
 /// an [AutoContent] attribute to the document type model.</para>
 /// <para>Implementing a custom factory may be required for some scenarios such as creating a collection
 /// of content items or creating a nested structure in a single factory. A custom factory type can be
 /// specified using a [ContentFactory] attribute.</para>
 /// </summary>
 /// <param name="values">The values to use to create the document</param>
 protected ContentFactoryBase(DocumentTypeBase values)
 {
     Values = values;
 }
 /// <summary>
 /// <para>Creates an instance of a document type using values supplied as a strongly-typed model.</para>
 /// <para>The RootContentFactory and ChildContentFactory[T] implementations work with the IAutoContent interface
 /// and the [AutoContent] attribute to place the content creation logic into methods on the code-first document type model.
 /// The recommended approach for content creation is to implement IAutoContent on the document type model and apply
 /// an [AutoContent] attribute to the document type model.</para>
 /// <para>Implementing a custom factory may be required for some scenarios such as creating a collection
 /// of content items or creating a nested structure in a single factory. A custom factory type can be
 /// specified using a [ContentFactory] attribute.</para>
 /// </summary>
 /// <param name="values">The values to use to create the document</param>
 protected ContentFactoryBase(DocumentTypeBase values)
 {
     Values = values;
 }
 /// <summary>
 /// Creates AutoContent which does not specify a parent document
 /// </summary>
 /// <param name="values">The document property values to use</param>
 public RootContentFactory(DocumentTypeBase values)
     : base(values)
 {
 }
 /// <summary>
 /// Creates AutoContent which does not specify a parent document
 /// </summary>
 /// <param name="values">The document property values to use</param>
 public RootContentFactory(DocumentTypeBase values)
     : base(values)
 {
 }
 /// <summary>
 /// Automatically constructs a factory for any inheritor of DocumentTypeBase
 /// </summary>
 /// <param name="values">The document type to create</param>
 /// <returns>a factory for any inheritor of DocumentTypeBase</returns>
 protected virtual IContentFactory ConstructFactory(DocumentTypeBase values)
 {
     values.NodeDetails.Name = _name;
     if (_parentType == null) //Root node
     {
         return new RootContentFactory(values);
     }
     else //Child node
     {
         return Activator.CreateInstance(typeof(ChildContentFactory<>).MakeGenericType(_parentType), values) as IContentFactory;
     }
 }