Refactor 'switch/case' That Is Based Enum Value
-
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.
-
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.
Map an Action to the enum in a dictionary and execute the appropriate entry when you need to use it.
-
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.
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.