Field from derived class.

Posts   
 
    
jwijnker
User
Posts: 32
Joined: 09-Mar-2011
# Posted on: 07-Apr-2011 14:30:50   

I was wondering is the followinging...

I have a base-class(party) and 2 derived classes(contact, organization) using the TargetPerEntityHierarchy

PARTY(base) + PartyId + PartyType

Contact + Firstname + Surname + ...

Organization + OrganizationName + VAT + ...

Now i want to get 'Name' from Party, which is in case of: - Contact: firstname + " " + surname; - Organisation: OrganizationName;

Reason for this is that i need a list of Customers as Parties with PartyId and PartyName,... So i created a CustomerRepository and get the Customers/Parties mixed as Contacts and Organizations. Now i need to publish the list, and here the problem raises...

So: Party p = new Contact(int); // Until here there nothing special... String s = p.Name; // Should give the correct name.

To do this is was creating a new property Name in the PartyEntity.

public string Name {
 get {
  switch(this.TypeId)
    case /* DISCRIMINATOR-VALUE Contact */ : 
     return Firstname + " " + Surname;
    break;
    case /* DISCRIMINATOR-VALUE Organization */ :
     return OrganizationName;
    break;
    default: // In case of the entity isn't fetched.
     return "";
    break;
  }// (I haven't found an/the DiscriminatorEnum yet, but i'll find it later...)
}}

It seems unnatural to me because the base class has knowledge here of it's derived classes. This shouldn't be a problem, because when a new subclass arrives, there is more work to do wink

But is this the way to handle it? Or is better to create a wrapper...??

Regards.

jwijnker
User
Posts: 32
Joined: 09-Mar-2011
# Posted on: 07-Apr-2011 17:38:53   

I found a solution for this problem.

In the base-class I added a new property 'Name':

public virtual string Name { 
            get { throw new NotImplementedException("BaseClass with DiscriminatorValue: " + this.TypeId + " has no property 'Name' implemented."); } 
        }

Derived classes should override the Name property:

public override string Name
        {
            get { return this.DerivedClassName; }
        }

Unfortunatly this property isn't forced for all derived classes, but by throwing an NotImplementedExecption it's 'sub-forced' to implement the Name property.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 07-Apr-2011 20:23:23   

Exactly what I was going to say - that's the correct solution!

Matt

jwijnker
User
Posts: 32
Joined: 09-Mar-2011
# Posted on: 19-Apr-2011 13:44:55   

I reopend this thread for the following....

I'm using a webgrid to display the "Name" of a PartyEntity. Now because the PartyEntity is either a CustomerEntity or an OrganizationEntity, the Name isn't found in the database when i want to search and/or sort the webGrid.

In case of a CustomerEntity the Name property is the contatenation of Firstname and Surname, in case of an OrganizationEntity the Name property is an alias for the organizationName-property.

Maybe there is a way by refactoring the Name properties so the Llbgen can use this property also 'backwards'?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 19-Apr-2011 21:26:59   

Is there a way to get the grid to use client side sorting, so it uses the values in the actual entities rather than going back to the database ?

Matt

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 20-Apr-2011 09:41:09   

You could add a computed column to the base table which concats the strings in the db, then you can filter on the field.

You can do in-memory sorting btw, on an entityview, which are bindable to controls.

Frans Bouma | Lead developer LLBLGen Pro
jwijnker
User
Posts: 32
Joined: 09-Mar-2011
# Posted on: 16-Aug-2011 17:46:43   

Right now i have the same problem again.

What i did was:

1st - i recreated the database create script so i was able to create a new database on an other server 2nd - i changed the database/catalog name in LlblGen Designer. 3rd - I regenerated the .Net (4.0 c#) code.

No again the message "The given key was not present in the dictionary." raises.

Maybe this can help to find a solution...

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 16-Aug-2011 20:33:27   

jwijnker wrote:

No again the message "The given key was not present in the dictionary." raises.

Hi there. This has nothing to do with the subject of this thread. Please open a new thread for this. That way it may be helpful for other guys searching for answers.
Also, please tell us more about your problem (http://llblgen.com/tinyforum/Messages.aspx?ThreadID=7722).

David Elizondo | LLBLGen Support Team