Using EntityCollection<IEntity2> as a method parameter

Posts   
 
    
clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 22-Apr-2009 19:03:24   

LLBLGen Pro version + buildnr = Version 2.0.0.0 Final (November 6th, 2006) Template = Adapter .NET version = 2.0

This is probably really just a C# question about generics and interfaces, but I thought I'd ask here anyways.

The EntityCollection class that is generated looks like this:


public partial class EntityCollection<TEntity> : EntityCollectionBase2<TEntity>
        where TEntity : EntityBase2, IEntity2

So it looks like the generic EntityCollection is constrained to types that inherit from EntityBase2 and implement interface IEntity2.

So I can make variable declarations like this:


EntityCollection<IEntity2> collection;
EntityCollection<EntityBase2> collection2;

So how come I can't declare method parameters that way. In the code below, Method1 won't compile, but Method2 will.


public void Method1( EntityCollection<IEntity2> collection)
{
    // do something
}

public void Method2( EntityCollection<EntityBase2> collection)
{
    // do something
}

The compilation error message for Method1 is:

The type 'SD.LLBLGen.Pro.ORMSupportClasses.IEntity2' must be convertible to 'SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2' in order to use it as parameter 'TEntity' in the generic type or method 'GCS.Data.HelperClasses.EntityCollection<TEntity>'

clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 22-Apr-2009 19:41:21   

clint wrote:

The EntityCollection class that is generated looks like this:


public partial class EntityCollection<TEntity> : EntityCollectionBase2<TEntity>
        where TEntity : EntityBase2, IEntity2

So it looks like the generic EntityCollection is constrained to types that inherit from EntityBase2 and implement interface IEntity2.

So I can make variable declarations like this:


EntityCollection<IEntity2> collection;
EntityCollection<EntityBase2> collection2;

Oops! I'm mistaken. The following code won't compile. But I swear it did a minute ago!


EntityCollection<IEntity2> collection;

Based on that, I guess you must use a type that inherits from EntityBase2. You can't simply use a type that implements IEntity2.

This raises another question. How come EntityCollection says the type must implement IEntity2? Isn't that redundant since the type will be inheriting from EntityBase2 which already implements IEntity2? When you inherit from a type that implements a certain interface, doesn't the derived class automatically have that interface too? I guess that's another C# question.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Apr-2009 06:53:16   

This raises another question. How come EntityCollection says the type must implement IEntity2? Isn't that redundant since the type will be inheriting from EntityBase2 which already implements IEntity2? When you inherit from a type that implements a certain interface, doesn't the derived class automatically have that interface too? I guess that's another C# question.

The both IEntity and EntityBase2 in the 'where' (generic type constraints) clause are needed. Say you create some class that implements IEntity2, you can't simply pass it to a EntityCollection as you need to inherit from EntityBase2 to do so.

The IEntity2 interface defines typed basic behavior. So, depending on what are you doing, could be more standard and practical to pass the object as IEntity2.

For more info about the 'where' clause, read this (MSDN docs).

David Elizondo | LLBLGen Support Team