I've been thinking about creating custom types for identifiers like this:
public enum CustomerId : int { /* intentionally empty */ }
public enum OrderId : int { }
public enum ProductId : int { }
My primary motivation for this is to prevent the kind of bug where you accidentally pass an orderItemId to a function that was expecting an orderItemDetailId.
It seems that enums work seamlessly with everything I would want to use in a typical .NET web application:
- MVC routing works fine
- JSON serialization works fine
- Every ORM I can think of works fine
So now I am wondering, "why shouldn't I do this?" These are the only drawbacks I can think of:
- It may confuse other developers
- It introduces inconsistency in your system if you have any non-integral identifiers.
- It may require extra casting, like
(CustomerId)42
. But I don't think this will be an issue, since the ORM and MVC routing will typically be handing you values of the enum type directly.
So my question is, what am I missing? This is probably a bad idea, but why?