Last active
September 22, 2015 07:59
-
-
Save jamiepollock/67c52e5533a5a0aa084f to your computer and use it in GitHub Desktop.
Ditto TypeConverter - Multiple IEnumerable<IPublishedContent> & Single IPublishedContent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Globalization; | |
using Umbraco.Core; | |
using Umbraco.Core.Models; | |
namespace My.Website.ComponentModel.TypeConverters | |
{ | |
public abstract class MultipleIPublishedContentTypeConverterBase<T> : TypeConverter | |
{ | |
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) | |
{ | |
return sourceType.Implements<IEnumerable<IPublishedContent>>(); | |
} | |
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) | |
{ | |
if (value is IEnumerable<IPublishedContent>) | |
{ | |
return ConvertFromMultipleIPublishedContent(value as IEnumerable<IPublishedContent>); | |
} | |
return base.ConvertFrom(context, culture, value); | |
} | |
protected abstract T ConvertFromMultipleIPublishedContent(IEnumerable<IPublishedContent> entities); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Our.Umbraco.Ditto; | |
using System.ComponentModel; | |
using My.Website.ComponentModel.TypeConverters; | |
namespace My.Website.ViewModels | |
{ | |
public class MyViewModel | |
{ | |
[UmbracoProperty("targetSearchPagePickedFromTheCmsAsIdLikeMyAuthorsToChooseWhereTheWebsiteHeaderSearchFormPostsToForTheirSearchResults")] | |
[TypeConverter(typeof(SinglePublishedContentUrlTypeConverter))] | |
public string FormUrl { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using Umbraco.Core.Models; | |
namespace My.Website.ComponentModel.TypeConverters | |
{ | |
public abstract class SingleIPublishedContentTypeConverterBase<T> : MultipleIPublishedContentTypeConverterBase<T> | |
{ | |
protected override T ConvertFromMultipleIPublishedContent(IEnumerable<IPublishedContent> entities) | |
{ | |
var firstItem = entities.FirstOrDefault(); | |
if (firstItem != null) | |
{ | |
return ConvertFromSingleIPublishedContent(firstItem); | |
} | |
return default(T); | |
} | |
public abstract T ConvertFromSingleIPublishedContent(IPublishedContent entity); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Umbraco.Core.Models; | |
namespace My.Website.ComponentModel.TypeConverters | |
{ | |
public class SinglePublishedContentUrlTypeConverter : SingleIPublishedContentTypeConverterBase<string> | |
{ | |
public override string ConvertFromSingleIPublishedContent(IPublishedContent entity) { | |
return entity.Url; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment