EntityCollections in Adapter TwoClass

Posts   
 
    
Bruce
User
Posts: 61
Joined: 18-May-2006
# Posted on: 09-Jul-2008 23:43:02   

Hello

I'm using version 2.5 and Adapter.TwoClasses2005.

I see that the collections belonging to an entity use the Entity Class rather than the MyEntity Class.

This means that the following gives an error:

private EntityCollection< MyOrderEntity > orderColl;
            
orderColl = customerEnt.Order;

I tried casting like this:

orderColl = (EntityCollection< MyOrderEntity >)mRecEnt.RecWorkPkg;

but that also does not compile.

Is there a way to do this?

Another issue is that the following does not compile:

private EntityCollection ordColl;
ordColl.EntityFactoryToUse = new OrderEntityFactory();
ordColl = customerEnt.Order;

The first two code lines are what is generated when I drag the general EntityCollection onto a form and then set the Entity factory, so that I can use it to set up a grid.

Is there a way to populate this order collection from customerEnt.Order or is it necessary to create another collection, populate it and set the grid's datasource to the new collection?

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 11-Jul-2008 11:05:59   

You should use the following code:

private EntityCollection< OrderEntity > orderColl;          
orderColl = customerEnt.Order;

Taking into consideration that customerEnt.Order will contain entities of type MyOrderEntity, since it uses MyOrderEntityFactory.

If you check the MyCustomerEntity you find the following:


public override EntityCollection<OrderEntity> Order
{
    get
    {
        EntityCollection<OrderEntity> toReturn = base.Order;
        toReturn.EntityFactoryToUse = new MyOrderEntityFactory();
        return toReturn;
    }
}

private EntityCollection ordColl; ordColl.EntityFactoryToUse = new OrderEntityFactory(); ordColl = customerEnt.Order;

The first two code lines are what is generated when I drag the general EntityCollection onto a form and then set the Entity factory, so that I can use it to set up a grid.

The ordColl is not a Generic collection, while the customerEnt.Order is a generic collection. the nongeneric EntityCollection type in the helperclasses is really EntityCollection<EntityBase>, and it can't be cast from EntityCollection<SomeEntity>, as that would mean covariance which isn't supported by C# and VB.NET

Is there a way to populate this order collection from customerEnt.Order or is it necessary to create another collection, populate it and set the grid's datasource to the new collection?

Either you directly use the customerEnt.Order, e.g. in databinding you can directly bind the customerEnt.Order to a dataGridView or to a bindingSource. Or else fetch the ordColl, and don't assign customerEnt.Order.

Bruce
User
Posts: 61
Joined: 18-May-2006
# Posted on: 11-Jul-2008 12:29:33   

Hi Wallaa

Thanks for that info. (This is my first time of using Adapter after several years of using Self-Servicing)

First part: Am I right in thinking that this means that one generally does not reference the MyEntities in the top tiers?

Second part: OK you have answered my question - its not possible to assign a non-generic to a generic. I was trying to do it this way because I am passing the fetched collection from one form to another. No problem - I'll just use the non-generic collection for setting up the grid in the designer and re-set the grid's datasource at run-time.

Regards Bruce

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Jul-2008 20:41:51   

First part: Am I right in thinking that this means that one generally does not reference the MyEntities in the top tiers?

Yes, MyEntities is intended for customization and business logic.

David Elizondo | LLBLGen Support Team