Subsonic and sub tables

subsonic

I love NHibernate's ability to have one table to store multiple types based on a discriminator. When I fetch an RegularItem, it will come back as the sub type of the discriminator is correct.

Does SubSonic have this ability?

Best Answer

Do you mean, you have a table with different values stored in it and, dependend on a value you want to return different objects?

e.g. you have a table pet

id     type     name
---------------------
1      dog      bello  
2      cat      scott  
3      cat      tiger  

and you want to get dog and cat objects from it?
I have a similar case, and I solved it by creating a Dog class and a Cat class that both inherit from subsonic's autogenerated pet class and implement my IPet interface stub, in conjunction with a factory method, where I cast my objects to the new Type:

public Class Dog : Pet, IPet { }
public Class Cat : Pet, IPet { }
public Interface IPet { }

public static IPet GetAllPets()
{

   List<IPet> pets = new List<IPet>();

   foreach Pet pet in PetCollection.FetchAll()
   {

       IPet newpet;

       if (pet.Type == "dog")
           newpet = new Dog();
       else if (pet.Type == "cat")
           newpet = new Cat();
       else throw new InvalidOperationException("Unknown pet type " + pet.Type);

       pet.CopyTo(newpet);
       newpet.MarkOld();

       pets.Add(newpet);

   }

}

Typed from memory, not guaranteed to compile. But the theory should be clear.

Related Topic