Added a new feature for plugin developers to create a model class that DynamicNode will return for their data type instead of the underlying type

This commit is contained in:
agrath@gmail.com
2011-06-19 08:19:11 -02:00
parent 315003c16f
commit 00141ba801
4 changed files with 66 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ using System.Xml;
using System.Xml.Linq;
using umbraco.cms.businesslogic.media;
using umbraco.MacroEngines.Library;
using umbraco.BusinessLogic.Utils;
@@ -363,6 +364,7 @@ namespace umbraco.MacroEngines
}
return list;
}
static Dictionary<Guid, Type> RazorDataTypeModelTypes = null;
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
@@ -393,6 +395,35 @@ namespace umbraco.MacroEngines
Guid dataType = ContentType.GetDataType(n.NodeTypeAlias, data.Alias);
if (RazorDataTypeModelTypes == null)
{
RazorDataTypeModelTypes = new Dictionary<Guid, Type>();
TypeFinder.FindClassesMarkedWithAttribute(typeof(RazorDataTypeModel))
.ToList()
.FindAll(type => typeof(IRazorDataTypeModel).IsAssignableFrom(type))
.ConvertAll(type =>
{
RazorDataTypeModel RazorDataTypeModelAttribute = (RazorDataTypeModel)Attribute.GetCustomAttribute(type, typeof(RazorDataTypeModel));
Guid g = RazorDataTypeModelAttribute.DataTypeEditorId;
return new KeyValuePair<Guid, Type>(g, type);
})
.ForEach(item => RazorDataTypeModelTypes.Add(item.Key, item.Value));
}
if (RazorDataTypeModelTypes.ContainsKey(dataType))
{
Type dataTypeType = RazorDataTypeModelTypes[dataType];
IRazorDataTypeModel razorDataTypeModel = Activator.CreateInstance(dataTypeType, false) as IRazorDataTypeModel;
if (razorDataTypeModel != null)
{
if (razorDataTypeModel.Init(n.Id, result))
{
result = razorDataTypeModel;
return true;
}
}
}
//convert the string value to a known type
return ConvertPropertyValueByDataType(ref result, name, dataType);

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace umbraco.MacroEngines
{
public interface IRazorDataTypeModel
{
bool Init(int CurrentNodeId, object PropertyData);
}
}

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace umbraco.MacroEngines
{
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public sealed class RazorDataTypeModel : Attribute
{
public readonly Guid DataTypeEditorId;
public RazorDataTypeModel(string DataTypeEditorId)
{
this.DataTypeEditorId = new Guid(DataTypeEditorId);
}
}
}

View File

@@ -86,7 +86,9 @@
<Compile Include="RazorDynamicNode\HtmlTagWrapper.cs" />
<Compile Include="RazorDynamicNode\HtmlTagWrapperBase.cs" />
<Compile Include="RazorDynamicNode\HtmlTagWrapperTextNode.cs" />
<Compile Include="RazorDynamicNode\IRazorDataTypeModel.cs" />
<Compile Include="RazorDynamicNode\PropertyResult.cs" />
<Compile Include="RazorDynamicNode\RazorDataTypeModel.cs" />
<Compile Include="RazorDynamicNode\RazorLibraryCore.cs" />
<Compile Include="RazorDynamicNode\DynamicNodeWalker.cs" />
<Compile Include="RazorDynamicNode\DynamicNull.cs" />