I'm still having trouble getting my head round Generics particularly when using the CSLA, I could remove the generic element from structure altogether but I don't want to lose the strong typing that the CSLA provides through the Save() method etc.
I have an abstract base class...
public abstract class AbstractBase<T> : BusinessBase<T> where T : Base<T>
There is a chain of inheritance that looks like this
Now since I don't know the type of object until I hit a database I have created an object factory which is basically a CommandBase object that hits the DB determines the type object and passes the datareader to the relevant concrete class which then creates the object. These object can all be maniulated as type AbstractBase so the concrete type doesn't really matter
The factory returns the item of type object since I could return it as type AbstractBase<T> since I didn't know the type T until the factory has done it's work
Ideally I would have been a static method on AbstractBase like...
public static AbstractBase<T> GetAbstractBase(int Id)
{
AbstractFactory factory;
factory = DataPortal.Execute<AbstractFactory >(new AbstractFactory(Id));
return (AbstractBase<T>)factory.ConcreteObject;
}
but again I wouldn't know the value of T so wouldn't be able to call the method. So I could have the Factory itself return the object but I can't convert it to AbstractBase because I don't know T. Well I do really becuase I can get the type of objct returned by the factory but this won't compile..
AbstractFactory factory;
factory = DataPortal.Execute<AbstractFactory >(new AbstractFactory(Id));
return
(AbstractBase<factory.ConcreteObject.GetType()>)factory.ConcreteObject;
Surely there is a better way of doing this I just don't know what it is! Can anyone assist?
Thanks for the reply
Without generics it become easy I could just say
ObjectBase object = ObjectFactory.GetObject(id);
I don't need to know anything about ObjectBase before making the call whereas the Generic method would have to be
ObjectBase<T> object = ObjectFactory.GetObject(id);
At which point I don't know the type of T. This essentially means that what I'm trying to achieve won't work. Since the caller doesn't need to know T and I don't really want them to if they need to convert to the concrete type then that was up to the caller.
I would like to avoid having pass the type into the method/factory as this would over complicate the method call and would require type knowledge and I won't always necessarily be creating the object from like you say a readonly list.
I had thought about the Interface approach but then dismissed it for some reason but what you've said makes sense.
Thanks for that.
Yeah unfortunately it doesn't take any methods from the base class (such as BusinessBase.Save()) but it's not a massive job to add them in.
Cheers for your advice on this.....and interface has proven successful :-)
I want access to Save() through the interface but I'm not quite sure yet.
Copyright (c) Marimer LLC