Search code examples
c#.netenumsembedded-resource

Resource file with multiple values (or access comments column via code)


Is it possible to have a resource file with entries that have multiple values.

Eg. Id like to have something like.

Resource.CanViewSection.Value
Resource.CanViewSection.Description.

My reason for this is that our database is being created with the code first approach, and we're going to need to implement very granular permissions in our application, so i'm expecting a lot of permissions & or possible repetition of items, and id like to centralize them all.

Thus in our database seeder i can do something like this:

private IEnumerable<Permission> SetupParameterPermissions()
{
    var innerCollection = new List<Permission>
        {
            //id like to do this
            this.New(Resource.CanViewSection.Value, Resource.CanViewSection.Description),

            //instead of this
            this.New("CanViewSection", "User can view the section")
        };

    return this.SetGroupId(innerCollection, PermissionGroupEnum.Parameters);
}

and in our services we can just run our access checks against the resource as well like this:

eg.

if(UserHasAccessTo(Resource.CanViewSection.Value))
{
// do something amazing
}

I've tried a few approaches, EG. adding a name to the value column of the resource & a description to the comment section, but i don't know how to programatically access the comments column of the resource file.

I realize I can achieve this effect with ENUMS as well, but i'm second guessing what the best approach would be, as we'll have a ton of permissions, and somehow the idea of a gigantic ENUM with 2 equally gigantic extensions weirds me out.

    public enum SomeEnum
    {          
        CanViewSection
    }    

    public static class SomeEnumExtensions
    {           
        public static string GetValue(this SomeEnum)
        {
            switch (me)
            {
                case SomeEnum.CanViewSection:
                    return "CanViewSection";
                default:
                    return "Fail!";
            }
        }

        public static string GetDescription(this SomeEnum)
        {
            switch (me)
            {
                case SomeEnum.CanViewSection:
                    return "YOLO!";
                default:
                    return "Fail!";
             }
        }
    }

I'm open to other suggestions as well?


Solution

  • Maybe you can try this (or something like it):

    1) Create resource file with your ID's. F.e.

       Resource.Code1
       Resource.Code2
    

    2) Create XML file and add it to project. It will look probably like this:

       <codes>
              <Code1 Value="Some value text" Description="Some description">
              <Code2 Value="Some value text" Description="Some description">
       </codes>
    

    3) Create some kind of wrapper class with 2 fields - Value and Description, f.e.

        public class ResourceWrapper
        {
        public string Value {get;set;}
        public string Description{get;set;}
        }
    

    3) Then create simple static method which will get value from XML file by code from your resource file, parse it, and return ResourceWrapper as a result.

      public static class ResourceHelper
      {
            public static ResourceWrapper GetSomeWrapper(string resourceCode);
      }
    

    Calling will look like (in your case):

      ResourceWrapper wrap = ResourceHelper.GetSomeWrapper(Resource.Code1.ToString());
      this.Add(new Permission(wrap.Value, wrap.Description));
    

    Probably you would like to store a collection of already wrapped objects in some kind of cache, or else.