fetch sub-type based on primary key

Posts   
 
    
Posts: 67
Joined: 10-Jun-2009
# Posted on: 16-Jun-2009 13:03:46   

Perhaps a simple question, but I just can't figure it out.

I have a supertype groups with two sub-types: personalgroups and systemgroups. I want to make a general detail screen for both sub-types. On this screen (an UserControl) I have a TextBox which contains the title of the group.

Both sub-types have different columns where the title is saved. Personalgroups have a title which is saved in the column groups.title. Systemgroups haven't got a title because they aren't saved in the database but in the localization. The reference to the key in the localization file is saved in the column groups.ref_title.

Based on the PK of the field, I now want to fill the TextBox for the title. How can I fetch the correct object from the database and get the correct title?

The problem I can't seem to solve is how to get the type of the object and get the proper title to display.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 16-Jun-2009 13:13:54   

Can the Titles/RefTitles be duplicated between the database and the localisation file ?

Can you just try fetching from the database first, and if that returns a null object, fetch from the localisation file instead ?

Matt

Posts: 67
Joined: 10-Jun-2009
# Posted on: 16-Jun-2009 13:18:13   

What I would like to do is first get the proper object type, and then get the title using a different member.

Something like this:


GroupEntity group = new GroupEntity(Id);

if (group is PersonalGroup)
  TextBox.Text = group.Title;
if (group is SystemGroup)
  TextBox.Text = (String)GetGlobalResourceObject("global", group.RefTitle);

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 16-Jun-2009 14:56:25   

Why not encapsulate this as a property or extension method that checks internally which type of entity it is and returns the correct property. This means that your calling code would not have to do the check.

Matt

Posts: 67
Joined: 10-Jun-2009
# Posted on: 16-Jun-2009 15:03:14   

I have tried that using a new function in the groupEntity class. But how would I get the proper type in an instance when I fetch a group from the database?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 16-Jun-2009 16:02:11   
if (typeof(this) == EntityType.PersonalGroup)
{}

?

Posts: 67
Joined: 10-Jun-2009
# Posted on: 16-Jun-2009 16:08:17   

See my previous post. When I try to fetch a groupentity from the database using the constructor, it wil allways create a groupentity, not any sub-type:


GroupEntity group = new GroupEntity(Id);

How do I get an instance of a subtype object in a variable, so I can evaluate it's type.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 16-Jun-2009 16:25:35   

You should be able to cast (or check the type of) the retrieved GroupEntity to one of its sub types successfully...

Posts: 67
Joined: 10-Jun-2009
# Posted on: 17-Jun-2009 09:34:01   

I´ve tried to cast the GroupEntity to a PersonalGroupEntity, but no luck at casting:


GroupEntity group = new GroupEntity(Id);
(PersonalGroupEntity)circle;

this gives me the following error.

Cannot cast 'group' (which has an actual type of 'EntityClasses. GroupEntity') to 'EntityClasses. PersonalGroupEntity'

Also, I've tried to create a new function which should give me the right property:


public partial class GroupEntity
{
    public string GroupTitle()
    {
        if (this is PersonalGroupEntity)
            return "PersonalGroup";
        else 
            return "some other sub-type";
    }
}

However, the test condition will always return false.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 17-Jun-2009 10:13:44   

Fetching a single entity is not polymorphic unless you use FetchPolymorphic() method on the root entity. eg.

var car = GroupEntity.FetchPolymorphic(null, id, null);

ref: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=4557

Or you may fetch an entityCollection specifying the id as a filter, fetching a collection is polymorphic.

Posts: 67
Joined: 10-Jun-2009
# Posted on: 17-Jun-2009 10:23:26   

That was exactly what I was looking for! Thank you very much.