My project has a helper class with multiple constants representing pre-defined roles .
public static class RolesHelper
{
public const string ModuloUsuarios = "Usuarios";
public const string ModuloMenus = "Menus";
public const string ModuloBanners = "Banners";
public const string ModuloGaleriaFotos = "GaleriasFotos";
public const string ModuloProgramacao = "Programacao";
public const string ModuloMetaTags = "MetaTags";
public const string ModuloNoticias = "Noticias";
public const string ModuloPaginas = "Paginas";
}
In the Seed
method (method used by the Entity Framework to update the database), I need to make a given user to be related to these values.
Currently there is a AdicionarUsuarioARole()
method that does the whole work, and this method is called several times, like this:
AdicionarUsuarioARole(user, RolesHelper.ModuloUsuarios);
AdicionarUsuarioARole(user, RolesHelper.ModuloMenus);
//E assim por diante
What I want is to get all public constants of this class in a collection, to iterate it and call the AdicionarUsuarioARole()
method with each value in this collection. This way, I do not have to worry about updating the Seed
method whenever I add a constant in this class.
For example:
var listaConstantes = RolesHelper.GetAllConstantValues();
foreach(var constVal in listaConstantes)
{
AdicionarUsuarioARole(user, constVal);
}