/// <summary>
        /// Welds a new part to the content item. If a part of the same type is already welded nothing is done.
        /// </summary>
        /// <typeparam name="TPart">The type of the part to be welded.</typeparam>
        /// <returns>A new Content Item Builder with the item having the new part welded.</returns>
        public ContentItemBuilder Weld <TPart>() where TPart : ContentPart, new()
        {
            // if the part hasn't be weld yet
            if (_item.Parts.FirstOrDefault(part => part.GetType().Equals(typeof(TPart))) == null)
            {
                var partName = typeof(TPart).Name;

                // obtain the type definition for the part
                var typePartDefinition = _definition.Parts.FirstOrDefault(p => p.PartDefinition.Name == partName);
                if (typePartDefinition == null)
                {
                    // If the content item's type definition does not define the part; use an empty type definition.
                    typePartDefinition = new ContentTypePartDefinition(
                        new ContentPartDefinition(partName),
                        new SettingsDictionary());
                }

                // build and weld the part
                var part = new TPart {
                    TypePartDefinition = typePartDefinition
                };
                _item.Weld(part);
            }

            return(this);
        }
Example #2
0
        /// <summary>
        /// Welds a new part to the content item. If a part of the same type is already welded nothing is done.
        /// </summary>
        /// <typeparam name="TPart">The type of the part to be welded.</typeparam>
        /// <returns>A new Content Item Builder with the item having the new part welded.</returns>
        public ContentItemBuilder Weld <TPart>() where TPart : ContentPart, new()
        {
            // if the part hasn't be weld yet
            if (_item.As <TPart>() == null)
            {
                var partName = typeof(TPart).Name;

                // build and welded the part
                var part = new TPart();
                _item.Weld(partName, part);
            }

            return(this);
        }