diff --git a/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/CallingMethodTests.cs b/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/CallingMethodTests.cs new file mode 100644 index 0000000000..eae6593e5d --- /dev/null +++ b/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/CallingMethodTests.cs @@ -0,0 +1,28 @@ +using System; +using System.Reflection; +using NUnit.Framework; + +namespace Umbraco.Tests.PublishedContent.StronglyTypedModels +{ + [TestFixture] + public class CallingMethodTests + { + private readonly Func _myProperty = MethodBase.GetCurrentMethod; + + public string AField + { + get { return Resolve(_myProperty()); } + } + + private string Resolve(MethodBase m) + { + return m.Name.Replace("get_", ""); + } + + [Test] + public void GetMyName() + { + Assert.AreEqual("AField", AField); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/TextpageModel.cs b/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/TextpageModel.cs new file mode 100644 index 0000000000..dee4505eef --- /dev/null +++ b/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/TextpageModel.cs @@ -0,0 +1,18 @@ +using System; +using Umbraco.Core.Models; + +namespace Umbraco.Tests.PublishedContent.StronglyTypedModels +{ + public class TextpageModel : TypedModelBase + { + public TextpageModel(IPublishedContent publishedContent) : base(publishedContent) + { + } + + public string Title { get { return ResolveString(ForThis()); } } + + public string BodyText { get { return ResolveString(ForThis()); } } + + public DateTime Date { get { return ResolveDate(ForThis()); } } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/TypedModelBase.cs b/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/TypedModelBase.cs new file mode 100644 index 0000000000..90fa21f9ec --- /dev/null +++ b/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/TypedModelBase.cs @@ -0,0 +1,67 @@ +using System; +using System.Reflection; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Web; + +namespace Umbraco.Tests.PublishedContent.StronglyTypedModels +{ + public abstract class TypedModelBase + { + private IPublishedContent _publishedContent; + + protected TypedModelBase(){} + + protected TypedModelBase(IPublishedContent publishedContent) + { + _publishedContent = publishedContent; + } + + internal void Add(IPublishedContent publishedContent) + { + _publishedContent = publishedContent; + } + + public readonly Func ForThis = MethodBase.GetCurrentMethod; + + public object Resolve(Type type, MethodBase methodBase) + { + var propertyTypeAlias = methodBase.ToUmbracoAlias(); + return _publishedContent.GetPropertyValue(propertyTypeAlias); + } + + public T Resolve(MethodBase methodBase) + { + var propertyTypeAlias = methodBase.ToUmbracoAlias(); + return _publishedContent.GetPropertyValue(propertyTypeAlias); + } + + public string ResolveString(MethodBase methodBase) + { + return Resolve(methodBase); + } + + public int ResolveInt(MethodBase methodBase) + { + return Resolve(methodBase); + } + + public bool ResolveBool(MethodBase methodBase) + { + return Resolve(methodBase); + } + + public DateTime ResolveDate(MethodBase methodBase) + { + return Resolve(methodBase); + } + } + + public static class TypeExtensions + { + public static string ToUmbracoAlias(this MethodBase methodBase) + { + return methodBase.Name.Replace("get_", "").ToUmbracoAlias(); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/UmbracoTemplatePage`T.cs b/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/UmbracoTemplatePage`T.cs new file mode 100644 index 0000000000..f76495531a --- /dev/null +++ b/src/Umbraco.Tests/PublishedContent/StronglyTypedModels/UmbracoTemplatePage`T.cs @@ -0,0 +1,25 @@ +using Umbraco.Web.Mvc; + +namespace Umbraco.Tests.PublishedContent.StronglyTypedModels +{ + public abstract class UmbracoTemplatePage : UmbracoTemplatePage where T : TypedModelBase, new() + { + protected override void InitializePage() + { + base.InitializePage(); + + //set the model to the current node if it is not set, this is generally not the case + if (Model != null) + { + //Map CurrentModel here + TypedModel = new T(); + TypedModel.Add(Model.Content); + } + } + + /// + /// Returns the a strongly typed model + /// + public T TypedModel { get; private set; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index ec2c32202f..05b766619d 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -193,6 +193,10 @@ + + + +