Implementing a POC and tests for a simple TypedModel base class
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Umbraco.Tests.PublishedContent.StronglyTypedModels
|
||||
{
|
||||
[TestFixture]
|
||||
public class CallingMethodTests
|
||||
{
|
||||
private readonly Func<MethodBase> _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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()); } }
|
||||
}
|
||||
}
|
||||
@@ -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<MethodBase> ForThis = MethodBase.GetCurrentMethod;
|
||||
|
||||
public object Resolve(Type type, MethodBase methodBase)
|
||||
{
|
||||
var propertyTypeAlias = methodBase.ToUmbracoAlias();
|
||||
return _publishedContent.GetPropertyValue(propertyTypeAlias);
|
||||
}
|
||||
|
||||
public T Resolve<T>(MethodBase methodBase)
|
||||
{
|
||||
var propertyTypeAlias = methodBase.ToUmbracoAlias();
|
||||
return _publishedContent.GetPropertyValue<T>(propertyTypeAlias);
|
||||
}
|
||||
|
||||
public string ResolveString(MethodBase methodBase)
|
||||
{
|
||||
return Resolve<string>(methodBase);
|
||||
}
|
||||
|
||||
public int ResolveInt(MethodBase methodBase)
|
||||
{
|
||||
return Resolve<int>(methodBase);
|
||||
}
|
||||
|
||||
public bool ResolveBool(MethodBase methodBase)
|
||||
{
|
||||
return Resolve<bool>(methodBase);
|
||||
}
|
||||
|
||||
public DateTime ResolveDate(MethodBase methodBase)
|
||||
{
|
||||
return Resolve<DateTime>(methodBase);
|
||||
}
|
||||
}
|
||||
|
||||
public static class TypeExtensions
|
||||
{
|
||||
public static string ToUmbracoAlias(this MethodBase methodBase)
|
||||
{
|
||||
return methodBase.Name.Replace("get_", "").ToUmbracoAlias();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Umbraco.Web.Mvc;
|
||||
|
||||
namespace Umbraco.Tests.PublishedContent.StronglyTypedModels
|
||||
{
|
||||
public abstract class UmbracoTemplatePage<T> : 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the a strongly typed model
|
||||
/// </summary>
|
||||
public T TypedModel { get; private set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user