EntityCollection Question

Posts   
 
    
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 15-Jan-2008 21:45:39   

I have an interface that I put on certain generated entities in my project.

I'd like to create a class that takes in its ctor an EntityCollection that is constrained to types that implement that interface. Is that possible?

(EDIT) I should have been more clear--the fact that it's a ctor really has nothing to do with it. I would just like to able to constrain the EntityCollection to types that implement that interface.

When I try:


        public ImportMapper(EntityCollection<IMyInterface> temp)
        {
            _coreAttributes = coreAttributes;
            _dynamicAttributes = dynamicAttributes;
            _batchId = batchId;
            _siteId = siteId;
        }

I get:

Error 2 The type 'Flex.DAL.Custom.Interface.IMyInterface' must be convertible to 'SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2' in order to use it as parameter 'TEntity' in the generic type or method 'Flex.DAL.HelperClasses.EntityCollection<TEntity>'

Thanks for any insight.

Phil

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Jan-2008 10:01:14   

I think you should derive from EntityCollection<TEntity>. i.e. create your own collection class that accepts the interface type, and which inherit from EntityCollection<TEntity>.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 16-Jan-2008 11:17:06   

I think it can be simpler.



public interface IMyInterface
{
}

public class Foo
{
    public void Bar<T>(EntityCollection<T> col)
        where T : EntityBase2, IEntity2, IMyInterface
    {
    }
}

So all you have to do is make your method generic like the example above with the where clause to restrict usage of it.

Frans Bouma | Lead developer LLBLGen Pro
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 16-Jan-2008 17:30:47   

Thanks guys. That worked.

I'm still confused about why this is not possible to do as a parameter in the ctor of another class. So now I need to instantiate my class and then call an "Initialize" method to properly constrain the collections I want to provide to it.

Though this is a limitation of C# generics (or my understanding of them) and not LLBL.

Thanks again,

Phil