views:

95

answers:

2

Hello everyone,

I've been using Sub Sonic 3.x.x.x and i've come across something i need help with. I'm using an abstract class and a factory pattern etc.. and this is basically the problem:

public abstract class Person { }

public class Male : Person { }

public class Female : Person { }

....

how do i get this to work String personType = "male"; Type myType = GetPersonTypeFromFactory(personType);

SimpleRepository rep = new SimpleRepository();

var all = rep.All<...>().ToList();

i can't put rep.All so how can i get this working ??

A: 

The SimpleRepository cannot persist object that are abstract. I can't speak to ActiveRecord however.

I'm sure you know this, but if you did use SimpleRepository and you're looking to get all of one type you could do rep.All<Male>(). This eliminates your Factory and I'm sure the example is a simplified from what you're doing in the real world so this probably isn't what you want.

Rob Sutherland
A: 

It’s worth bearing in mind that de-serialising abstract objects is always a pain in the ass as abstract classes don’t have a default constructor; having said that, there is nothing stopping you de-serialising to a concrete class, and returning and interface/abstract class from your factory.
When you persist/retrieve data to the DB you just do it via your concrete DO object and instantiate your BO object in the factory via a copy constructor and have it implement the same interface as the DO object. But as always with patterns, you’ve got to ask what you’re getting for the code.

To retrieve : DO --> Factory --> BO

To Store : BO --> Façade/Controller --> DO

DO and BO implement IYourObject interface, or derive from YourObjectBase, which has the copy constructor YourObjectBase(YourObjectBase src).

Lots of lines of code though if you don’t have multiple BO types :-)

Matware