Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Refactor 'switch/case' That Is Based Enum Value

Refactor 'switch/case' That Is Based Enum Value

Scheduled Pinned Locked Moved C#
questionasp-netgame-devregexarchitecture
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt U
    wrote on last edited by
    #1

    While this project I'm on is MVC, this question isn't specific to MVC, so I felt like this was the right place. Anyhow, we are using the Repository pattern for data access. One particular entity type, e.g. SharedObj, has several other entity types that the SharedObj entity can be tied to. Each is an IEnumerable, like this:

    public class SharedObj
    {
    IEnumerable Obj1s;
    IEnumerable Obj2s;
    IEnumerable Obj3s;
    // More entity collections
    }

    So when we add a child object to one of those collections of SharedObj, we have a method like this:

    // 'entity' is of type 'SharedObj'

    public void AddObj(ObjType objType, int objId)
    {
    switch (objType)
    {
    case ObjType.Obj1:
    entity.Obj1s.Add(GetRepository().GetByID(objId);
    break;
    case ObjType.Obj2:
    entity.Obj2s.Add(GetRepository().GetByID(objId);
    break;
    case ObjType.Obj3:
    entity.Obj3s.Add(GetRepository().GetByID(objId);
    break;
    // case/break for each remaining entity type
    }
    }

    "ObjType" is an enumeration of object types. The "GetRepository" method calls "DependencyResolver.Current.GetService>();" and we use Unity for dependency injection. Is there a way to make a more generic "Add" call based on the supplied "ObjType"? Or is this the only concept that can be applied in order to achieve what we're doing?

    djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

    P P 2 Replies Last reply
    0
    • M Matt U

      While this project I'm on is MVC, this question isn't specific to MVC, so I felt like this was the right place. Anyhow, we are using the Repository pattern for data access. One particular entity type, e.g. SharedObj, has several other entity types that the SharedObj entity can be tied to. Each is an IEnumerable, like this:

      public class SharedObj
      {
      IEnumerable Obj1s;
      IEnumerable Obj2s;
      IEnumerable Obj3s;
      // More entity collections
      }

      So when we add a child object to one of those collections of SharedObj, we have a method like this:

      // 'entity' is of type 'SharedObj'

      public void AddObj(ObjType objType, int objId)
      {
      switch (objType)
      {
      case ObjType.Obj1:
      entity.Obj1s.Add(GetRepository().GetByID(objId);
      break;
      case ObjType.Obj2:
      entity.Obj2s.Add(GetRepository().GetByID(objId);
      break;
      case ObjType.Obj3:
      entity.Obj3s.Add(GetRepository().GetByID(objId);
      break;
      // case/break for each remaining entity type
      }
      }

      "ObjType" is an enumeration of object types. The "GetRepository" method calls "DependencyResolver.Current.GetService>();" and we use Unity for dependency injection. Is there a way to make a more generic "Add" call based on the supplied "ObjType"? Or is this the only concept that can be applied in order to achieve what we're doing?

      djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Map an Action to the enum in a dictionary and execute the appropriate entry when you need to use it.

      1 Reply Last reply
      0
      • M Matt U

        While this project I'm on is MVC, this question isn't specific to MVC, so I felt like this was the right place. Anyhow, we are using the Repository pattern for data access. One particular entity type, e.g. SharedObj, has several other entity types that the SharedObj entity can be tied to. Each is an IEnumerable, like this:

        public class SharedObj
        {
        IEnumerable Obj1s;
        IEnumerable Obj2s;
        IEnumerable Obj3s;
        // More entity collections
        }

        So when we add a child object to one of those collections of SharedObj, we have a method like this:

        // 'entity' is of type 'SharedObj'

        public void AddObj(ObjType objType, int objId)
        {
        switch (objType)
        {
        case ObjType.Obj1:
        entity.Obj1s.Add(GetRepository().GetByID(objId);
        break;
        case ObjType.Obj2:
        entity.Obj2s.Add(GetRepository().GetByID(objId);
        break;
        case ObjType.Obj3:
        entity.Obj3s.Add(GetRepository().GetByID(objId);
        break;
        // case/break for each remaining entity type
        }
        }

        "ObjType" is an enumeration of object types. The "GetRepository" method calls "DependencyResolver.Current.GetService>();" and we use Unity for dependency injection. Is there a way to make a more generic "Add" call based on the supplied "ObjType"? Or is this the only concept that can be applied in order to achieve what we're doing?

        djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Similar to what Pete said; I'd prefer to use a Dictionary<type,List<whatever>> And I think that having AddObj perform GetRepository and GetByID violates the Single Responsibility Principle. By passing in the object, you could probably also leverage generics and simplify things even more.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups