Descriptor Pattern Part II

I described the descriptor patteren earlier, this time I want to elaborate a bit showing the advantage of extending it and using it in combination with an MVC application. I created two classes ApplicationAction and Account Action where AccountAction extends ApplicationAction.

Here is the implementation of the ApplicationAction

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Divelicious.Website.Code.Descriptor
{
public class ApplicationAction
{
private string _description = String.Empty;

private static readonly IDictionary dict = new Dictionary<string, ApplicationAction>();

protected ApplicationAction(string description)
{
_description = description;
dict.Add(description, this);
}

public override string ToString()
{
return _description;
}

public static ApplicationAction Parse(string description)
{
if (dict.Contains(description))
{
return dict[description] as ApplicationAction;
}
else
{
throw new NotImplementedException("This description is currently not supported.");
}
}

public static bool TryParse(string description, out ApplicationAction gender)
{
try
{
gender = Parse(description);
return true;
}
catch (NotImplementedException)
{
gender = null;
return false;
}
}

public static IEnumerable GetMembers()
{
return dict.Values.Cast().ToList();
}
}
}

Here is the AccountAction


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Divelicious.Website.Code.Descriptor
{
public class AccountAction : ApplicationAction
{
private AccountAction(string description)
: base(description)
{

}

// Account
public static readonly AccountAction Login = new AccountAction("Login");
public static readonly AccountAction LogOff = new AccountAction("LogOff");
public static readonly AccountAction Register = new AccountAction("Register");
public static readonly AccountAction Disassociate = new AccountAction("Disassociate");
public static readonly AccountAction Manage = new AccountAction("Manage");
public static readonly AccountAction ExternalLogin = new AccountAction("ExternalLogin");
public static readonly AccountAction ExternalLoginCallback = new AccountAction("ExternalLoginCallback");
public static readonly AccountAction ExternalLoginConfirmation = new AccountAction("ExternalLoginConfirmation");
public static readonly AccountAction ExternalLoginFailure = new AccountAction("ExternalLoginFailure");
public static readonly AccountAction ExternalLoginsList = new AccountAction("ExternalLoginsList");
public static readonly AccountAction RemoveExternalLogins = new AccountAction("RemoveExternalLogins");
}
}

 

And when you want to use it. 

<a href="@Url.Action(@AccountAction.Login.ToString(), "Account", new { Area="" })">Login</a>

Add comment

biuquote
  • Comment
  • Preview
Loading

Sponsored by

Calendar

<<  May 2018  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

RecentComments

TextBox