Typed collections

Posts   
 
    
nbrake
User
Posts: 30
Joined: 18-May-2008
# Posted on: 18-May-2008 22:45:44   

Hi there,

I am using V2.5, TwoClasses2005WithValidator, March 28th build, on .NET 2.0, running against SQL Server 2005

My actual case

I have a class called MyImportEntity. MyImportEntity maps a db-1:n relationship to a table called report through a collection of called MyReportEntity.

An instance of MyImportEntity, import, has a member called Report, and this member is of the type EntityCollection<ReportEntity>, and not EntityCollection<MyReportEntity>.

My question is: why aren't the subclasses used in defining the collections? At run time, the members of the collection are of the subclass type.

Why am I asking this?

I defined a number of methods using collections of the subclasses. Now I am unable to call such a method with import.report, sind report is EntityCollection<ReportEntity> and not EntityCollection<MyReportEntity>.....

Best regards

brake

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-May-2008 06:52:28   

Hi brake,

If I understand you well, at your DB you have something like:

 MyImport -(1:n)> Report

This is mapped to LLBLGen as:

 MyImportEntity -(1:n)> ReportEntity

Now you create a ReportEntity subclass at code (MyReportEntity). Then you want to use such class instead of the original ReportEntity.

Why not just to extend the ReportEntity adding your own methods and properties?

Anyway, this should be done via casting:

import.Report = someMyReportEntity.

((MyReportEntity) import.Report).MyCustomMethod();
David Elizondo | LLBLGen Support Team
nbrake
User
Posts: 30
Joined: 18-May-2008
# Posted on: 19-May-2008 09:05:07   

Hi Daelmo,

I am already using the 2 classes solution, which means I mostly use the subclasses generated by LLBLGen. In the generated subclasses, the entity collections (that map 1:n relationships) doen't use the subclass, but the original class. In my example, MyImport has members of the type Report (an import consists of a number of reports), instead of MyReport. During runtime MyReport elements are returned, but not while compiling.

This is the code generated


/// <summary> Gets the EntityCollection with the related entities of type 'MBReportEntity' which are related to this entity via a relation of type '1:n'.
/// If the EntityCollection hasn't been fetched yet, the collection returned will be empty.</summary>
[TypeContainedAttribute(typeof(MBReportEntity))]
public virtual EntityCollection<[b]MBReportEntity[/b]> MBReport

import.Report is an EntityCollection of type EntityCollection<MBReportEntity>. It is also not possible to up cast EntityCollection<MBReportEntity> to EntityCollection<MyMBReportEntity>, as vs would not take that.

My question is, why aren't the members the map to 1:n relationships defined as subclasses, something like?

[TypeContainedAttribute(typeof(MBReportEntity))]
public virtual EntityCollection<[b]My[/b]MBReportEntity> MBReport

Best regards

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 19-May-2008 18:34:49   

You're using adapter I think (I think David assumed you're on selfservicing wink ).

The thing is that EntityCollection<SomeEntity> isn't a supertype of EntityCollection<MySomeEntity>, even though MySomeEntity is a subtype of SomeEntity.

This means that if the fields were defined with a different mapping, they created a new hierarchy parallel to the parent one, so instead the collection is kept (as MySomeENtity is storable in a collection of type EntityCollection<SomeEntity>). The factory of the collection is set to the My... variant, and the attribute on it tells the databinding code that the type in the collection is the My variant..

Frans Bouma | Lead developer LLBLGen Pro
nbrake
User
Posts: 30
Joined: 18-May-2008
# Posted on: 20-May-2008 10:01:02   

Of course you are right, I am using adapter, and should have mentioned this!

That was the explaination I needed!

Thank you for the reply! Nasser