1

IE: A model called Equipment responsible for storing information about equipments, and its status.

My question is, what is the efficient way to store an item's status. My approach is creating different model called EquipmentStatus. But when it comes to write the code, you need to write code like below :

if(equiment.equipmentstatusid == 2) 

I think it is not good approach. What is the best way to do this ?

umki
  • 769
  • 13
  • 31
  • I don't really follow. What's wrong with having your `Equipment` Model contain an `EquipmentStatus` model? – Jonesopolis Mar 24 '14 at 12:27
  • 1
    You probably want to be using an Enum. – Calum Mar 24 '14 at 12:29
  • What does EquipmentStatus store? – Nilesh Mar 24 '14 at 12:30
  • @Jonesy If i use EquipmentStatus table, i need to handle status depends on ID. Ie. 5-Closed 6-Opened status in one of my customer. But for other customer it can be changed, so i need to change all of the codes. – umki Mar 24 '14 at 12:31
  • @Nilesh EquipmentStatus stores only ID and StatusName – umki Mar 24 '14 at 12:32
  • 1
    Enum can be usefull. @Calum – umki Mar 24 '14 at 12:32
  • Yep, then as @Calum said Enum would be always useful. You can write a DB driven enum and then use `Switch Case` based on the enum value. – Nilesh Mar 24 '14 at 12:34
  • 1
    You can use `Enum.Parse` as `int i = 2; var val = (Status) Enum.Parse(typeof(Status), i.ToString());` And then use `Switch(val)` – Nilesh Mar 24 '14 at 12:48

1 Answers1

1

There is nothing wrong in your code but if you want to do it in efficient way, use can enum.

Also see this great post. When to use Enum?

public enum Status
{
    Success = 1,
    Failed = 2,
    Default = 3
}

if(equiment.equipmentstatusid == (int)Status.Success)
{
 //something
}
Community
  • 1
  • 1
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56