mercredi 11 mai 2016

What is a good design pattern in C# to instantiate referenced objects of different subclasses?

I have a manager that read objects from a repository. The objects reference other objects of a known base class, based on a stored enumeration property.

What's a good design pattern to know which manager/repository to query on the reference object? A switch block over the stored kind of the object would get out of hand very quickly.

For example:

public class Person
{
    public Animal Pet { get; set; }
    public AnimalKind PetKind { get; set; }
}

public enum AnimalKind
{
    Dog,
    Cat
}

public abstract class Animal
{

}

public class Dog : Animal
{

}

public class Cat : Animal
{

}

public class DogManager /**/
public class CatManager /**/

public class PersonManager
{
    public IList<Person> GetPersons()
    {
        var persons = Repository.GetPersons();
        // Pattern to instantiate person.Pet with the proper class???
        // The Repository is very lightweight and does not load references.
    }
}

Could someone point me to the right direction? Any recomended book?

Edit: There is a table in the database for each type of Animal.

Aucun commentaire:

Enregistrer un commentaire