tags:

views:

110

answers:

2

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?

+1  A: 

The short answer is no, SubSonic does not have this feature built-in. You might be able to sort of recreate that with ExecuteTypedList<>, but it would be a lot of manual work (you'd probably be rewriting most of the functionality of the NH feature).

John Sheehan
A: 

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.

SchlaWiener
I changed the Example a little, because it won't work (you can cast a dog as a pet, but not a pet to a dog).
SchlaWiener