/// <summary> /// Applies previously captured state delta to view, effectively replaying cached fragment /// </summary> /// <param name="memento">memento captured in previous begin/end calls</param> public void DoMemento(CacheMemento memento) { memento.SpoolOutput.WriteTo(_state.Output); foreach (var content in memento.Content) { // create named content if it doesn't exist TextWriter writer; if (_state.Content.TryGetValue(content.Key, out writer) == false) { writer = new SpoolWriter(); _state.Content.Add(content.Key, writer); } // and in any case apply the delta var originator = TextWriterOriginator.Create(writer); originator.DoMemento(content.Value); } // add recorded once deltas that were not yet in this subject's table var newItems = memento.OnceTable.Where(once => _state.OnceTable.ContainsKey(once.Key) == false); foreach (var once in newItems) { _state.OnceTable.Add(once.Key, once.Value); } }
public CacheMemento EndMemento() { CacheMemento memento = new CacheMemento(); if (this._priorOutput != null) { this._spoolOutput.WriteTo(this._priorOutput); this._state.Output = this._priorOutput; memento.SpoolOutput = this._spoolOutput; } foreach (KeyValuePair <string, TextWriterOriginator> pair in this._priorContent) { TextWriterMemento memento2 = pair.Value.EndMemento(); if (memento2.Written.Any <string>(part => !string.IsNullOrEmpty(part))) { memento.Content.Add(pair.Key, memento2); } } foreach (KeyValuePair <string, TextWriter> pair2 in from kv in this._state.Content where !this._priorContent.ContainsKey(kv.Key) select kv) { TextWriterOriginator originator = TextWriterOriginator.Create(pair2.Value); memento.Content.Add(pair2.Key, originator.CreateMemento()); } IEnumerable <KeyValuePair <string, string> > source = from once in this._state.OnceTable where !this._priorOnceTable.ContainsKey(once.Key) select once; memento.OnceTable = source.ToDictionary <KeyValuePair <string, string>, string, string>(once => once.Key, once => once.Value); return(memento); }
public void BeginMemento() { foreach (KeyValuePair <string, TextWriter> pair in this._state.Content) { TextWriterOriginator originator = TextWriterOriginator.Create(pair.Value); this._priorContent.Add(pair.Key, originator); originator.BeginMemento(); } this._priorOnceTable = this._state.OnceTable.ToDictionary <KeyValuePair <string, string>, string, string>(kv => kv.Key, kv => kv.Value); if (!this._state.Content.Any <KeyValuePair <string, TextWriter> >(kv => object.ReferenceEquals(kv.Value, this._state.Output))) { this._priorOutput = this._state.Output; this._spoolOutput = new SpoolWriter(); this._state.Output = this._spoolOutput; } }
/// <summary> /// Establishes original state for memento capturing purposes /// </summary> public void BeginMemento() { foreach (var content in _state.Content) { var writerOriginator = TextWriterOriginator.Create(content.Value); _priorContent.Add(content.Key, writerOriginator); writerOriginator.BeginMemento(); } _priorOnceTable = _state.OnceTable.ToDictionary(kv => kv.Key, kv => kv.Value); // capture current output also if it's not locked into a named output at the moment // this could be a case in view's output, direct to network, or various macro or content captures if (_state.Content.Any(kv => ReferenceEquals(kv.Value, _state.Output)) == false) { _priorOutput = _state.Output; _spoolOutput = new SpoolWriter(); _state.Output = _spoolOutput; } }
public void DoMemento(CacheMemento memento) { memento.SpoolOutput.WriteTo(this._state.Output); foreach (KeyValuePair <string, TextWriterMemento> pair in memento.Content) { TextWriter writer; if (!this._state.Content.TryGetValue(pair.Key, out writer)) { writer = new SpoolWriter(); this._state.Content.Add(pair.Key, writer); } TextWriterOriginator.Create(writer).DoMemento(pair.Value); } foreach (KeyValuePair <string, string> pair2 in from once in memento.OnceTable where !this._state.OnceTable.ContainsKey(once.Key) select once) { this._state.OnceTable.Add(pair2.Key, pair2.Value); } }
/// <summary> /// Finalizes state change and creates memento /// </summary> /// <returns>memento holding the details of the resulting state delta</returns> public CacheMemento EndMemento() { var memento = new CacheMemento(); // for capturing subject.Output directly, replay what was spooled, and save it // in the memento if (_priorOutput != null) { _spoolOutput.WriteTo(_priorOutput); _state.Output = _priorOutput; memento.SpoolOutput = _spoolOutput; } // save any deltas on named content that have expanded foreach (var content in _priorContent) { var textMemento = content.Value.EndMemento(); if (textMemento.Written.Any(part => string.IsNullOrEmpty(part) == false)) { memento.Content.Add(content.Key, textMemento); } } // also save any named content in it's entirety that added created after BeginMemento was called foreach (var content in _state.Content.Where(kv => _priorContent.ContainsKey(kv.Key) == false)) { var originator = TextWriterOriginator.Create(content.Value); memento.Content.Add(content.Key, originator.CreateMemento()); } // capture anything from the oncetable that was added after BeginMemento was called var newItems = _state.OnceTable.Where(once => _priorOnceTable.ContainsKey(once.Key) == false); memento.OnceTable = newItems.ToDictionary(once => once.Key, once => once.Value); return(memento); }