Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 247 lines (202 sloc) 7.999 kb
69a5b5bb »
2009-01-20 Adding decorator pattern, removing Key, moving Entry to it's own class.
1 // Copyright 2008 Louis DeJardin - http://whereslou.com
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 using System;
16 using System.Collections.Generic;
17 using System.IO;
18 using System.Threading;
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
19 using Spark.Caching;
69a5b5bb »
2009-01-20 Adding decorator pattern, removing Key, moving Entry to it's own class.
20 using Spark.Spool;
b6a5c3d7 »
2009-07-20 Multipart keys in cache
21 using Spark.Utilities;
69a5b5bb »
2009-01-20 Adding decorator pattern, removing Key, moving Entry to it's own class.
22
23 namespace Spark
24 {
25 public class SparkViewContext
26 {
a3f09d2b »
2009-07-09 Removing the ICacheSubject interface
27 public SparkViewContext()
28 {
29 Content = new Dictionary<string, TextWriter>();
30 Globals = new Dictionary<string, object>();
31 OnceTable = new Dictionary<string, string>();
32 }
33
69a5b5bb »
2009-01-20 Adding decorator pattern, removing Key, moving Entry to it's own class.
34 public TextWriter Output { get; set; }
35 public Dictionary<string, TextWriter> Content { get; set; }
36 public Dictionary<string, object> Globals { get; set; }
37 public Dictionary<string, string> OnceTable { get; set; }
38 }
39
a3f09d2b »
2009-07-09 Removing the ICacheSubject interface
40 public abstract class SparkViewBase : ISparkView
69a5b5bb »
2009-01-20 Adding decorator pattern, removing Key, moving Entry to it's own class.
41 {
42 private SparkViewContext _sparkViewContext;
43
44 public abstract Guid GeneratedViewId { get; }
45
46 public virtual bool TryGetViewData(string name, out object value)
47 {
48 value = null;
49 return false;
50 }
51
52 public virtual SparkViewContext SparkViewContext
53 {
54 get
55 {
56 return _sparkViewContext ??
a3f09d2b »
2009-07-09 Removing the ICacheSubject interface
57 Interlocked.CompareExchange(ref _sparkViewContext, new SparkViewContext(), null) ??
69a5b5bb »
2009-01-20 Adding decorator pattern, removing Key, moving Entry to it's own class.
58 _sparkViewContext;
59 }
60 set { _sparkViewContext = value; }
61 }
62
63
64 public TextWriter Output { get { return SparkViewContext.Output; } set { SparkViewContext.Output = value; } }
65 public Dictionary<string, TextWriter> Content { get { return SparkViewContext.Content; } set { SparkViewContext.Content = value; } }
66 public Dictionary<string, object> Globals { get { return SparkViewContext.Globals; } set { SparkViewContext.Globals = value; } }
67 public Dictionary<string, string> OnceTable { get { return SparkViewContext.OnceTable; } set { SparkViewContext.OnceTable = value; } }
68
69 public IDisposable OutputScope(string name)
70 {
71 TextWriter writer;
72 if (!Content.TryGetValue(name, out writer))
73 {
74 writer = new SpoolWriter();
75 Content.Add(name, writer);
76 }
77 return new OutputScopeImpl(this, writer);
78 }
79
80 public IDisposable OutputScope(TextWriter writer)
81 {
82 return new OutputScopeImpl(this, writer);
83 }
84
85 public IDisposable OutputScope()
86 {
87 return new OutputScopeImpl(this, new SpoolWriter());
88 }
89
6067a9d8 »
2010-04-03 Added <markdown> tag to spark
90 public IDisposable MarkdownOutputScope()
91 {
92 return new MarkdownOutputScopeImpl(this, new SpoolWriter());
93 }
94
69a5b5bb »
2009-01-20 Adding decorator pattern, removing Key, moving Entry to it's own class.
95
96 public bool Once(object flag)
97 {
98 var flagString = Convert.ToString(flag);
99 if (SparkViewContext.OnceTable.ContainsKey(flagString))
100 return false;
101
102 SparkViewContext.OnceTable.Add(flagString, null);
103 return true;
104 }
105
106
107 public class OutputScopeImpl : IDisposable
108 {
109 private readonly SparkViewBase view;
110 private readonly TextWriter previous;
111
112 public OutputScopeImpl(SparkViewBase view, TextWriter writer)
113 {
114 this.view = view;
115 previous = view.Output;
116 view.Output = writer;
117 }
118
119 public void Dispose()
120 {
121 view.Output = previous;
122 }
123 }
124
6067a9d8 »
2010-04-03 Added <markdown> tag to spark
125 public class MarkdownOutputScopeImpl : IDisposable
126 {
127 private readonly SparkViewBase view;
128 private readonly TextWriter previous;
129
130 public MarkdownOutputScopeImpl(SparkViewBase view, TextWriter writer)
131 {
132 this.view = view;
133 previous = view.Output;
134 view.Output = writer;
135 }
136
137 public void Dispose()
138 {
139 var source = view.Output.ToString();
140 view.Output = previous;
141
142 var markdown = new Markdown();
143
144 view.Output.Write(markdown.Transform(source));
145 }
146 }
147
b7bf496b »
2009-07-20 Adding cache expires attribute
148 protected bool BeginCachedContent(string site, CacheExpires expires, params object[] key)
cb105822 »
2009-07-08 Roughing out caching support
149 {
b6a5c3d7 »
2009-07-20 Multipart keys in cache
150 _currentCacheScope = new CacheScopeImpl(this, CacheUtilities.ToIdentifier(site, key), expires);
0b208d57 »
2009-07-20 Cache attibute and value holder
151 if (_currentCacheScope.Begin())
152 return true;
153
154 EndCachedContent();
155 return false;
cb105822 »
2009-07-08 Roughing out caching support
156 }
157
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
158 protected void EndCachedContent()
159 {
5fcd8d4d »
2009-07-20 Adding a cache signal attribute with ICacheSignal and CacheSignal
160 _currentCacheScope = _currentCacheScope.End(null);
161 }
162
163 protected void EndCachedContent(ICacheSignal signal)
164 {
165 _currentCacheScope = _currentCacheScope.End(signal);
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
166 }
167
168 private CacheScopeImpl _currentCacheScope;
cb105822 »
2009-07-08 Roughing out caching support
169 public ICacheService CacheService { get; set; }
170
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
171
172 private class CacheScopeImpl
cb105822 »
2009-07-08 Roughing out caching support
173 {
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
174 private readonly CacheScopeImpl _previousCacheScope;
cb105822 »
2009-07-08 Roughing out caching support
175
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
176 private readonly ICacheService _cacheService;
177 private readonly CacheOriginator _originator;
178 private readonly string _identifier;
b7bf496b »
2009-07-20 Adding cache expires attribute
179 private readonly CacheExpires _expires;
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
180 private bool _recording;
cb105822 »
2009-07-08 Roughing out caching support
181
182 private static readonly ICacheService _nullCacheService = new NullCacheService();
183
b6a5c3d7 »
2009-07-20 Multipart keys in cache
184 public CacheScopeImpl(SparkViewBase view, string identifier, CacheExpires expires)
cb105822 »
2009-07-08 Roughing out caching support
185 {
b6a5c3d7 »
2009-07-20 Multipart keys in cache
186 _identifier = identifier;
b7bf496b »
2009-07-20 Adding cache expires attribute
187 _expires = expires;
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
188 _previousCacheScope = view._currentCacheScope;
189 _cacheService = view.CacheService ?? _nullCacheService;
a3f09d2b »
2009-07-09 Removing the ICacheSubject interface
190 _originator = new CacheOriginator(view.SparkViewContext);
cb105822 »
2009-07-08 Roughing out caching support
191 }
192
b6a5c3d7 »
2009-07-20 Multipart keys in cache
193
cb105822 »
2009-07-08 Roughing out caching support
194 public bool Begin()
195 {
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
196 var memento = _cacheService.Get(_identifier) as CacheMemento;
197 if (memento == null)
198 {
199 _recording = true;
200 _originator.BeginMemento();
201 }
202 else
cb105822 »
2009-07-08 Roughing out caching support
203 {
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
204 _recording = false;
205 _originator.DoMemento(memento);
cb105822 »
2009-07-08 Roughing out caching support
206 }
207
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
208 return _recording;
cb105822 »
2009-07-08 Roughing out caching support
209 }
210
5fcd8d4d »
2009-07-20 Adding a cache signal attribute with ICacheSignal and CacheSignal
211 public CacheScopeImpl End(ICacheSignal signal)
cb105822 »
2009-07-08 Roughing out caching support
212 {
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
213 if (_recording)
214 {
215 var memento = _originator.EndMemento();
5fcd8d4d »
2009-07-20 Adding a cache signal attribute with ICacheSignal and CacheSignal
216 _cacheService.Store(_identifier, _expires, signal, memento);
cf1951e0 »
2009-07-09 Fully implemented output, named content, and once flag caching
217 }
218 return _previousCacheScope;
cb105822 »
2009-07-08 Roughing out caching support
219 }
220
221
222 private class NullCacheService : ICacheService
223 {
224 public object Get(string identifier)
225 {
226 return null;
227 }
228
5fcd8d4d »
2009-07-20 Adding a cache signal attribute with ICacheSignal and CacheSignal
229 public void Store(string identifier, CacheExpires expires, ICacheSignal signal, object item)
cb105822 »
2009-07-08 Roughing out caching support
230 {
231 }
232 }
233 }
234
69a5b5bb »
2009-01-20 Adding decorator pattern, removing Key, moving Entry to it's own class.
235 public virtual void RenderView(TextWriter writer)
236 {
22f71031 »
2009-01-21 Changing RenderView and adding abstract Render to simplify decorated …
237 using (OutputScope(writer))
238 {
239 Render();
240 }
69a5b5bb »
2009-01-20 Adding decorator pattern, removing Key, moving Entry to it's own class.
241 }
22f71031 »
2009-01-21 Changing RenderView and adding abstract Render to simplify decorated …
242
243 public abstract void Render();
69a5b5bb »
2009-01-20 Adding decorator pattern, removing Key, moving Entry to it's own class.
244 }
cb105822 »
2009-07-08 Roughing out caching support
245
69a5b5bb »
2009-01-20 Adding decorator pattern, removing Key, moving Entry to it's own class.
246 }
Something went wrong with that request. Please try again.