Generic Method to Return List<T>

Posts   
 
    
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 08-Feb-2006 22:58:34   

I am tying to create a base class for a business layer using generics in c# 2.0. I want to have a method like:

GetList<T>(RelationPredicateBucket filter, SortExpression sorter, PrefetchPath2 prefetchPath)

That way from my businessLayer I could call:

List<MyEntity> myEntity = GetList<MyEntity>(filer, sorter, prefechPath)

and get back a strongly typed generic list instead of an EntityCollection. Here is what I have so far:


        public List<T> GetList<T>(RelationPredicateBucket filter, SortExpression sorter, PrefetchPath2 prefetchPath) where T : ??EntityBase2?? //(is EntityBase2 right here?  I know I need some constraint)
        {
            List<T> toReturn = new List<T>();
            EntityCollection items = new EntityCollection(?new T'sFactory()?);//(how do I get correct EntityFactory from the type)


            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(items, filter, 0, sorter, prefetchPath);
            }

            foreach (T item in items)
            {
                toReturn.Add(item);
            }

            return toReturn;
        }

As you can see I have two questions. Is EntityBase2 the correct constraint? How do I get the Proper EntityFactory from the type T?

Thanks in advance.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 09-Feb-2006 08:34:42   

You run into the same problem I had when refactoring the code for v2.

EntityBase2 is a proper candidate, you can also opt for IEntity2 and 'class'. I also think you should pass in either the factory to the method as an instance, or pass it as a type to the generic method.

Frans Bouma | Lead developer LLBLGen Pro
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 09-Feb-2006 15:56:56   

So there is no staic method GetFactory(EntityBase2) or EntityBase2.GetMyFactory()?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 09-Feb-2006 16:15:03   

No, there's not, because you can use your own factories if you want to.

Frans Bouma | Lead developer LLBLGen Pro
pilotboba
User
Posts: 434
Joined: 05-Aug-2005
# Posted on: 09-Feb-2006 16:36:54   

Sam wrote:

I am tying to create a base class for a business layer using generics in c# 2.0. I want to have a method like:

GetList<T>(RelationPredicateBucket filter, SortExpression sorter, PrefetchPath2 prefetchPath)

Unfortunatly there is no way to instantiate an entity type / collection in llblgen just from knowing the type.

We tried to do this ourself also.

There's no way to do something like this in LLBLGen...

IList<MyEntity> stuff = adp.FetchEntityCollection(typeof(MyEntity), exp)

or

IList<MyEntity> stuff = adp.FetchEntityCollection(MyEntity.GetType(), exp)

So, it is very hard to create a single generic class to do this. What I think you will have to do is create a concrete methods in each class that return a generic collection of type whatever the entity is.

BOb

Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 09-Feb-2006 17:41:27   

Thanks for the input all. I think that I will probably just pass the Factory in as a parameter of the method. It will be great when LLBLGen 2 supports generics out of the box. One question regarding that: Is your EntityCollection<T> Going to derive from List<T> or one of the other generic collection classes? I love the richness of List<T> (FindAll(), FindFisrt()) and such but I have also heard there is a lot of overhead there? What are you current plans?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 10-Feb-2006 08:05:19   

Sam wrote:

Thanks for the input all. I think that I will probably just pass the Factory in as a parameter of the method. It will be great when LLBLGen 2 supports generics out of the box. One question regarding that: Is your EntityCollection<T> Going to derive from List<T> or one of the other generic collection classes? I love the richness of List<T> (FindAll(), FindFisrt()) and such but I have also heard there is a lot of overhead there? What are you current plans?

The build in collections werent that great for base classes, because they didn't have virtual methods for add etc.

So I build my own CollectionCore<T> class which implements the interfaces and aggregates a List<T>. This also to prevent users to use List<T> methods which I didn't want to provide on the entitycollection level. Most code is in the CollectionCore<T>. Then I created 2 derived classes, EntityCollectionBase<T> and EntityCollectionBase2<T>, and selfservicing has its _entityName_Collections derive from EntityCollectionBase<T> and adapter can use EntityCollection<T> which derives from EntityCollectionBase2<T> or the class EntityCollection which is just a derived class of EntityCollection<EntityBase2>

Selfservicing will keep the entitynamed collections because of the nature of selfservicing (entity specific code in the collections) and because of backwards compatibility.

The find stuff is implemented in the CollectionCore<T> using predicate expression using find methods, or you can use the EntityView object which is new and which offers a rich filter/sorting and projection functionality on the data in the collection, which is prefered so you keep the original collection.

Frans Bouma | Lead developer LLBLGen Pro