I'm having a situation where type
of each business object in my application has to be saved to some table in the database. I would need to represent each type
by some enum
or sorts.
There exists a basic framework in another dll which is independent of my model, and should be. My model classes have to inherit a base class/interface from the external framework first of all. The problem is I cant have the enum
that represents my business objects in the external dll since that should be independent of any model. For instance,
Base class in external dll:
namespace external
{
public enum EnumThatDenotesPoco { Vehicle, Animal, Foo }
public abstract class Framework
{
public abstract EnumThatDenotesPoco RecordType { get; }
}
}
and my project:
namespace ourApplication
{
public class Vehicle : Framework
{
public override EnumThatDenotesPoco RecordType { get { return EnumThatDenotesPoco.Vehicle; } }
}
}
wont work since Vehicle, Animal, Foo
are in my application project. What would be a better design in this case?
I have two ways to go about it, but not sure if that's the right approach.
1.
namespace external
{
public abstract class Framework
{
public abstract Enum RecordType { get; } //base class of all enums
}
}
namespace ourApplication
{
public enum EnumThatDenotesPoco { Vehicle, Animal, Foo }
public class Vehicle : Framework
{
public override Enum RecordType { get { return EnumThatDenotesPoco.Vehicle; } }
}
}
This works. vehicle.RecordType.
rightly yields 0
.
2.
namespace external
{
public class EntityBase // an empty enum class
{
}
public abstract class Framework
{
public abstract EntityBase RecordType { get; }
}
}
namespace ourApplication
{
public sealed class Entity : EntityBase
{
public static readonly Entity Vehicle = 1;
public static readonly Entity Animal = 2;
public static readonly Entity Foo = 3; //etc
int value;
public static implicit operator Entity(int x)
{
return new Entity { value = x };
}
public override string ToString()
{
return value.ToString();
}
}
public class Vehicle : Framework
{
public override EntityBase RecordType { get { return Entity.Vehicle; } }
}
}
Both works.