Skip to content

Instantly share code, notes, and snippets.

@roryf
Created July 27, 2011 15:19
Show Gist options
  • Save roryf/1109592 to your computer and use it in GitHub Desktop.
Save roryf/1109592 to your computer and use it in GitHub Desktop.
Flash message implementation for ASP.NET MVC
public class Flash : DynamicObject
{
private readonly TempDataDictionary _store;
private const string KeyPrefix = "Flash";
public Flash(TempDataDictionary store)
{
_store = store;
}
// Implementing this function improves the debugging experience as it provides the debugger with the list of all
// the properties currently defined on the object
public override IEnumerable<string> GetDynamicMemberNames()
{
return _store.Keys.Where(x => x.StartsWith(KeyPrefix)).Select(x => x.Substring(5));
}
private string GetKey(string messageName)
{
return string.Format("{0}{1}", KeyPrefix, messageName);
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (binder.Name.StartsWith("Has"))
{
var requestedKey = GetKey(binder.Name.Substring(3));
result = _store.ContainsKey(requestedKey);
}
else
{
var requestedKey = GetKey(binder.Name);
result = _store[requestedKey];
}
// since ViewDataDictionary always returns a result even if the key does not exist, always return true
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
var key = GetKey(binder.Name);
_store[key] = value;
// you can always set a key in the dictionary so return true
return true;
}
}
public static class FlashExtensions
{
public static dynamic Flash(this TempDataDictionary @this)
{
return new Flash(@this);
}
}
@if (TempData.Flash().HasSuccess)
{
<div class="message success" role="alert">
<span class="icon"> </span>
<p>
@TempData.Flash().Success
</p>
</div>
}
public class FlashSpecs
{
[Subject(typeof(Flash), "setting message")]
public class when_setting_message
{
private static dynamic flash;
private static TempDataDictionary tempData;
Establish context = () =>
{
tempData = new TempDataDictionary();
flash = new Flash(tempData);
};
Because of = () => flash.Success = "succes";
It should_populate_temp_data = () => tempData["FlashSuccess"].ShouldNotBeNull();
}
[Subject(typeof(Flash), "testing for message presence")]
public class when_message_is_set
{
private static dynamic flash;
private static bool hasMessage;
Establish context = () =>
{
var tempData = new TempDataDictionary();
tempData["FlashSuccess"] = "success";
flash = new Flash(tempData);
};
Because of = () => hasMessage = flash.HasSuccess;
It should_report_has_message = () => hasMessage.ShouldBeTrue();
}
[Subject(typeof(Flash), "testing for message presence")]
public class when_message_is_not_set
{
private static dynamic flash;
private static bool hasMessage;
Establish context = () =>
{
var tempData = new TempDataDictionary();
flash = new Flash(tempData);
};
Because of = () => hasMessage = flash.HasSuccess;
It should_report_does_not_have_message = () => hasMessage.ShouldBeFalse();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment