I have a code structure like below.. I know this example is bit weird, but the entities I am working on are similar in structure.
interface IAnimal
{
string name;
int Age;
string AnimalType;
ICollection<Dependent> Dependents;
}
interface IDependent : IAnimal
{
string RelationshipToParent;
}
class Dependent : IDependent
{
public string name;
public int Age;
public string AnimalType;
public RelationshipToParent;
public ICollection<Dependent> Dependents;
}
class Animal : IAnimal
{
public string name;
public int Age;
public ICollection<Dependent> Dependents;
}
class Tiger : Animal
{
public string name;
public int Age;
public ICollection<Dependent> Dependents;
}
class Dog : Animal
{
public string name;
public int Age;
public ICollection<Dependent> Dependents;
}
class DogDependent : Dependent
{
public string name;
public int Age;
public string AnimalType;
public RelationshipToParent;
public ICollection<Dependent> Dependents;
}
class TigerDependent : Dependent
{
public string name;
public int Age;
public string AnimalType;
public RelationshipToParent;
public ICollection<Relative> Dependents;
}
Animal can have dependents as any other animal(eg..Tiger can have dependents as Dog and Elephant) Animal and Dependent classes are the persistent entities in SQL server(using Entity Framework). My problem is I have approx 20 such concrete Animals.. After reading the entity from DB, I need a sophisticated way to convert Animal to concrete types like Tiger,Dog,etc.. based on AnimalType property.
example.. If AnimalType of Animal oobject is "Tiger" then Animal should be converted into Tiger type(with all the properties). Same goes with dependents as well, If Dependent of that Animal type is Dog, then Dependent should be converted into DogDependent.
Is there a better way to do this rather than writing a mapper for each type?
Aucun commentaire:
Enregistrer un commentaire