Skip to content

Commit 2118500

Browse files
committed
Merged the latest work on testing and improvement into the master development channel
2 parents 8d12665 + 4775828 commit 2118500

File tree

19 files changed

+246
-96
lines changed

19 files changed

+246
-96
lines changed

Parser/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
1818
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
1919

20-
[assembly: AssemblyVersion("0.0.1.0")]
20+
[assembly: AssemblyVersion("0.1.0.0")]
2121

2222
// The following attributes are used to specify the signing key for the assembly,
2323
// if desired. See the Mono documentation for more information about signing.

ZptViewEngine/ZptView.cs renamed to Parser/Mvc/ZptView.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Web.Mvc;
33
using System.IO;
44

5-
namespace CraigFowler.Web.ZPT
5+
namespace CraigFowler.Web.ZPT.Mvc
66
{
77
/// <summary>
88
/// <para>Represents an <see cref="IView"/> that represents a ZPT page.</para>
@@ -43,7 +43,7 @@ protected ZptViewEngine Engine {
4343
/// </param>
4444
public void Render (ViewContext viewContext, TextWriter writer)
4545
{
46-
ZptDocument document = this.Engine.GetZptDocument(this.RelativeViewPath);
46+
IZptDocument document = this.Engine.GetZptDocument(this.RelativeViewPath);
4747
ITemplateDocument template = document.GetTemplateDocument();
4848

4949
template.TalesContext.AddDefinition("documents", this.Engine.GetViewCache());

ZptViewEngine/ZptViewEngine.cs renamed to Parser/Mvc/ZptViewEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.IO;
55
using CraigFowler.Web.ZPT.Tales;
66

7-
namespace CraigFowler.Web.ZPT
7+
namespace CraigFowler.Web.ZPT.Mvc
88
{
99
/// <summary>
1010
/// <para>Provides an MVC2 <see cref="IViewEngine"/> implementation for serving ZPT pages.</para>
@@ -41,12 +41,12 @@ public string ViewRoot
4141
/// <returns>
4242
/// A <see cref="ZptDocument"/>
4343
/// </returns>
44-
public ZptDocument GetZptDocument(string path)
44+
public IZptDocument GetZptDocument(string path)
4545
{
46-
ZptDocument output;
46+
IZptDocument output;
4747
ZptDocumentCollection viewCache = GetViewCache();
4848

49-
output = viewCache.RetrieveItem(ReformatPath(path)) as ZptDocument;
49+
output = viewCache.RetrieveItem(ReformatPath(path)) as IZptDocument;
5050

5151
if(output == null)
5252
{

Parser/Parser.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<ProjectGuid>{B887669F-17F7-45E9-BE79-A87570BFFA80}</ProjectGuid>
99
<OutputType>Library</OutputType>
1010
<RootNamespace>CraigFowler.Web.ZPT</RootNamespace>
11-
<AssemblyName>parser</AssemblyName>
11+
<AssemblyName>zpt-parser</AssemblyName>
1212
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
1313
<newfilesearch>OnLoadAutoInsert</newfilesearch>
1414
</PropertyGroup>
@@ -37,6 +37,8 @@
3737
<ItemGroup>
3838
<Reference Include="System" />
3939
<Reference Include="System.Xml" />
40+
<Reference Include="System.Web" />
41+
<Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
4042
</ItemGroup>
4143
<ItemGroup>
4244
<Compile Include="AssemblyInfo.cs" />
@@ -81,6 +83,8 @@
8183
<Compile Include="ZptDocumentCollection.cs" />
8284
<Compile Include="Metal\IMetalDocument.cs" />
8385
<Compile Include="Metal\MetalMacroCollection.cs" />
86+
<Compile Include="Mvc\ZptView.cs" />
87+
<Compile Include="Mvc\ZptViewEngine.cs" />
8488
</ItemGroup>
8589
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
8690
<ItemGroup>
@@ -91,6 +95,7 @@
9195
<Folder Include="Tal\Exceptions\" />
9296
<Folder Include="Metal\" />
9397
<Folder Include="Metal\Exceptions\" />
98+
<Folder Include="Mvc\" />
9499
</ItemGroup>
95100
<ProjectExtensions>
96101
<MonoDevelop>

Parser/ZptDocument.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,15 @@ private MetalMacroCollection MacroCache {
108108
/// <returns>
109109
/// An object instance that implements <see cref="ITemplateDocument"/>.
110110
/// </returns>
111-
public virtual ITemplateDocument GetTemplateDocument()
111+
public ITemplateDocument GetTemplateDocument()
112112
{
113113
if(this.UnderlyingDocument == null)
114114
{
115115
this.UnderlyingDocument = this.Metadata.LoadDocument();
116116
}
117-
117+
118+
this.AssignModelData(this.UnderlyingDocument.TalesContext);
119+
118120
return this.UnderlyingDocument;
119121
}
120122

@@ -147,6 +149,32 @@ public MetalMacroCollection GetMacros()
147149

148150
return this.MacroCache;
149151
}
152+
153+
/// <summary>
154+
/// <para>
155+
/// Helper method to perform assignments of data (perhaps from a domain model of some kind) to the root
156+
/// <see cref="TalesContext"/> of the ZPT document that is to be rendered by this view.
157+
/// </para>
158+
/// </summary>
159+
/// <remarks>
160+
/// <para>
161+
/// This method does nothing on its own, it serves as a hook that implementing classes may override in order to
162+
/// provide data to the TALES context of the view that will be rendering.
163+
/// </para>
164+
/// <para>
165+
/// In a derived class, data may be added to the <paramref name="talesContext"/> using the
166+
/// <see cref="TalesContext.AddDefinition(string,object)"/> method. These definitions are added at the root level
167+
/// of the TALES context of the document. This data to be added may be taken from a domain model of some kind or
168+
/// from a session state or any other source.
169+
/// </para>
170+
/// </remarks>
171+
/// <param name="talesContext">
172+
/// A <see cref="TalesContext"/>
173+
/// </param>
174+
protected virtual void AssignModelData(TalesContext talesContext)
175+
{
176+
// Intentionally do nothing here. It is up to implementing classes to add code to overrides of this method.
177+
}
150178

151179
#endregion
152180

Parser/ZptMetadata.cs

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class ZptMetadata
7070
#region fields
7171

7272
private Type documentClass;
73+
private static Type defaultDocumentClass;
7374

7475
#endregion
7576

@@ -95,21 +96,28 @@ public ZptDocumentType DocumentType
9596
public Type DocumentClass
9697
{
9798
get {
98-
return documentClass;
99+
Type output = documentClass;
100+
101+
if(output == null)
102+
{
103+
output = DefaultDocumentClass;
104+
}
105+
106+
return output;
99107
}
100108
set {
109+
/* If we are passed a null reference then fall back to the default type.
110+
* If we are passed a concrete type then try using that (if it implements the required interface)
111+
* Otherwise throw an exception.
112+
*/
101113
if(value == null)
102114
{
103-
throw new ArgumentNullException("value");
104-
}
105-
106-
bool implementsIZptDocument = ImplementsRequiredInterface(value);
107-
108-
// If we found a type that implements IZptDocument then we use it, otherwise throw an exception
109-
if(implementsIZptDocument)
110-
{
111-
documentClass = value;
115+
documentClass = DefaultDocumentClass;
112116
}
117+
else if(ImplementsRequiredInterface(value))
118+
{
119+
documentClass = value;
120+
}
113121
else
114122
{
115123
throw new ArgumentOutOfRangeException("value",
@@ -161,6 +169,35 @@ public string DocumentFilePath
161169
get;
162170
set;
163171
}
172+
173+
/// <summary>
174+
/// <para>Gets and sets a default value for <see cref="DocumentClass"/>.</para>
175+
/// </summary>
176+
public static Type DefaultDocumentClass
177+
{
178+
get {
179+
return defaultDocumentClass;
180+
}
181+
set {
182+
/* If we are passed a null reference then throw an exception (this property may not be null).
183+
* If we are passed a concrete type then try using that (if it implements the required interface)
184+
* Otherwise throw an exception.
185+
*/
186+
if(value == null)
187+
{
188+
throw new ArgumentNullException("value");
189+
}
190+
else if(ImplementsRequiredInterface(value))
191+
{
192+
defaultDocumentClass = value;
193+
}
194+
else
195+
{
196+
throw new ArgumentOutOfRangeException("value",
197+
"The type passed to this property does not implement IZptDocument.");
198+
}
199+
}
200+
}
164201

165202
/// <summary>
166203
/// <para>
@@ -303,8 +340,8 @@ private void WriteToXml(TextWriter writer)
303340
public ZptMetadata()
304341
{
305342
this.DocumentType = ZptDocumentType.Metal;
306-
this.DocumentClass = typeof(ZptDocument);
307343
this.DocumentFilePath = null;
344+
this.DocumentClass = null;
308345
}
309346

310347
/// <summary>
@@ -313,6 +350,7 @@ public ZptMetadata()
313350
static ZptMetadata()
314351
{
315352
ForeignDocumentClasses = new Dictionary<string, Type>();
353+
DefaultDocumentClass = typeof(ZptDocument);
316354
}
317355

318356
#endregion
@@ -491,6 +529,41 @@ public static void RegisterDocumentClass(Type typeToRegister)
491529

492530
ForeignDocumentClasses.Add(typeToRegister.FullName, typeToRegister);
493531
}
532+
533+
/// <summary>
534+
/// <para>
535+
/// Registers all of the compatible types within an <see cref="Assembly"/> using
536+
/// <see cref="RegisterDocumentClass"/>.
537+
/// </para>
538+
/// </summary>
539+
/// <remarks>
540+
/// <para>
541+
/// This method is a shortcut to separately calling <see cref="RegisterDocumentClass"/> on each type within an
542+
/// <see cref="Assembly"/>. Instead this method scans the assembly for every compatible type and registers them.
543+
/// </para>
544+
/// </remarks>
545+
/// <param name="fromAssembly">
546+
/// A <see cref="Assembly"/>
547+
/// </param>
548+
public static void RegisterDocumentClasses(Assembly fromAssembly)
549+
{
550+
Type[] allTypes;
551+
552+
if(fromAssembly == null)
553+
{
554+
throw new ArgumentNullException("fromAssembly");
555+
}
556+
557+
allTypes = fromAssembly.GetExportedTypes();
558+
559+
foreach(Type type in allTypes)
560+
{
561+
if(ImplementsRequiredInterface(type))
562+
{
563+
RegisterDocumentClass(type);
564+
}
565+
}
566+
}
494567

495568
/// <summary>
496569
/// <para>

SampleMvcSite/Content/Member.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using CraigFowler.Web.ZPT.Tales;
33

4-
namespace SampleMvcSite.Content
4+
namespace CraigFowler.Samples.Mvc.Content
55
{
66
/// <summary>
77
/// <para>Stub class for testing a member-like object.</para>

SampleMvcSite/Controllers/HomeController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using System.Web;
55
using System.Web.Mvc;
66
using System.Web.Mvc.Ajax;
7-
using SampleMvcSite.Content;
7+
using CraigFowler.Samples.Mvc.Content;
88

9-
namespace Controllers
9+
namespace CraigFowler.Samples.Mvc.Controllers
1010
{
1111
[HandleError]
1212
public class HomeController : Controller
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
using System.Web.Mvc;
6+
using System.Web.Mvc.Ajax;
7+
namespace CraigFowler.Samples.Mvc.Controllers
8+
{
9+
public class TestController : Controller
10+
{
11+
public ActionResult Index()
12+
{
13+
return View("test-page");
14+
}
15+
}
16+
}
17+

SampleMvcSite/Global.asax

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<%@ Application Inherits="SampleMvcSite.MvcApplication" %>
1+
<%@ Application Inherits="CraigFowler.Samples.Mvc.MvcApplication" %>

0 commit comments

Comments
 (0)