Skip to content

Commit e890b8b

Browse files
committed
U7 / Typed model support
No guarantees what-so-ever, but IT WORKS! :)
1 parent 1fb5418 commit e890b8b

File tree

11 files changed

+525
-10
lines changed

11 files changed

+525
-10
lines changed

Umbraco.CodeGen.Integration/ApplicationEvents.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ApplicationEvents : IApplicationEventHandler
1919
private IEnumerable<DataTypeDefinition> dataTypes;
2020
private CodeGeneratorConfiguration configuration;
2121
private USyncConfiguration uSyncConfiguration;
22-
private USyncDataTypeProvider dataTypesProvider;
22+
private IDataTypeProvider dataTypesProvider;
2323

2424
private readonly Dictionary<string, string> paths = new Dictionary<string, string>();
2525
private readonly ContentTypeSerializer serializer = new ContentTypeSerializer();
@@ -36,8 +36,13 @@ public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, App
3636
var configurationProvider = new CodeGeneratorConfigurationFileProvider(HttpContext.Current.Server.MapPath("~/config/CodeGen.config"));
3737

3838
uSyncConfiguration = uSyncConfigurationProvider.GetConfiguration();
39-
40-
dataTypesProvider = new USyncDataTypeProvider(uSyncConfiguration.USyncFolder);
39+
40+
dataTypesProvider = new USyncDataTypeProvider(uSyncConfiguration.USyncFolder);
41+
42+
if (!dataTypesProvider.GetDataTypes().Any())
43+
return;
44+
//new UmbracoDataTypeProvider();
45+
//new USyncDataTypeProvider(uSyncConfiguration.USyncFolder);
4146

4247
dataTypes = dataTypesProvider.GetDataTypes();
4348
configuration = configurationProvider.GetConfiguration();
@@ -154,7 +159,18 @@ public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, Appl
154159
}
155160
}
156161

157-
public class HttpContextPathResolver : IRelativePathResolver
162+
public class UmbracoDataTypeProvider : IDataTypeProvider
163+
{
164+
public IEnumerable<DataTypeDefinition> GetDataTypes()
165+
{
166+
var dataTypeDefinitions = ApplicationContext.Current.Services.DataTypeService
167+
.GetAllDataTypeDefinitions()
168+
.Select(dt => new DataTypeDefinition(dt.Name, dt.PropertyEditorAlias, dt.Name));
169+
return dataTypeDefinitions;
170+
}
171+
}
172+
173+
public class HttpContextPathResolver : IRelativePathResolver
158174
{
159175
public string Resolve(string relativePath)
160176
{

Umbraco.CodeGen.Integration/USyncDataTypeProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Linq;
44
using System.Xml.Linq;
5+
using System.Xml.XPath;
56
using Umbraco.CodeGen.Configuration;
67

78
namespace Umbraco.CodeGen.Integration
@@ -28,7 +29,7 @@ public IEnumerable<DataTypeDefinition> GetDataTypes()
2829
private static DataTypeDefinition CreateDefinition(string configPath)
2930
{
3031
var doc = XDocument.Load(configPath);
31-
var dataType = doc.Element("DataType");
32+
var dataType = doc.XPathSelectElement("DataTypes/DataType");
3233
if (dataType == null) return null;
3334
var name = AttributeValue(dataType, "Name");
3435
var dataTypeId = AttributeValue(dataType, "Id");

Umbraco.CodeGen.Integration/Umbraco.CodeGen.Integration.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>Umbraco.CodeGen.Integration</RootNamespace>
1111
<AssemblyName>Umbraco.CodeGen.Integration</AssemblyName>
12-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<TargetFrameworkProfile />
1515
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<configSections>
4+
<section name="usync" type="jumps.umbraco.usync.uSyncSettingsSection,jumps.umbraco.usync" allowLocation="true" allowDefinition="Everywhere" />
5+
</configSections>
6+
7+
<usync read="true" write="false" attach="true" folder="~/uSync/" archive="~/uSync.Archive" versions="true" preserve="false">
8+
9+
<PreservedPreValues>
10+
<add key="f8d60f68-ec59-4974-b43b-c46eb5677985" value="Colour Picker"/>
11+
<add key="b4471851-82b6-4c75-afa4-39fa9c6a75e9" value="checkbox list"/>
12+
<add key="a74ea9c9-8e18-4d2a-8cf6-73c6206c5da6" value="dropdown"/>
13+
<add key="928639ed-9c73-4028-920c-1e55dbb68783" value="dropdown multiple" />
14+
<add key="a52c7c1c-c330-476e-8605-d63d3b84b6a6" value="radiobox" />
15+
</PreservedPreValues>
16+
17+
<MatchedPreValues>
18+
<add key="7e062c13-7c41-4ad9-b389-41d88aeef87c" value="Multi Node Tree Picker" />
19+
</MatchedPreValues>
20+
21+
<!-- turn bits on/off - default is on (true) (for backwards compatability) -->
22+
<Elements
23+
dataTypes="true"
24+
docTypes="true"
25+
mediaTypes="true"
26+
macros="true"
27+
stylesheets="true"
28+
templates="true"
29+
dictionary ="true"
30+
/>
31+
32+
<!-- experimental (v1.5+)-->
33+
<!--
34+
DocumentTypes -> DeletePropertyValues -
35+
remove property values that are not in the uSync file
36+
-->
37+
<DocumentTypes
38+
DeletePropertyValues="false"
39+
/>
40+
41+
</usync>
42+
</configuration>

Umbraco.CodeGen.Integration/packages.config

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<packages>
33
<package id="ClientDependency" version="1.7.0.4" targetFramework="net40" developmentDependency="true" />
44
<package id="ClientDependency-Mvc" version="1.7.0.4" targetFramework="net40" developmentDependency="true" />
5-
<package id="HtmlAgilityPack" version="1.4.6" targetFramework="net40" developmentDependency="true" />
5+
<package id="HtmlAgilityPack" version="1.4.6" targetFramework="net40" developmentDependency="true" requireReinstallation="True" />
6+
<package id="ICSharpCode.NRefactory" version="5.3.0" targetFramework="net45" />
67
<package id="Lucene.Net" version="2.9.4.1" targetFramework="net40" developmentDependency="true" />
78
<package id="Microsoft.AspNet.Mvc" version="4.0.30506.0" targetFramework="net40" developmentDependency="true" />
89
<package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="1.0.1" targetFramework="net40" developmentDependency="true" />
@@ -12,14 +13,13 @@
1213
<package id="Microsoft.AspNet.WebApi.Core" version="4.0.30506.0" targetFramework="net40" developmentDependency="true" />
1314
<package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.30506.0" targetFramework="net40" developmentDependency="true" />
1415
<package id="Microsoft.AspNet.WebPages" version="2.0.30506.0" targetFramework="net40" developmentDependency="true" />
15-
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" developmentDependency="true" />
16+
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" developmentDependency="true" requireReinstallation="True" />
1617
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" developmentDependency="true" />
1718
<package id="MiniProfiler" version="2.1.0" targetFramework="net40" developmentDependency="true" />
19+
<package id="Mono.Cecil" version="0.9.5.4" targetFramework="net45" />
1820
<package id="MySql.Data" version="6.6.5" targetFramework="net40" developmentDependency="true" />
1921
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" developmentDependency="true" />
2022
<package id="SharpZipLib" version="0.86.0" targetFramework="net40" developmentDependency="true" />
2123
<package id="UmbracoCms.Core" version="6.1.6" targetFramework="net40" developmentDependency="true" />
2224
<package id="xmlrpcnet" version="2.5.0" targetFramework="net40" developmentDependency="true" />
23-
<package id="ICSharpCode.NRefactory" version="5.3.0" targetFramework="net45" />
24-
<package id="Mono.Cecil" version="0.9.5.4" targetFramework="net45" />
2525
</packages>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Umbraco.Tests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Umbraco.Tests")]
13+
[assembly: AssemblyCopyright("Copyright © 2013")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("41d3e760-88d1-45a6-8e64-51ed3266d7b2")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using Umbraco.Core;
5+
using Umbraco.Core.Models;
6+
7+
namespace Umbraco.CodeGen.WaitForSixTwo
8+
{
9+
public class PublishedContentFactoryHook : IApplicationEventHandler
10+
{
11+
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
12+
{
13+
Core.Models.PublishedContent.PublishedContentModelFactoryResolver.Current.SetFactory(new PublishedContentModelFactoryImpl());
14+
}
15+
16+
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
17+
{
18+
19+
}
20+
21+
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
22+
{
23+
}
24+
}
25+
26+
internal class PublishedContentModelFactoryImpl : Core.Models.PublishedContent.IPublishedContentModelFactory
27+
{
28+
//private readonly Dictionary<string, ConstructorInfo> _constructors
29+
// = new Dictionary<string, ConstructorInfo>();
30+
31+
private readonly Dictionary<string, Func<IPublishedContent, IPublishedContent>> _constructors
32+
= new Dictionary<string, Func<IPublishedContent, IPublishedContent>>();
33+
34+
public PublishedContentModelFactoryImpl()
35+
{
36+
var types = PluginManager.Current.ResolveTypes<PublishedContentModel>();
37+
var ctorArgTypes = new[] { typeof(IPublishedContent) };
38+
39+
foreach (var type in types)
40+
{
41+
var constructor = type.GetConstructor(ctorArgTypes);
42+
if (constructor == null)
43+
throw new InvalidOperationException(string.Format("Type {0} is missing a public constructor with one argument of type IPublishedContent.", type.FullName));
44+
var attribute = type.GetCustomAttribute<Core.Models.PublishedContent.PublishedContentModelAttribute>(false);
45+
var typeName = attribute == null ? type.Name : attribute.ContentTypeAlias;
46+
typeName = typeName.ToLowerInvariant();
47+
48+
if (_constructors.ContainsKey(typeName))
49+
throw new InvalidOperationException(string.Format("More that one type want to be a model for content type {0}.", typeName));
50+
51+
// should work everywhere, but slow
52+
//_constructors[typeName] = constructor;
53+
54+
// much faster with a dynamic method but potential MediumTrust issues
55+
// here http://stackoverflow.com/questions/16363838/how-do-you-call-a-constructor-via-an-expression-tree-on-an-existing-object
56+
57+
// fast enough and works in MediumTrust
58+
// read http://boxbinary.com/2011/10/how-to-run-a-unit-test-in-medium-trust-with-nunitpart-three-umbraco-framework-testing/
59+
var exprArg = Expression.Parameter(typeof(IPublishedContent), "content");
60+
var exprNew = Expression.New(constructor, exprArg);
61+
var expr = Expression.Lambda<Func<IPublishedContent, IPublishedContent>>(exprNew, exprArg);
62+
var func = expr.Compile();
63+
_constructors[typeName] = func;
64+
}
65+
}
66+
67+
public IPublishedContent CreateModel(IPublishedContent content)
68+
{
69+
// be case-insensitive
70+
var contentTypeAlias = content.DocumentTypeAlias.ToLowerInvariant();
71+
72+
//ConstructorInfo constructor;
73+
//return _constructors.TryGetValue(contentTypeAlias, out constructor)
74+
// ? (IPublishedContent) constructor.Invoke(new object[] { content })
75+
// : content;
76+
77+
Func<IPublishedContent, IPublishedContent> constructor;
78+
return _constructors.TryGetValue(contentTypeAlias, out constructor)
79+
? constructor(content)
80+
: content;
81+
}
82+
}
83+
84+
public abstract class PublishedContentModel : Umbraco.Core.Models.PublishedContent.PublishedContentExtended
85+
{
86+
/// <summary>
87+
/// Initializes a new instance of the <see cref="PublishedContentModel"/> class with
88+
/// an original <see cref="IPublishedContent"/> instance.
89+
/// </summary>
90+
/// <param name="content">The original content.</param>
91+
protected PublishedContentModel(IPublishedContent content)
92+
: base(content)
93+
{ }
94+
}
95+
}

0 commit comments

Comments
 (0)