Skip to content

Commit

Permalink
support exchanging values on DynamicViewBag across AppDomains
Browse files Browse the repository at this point in the history
  • Loading branch information
campersau committed Dec 1, 2017
1 parent 311c3bb commit 4500022
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
121 changes: 121 additions & 0 deletions src/source/RazorEngine.Core/Common/CrossAppDomainDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System.Collections;
using System.Collections.Generic;

namespace RazorEngine.Common
{
internal class CrossAppDomainDictionary<TKey, TValue> : CrossAppDomainObject, IDictionary<TKey, TValue>
{
#region Fields

private readonly IDictionary<TKey, TValue> _dict;

#endregion

#region Constructor

public CrossAppDomainDictionary()
{
_dict = new Dictionary<TKey, TValue>();
}

public CrossAppDomainDictionary(int capacity)
{
_dict = new Dictionary<TKey, TValue>(capacity);
}

public CrossAppDomainDictionary(IEqualityComparer<TKey> comparer)
{
_dict = new Dictionary<TKey, TValue>(comparer);
}

public CrossAppDomainDictionary(IDictionary<TKey, TValue> dictionary)
{
_dict = new Dictionary<TKey, TValue>(dictionary);
}

public CrossAppDomainDictionary(int capacity, IEqualityComparer<TKey> comparer)
{
_dict = new Dictionary<TKey, TValue>(capacity, comparer);
}

public CrossAppDomainDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
{
_dict = new Dictionary<TKey, TValue>(dictionary, comparer);
}

#endregion

#region Properties

public TValue this[TKey key] { get { return _dict[key]; } set { _dict[key] = value; } }

public ICollection<TKey> Keys { get { return _dict.Keys; } }

public ICollection<TValue> Values { get { return _dict.Values; } }

public int Count { get { return _dict.Count; } }

public bool IsReadOnly { get { return _dict.IsReadOnly; } }

#endregion

#region Methods

public void Add(TKey key, TValue value)
{
_dict.Add(key, value);
}

public void Add(KeyValuePair<TKey, TValue> item)
{
_dict.Add(item);
}

public void Clear()
{
_dict.Clear();
}

public bool Contains(KeyValuePair<TKey, TValue> item)
{
return _dict.Contains(item);
}

public bool ContainsKey(TKey key)
{
return _dict.ContainsKey(key);
}

public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
_dict.CopyTo(array, arrayIndex);
}

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _dict.GetEnumerator();
}

public bool Remove(TKey key)
{
return _dict.Remove(key);
}

public bool Remove(KeyValuePair<TKey, TValue> item)
{
return _dict.Remove(item);
}

public bool TryGetValue(TKey key, out TValue value)
{
return _dict.TryGetValue(key, out value);
}

IEnumerator IEnumerable.GetEnumerator()
{
return _dict.GetEnumerator();
}

#endregion
}
}
1 change: 1 addition & 0 deletions src/source/RazorEngine.Core/RazorEngine.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
<ItemGroup>
<Compile Include="AttributeValue.cs" />
<Compile Include="CodeGenerators\SetModelTypeCodeGenerator.cs" />
<Compile Include="Common\CrossAppDomainDictionary.cs" />
<Compile Include="Compilation\CompilerServiceBase.cs" />
<Compile Include="Compilation\CompilerServiceBuilder.cs" />
<Compile Include="Compilation\CompilerServicesUtility.cs" />
Expand Down
3 changes: 2 additions & 1 deletion src/source/RazorEngine.Core/Templating/DynamicViewBag.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace RazorEngine.Templating
{
using RazorEngine.Common;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -13,7 +14,7 @@ public class DynamicViewBag : DynamicObject
{
#region Fields
private readonly IDictionary<string, object> _dict =
new System.Collections.Generic.Dictionary<string, object>();
new CrossAppDomainDictionary<string, object>();
#endregion
/// <summary>
/// Create a new DynamicViewBag.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ public void IsolatedRazorEngineService_DynamicViewBag_InSandBox()
}
}

/// <summary>
/// Tests that exchanging values on the viewbag works across app domains
/// </summary>
[Test]
public void IsolatedRazorEngineService_DynamicViewBag_FromSandBox()
{
using (var service = IsolatedRazorEngineService.Create(SandboxCreator))
{
const string template = "@{ ViewBag.Test = \"TestItem\"; }";
const string expected = "TestItem";
dynamic viewbag = new DynamicViewBag();

string result = service.RunCompile(template, "test", (Type)null, (object)null, (DynamicViewBag)viewbag);
Assert.AreEqual(expected, viewbag.Test);
}
}

/// <summary>
/// Tests that a simple viewbag is working.
/// </summary>
Expand Down

0 comments on commit 4500022

Please sign in to comment.