This is a migrated thread and some comments may be shown as answers.

[Solved] Can you inherit from an entity class?

2 Answers 99 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
David Ribb
Top achievements
Rank 1
David Ribb asked on 24 Nov 2010, 12:48 PM
I'd like to have the genereated classes as a base class and inherit from them.  This way I can overide the properties to perform tasks needed.  For example when I set the last name or first name I want it to fire the OnPropertyChanged for FullName to force the rebinding in my WPF application.

2 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 25 Nov 2010, 09:20 AM
Hi David,

You don't even need inheritance for the scenario you used as an example.

Since all generated classes are partial classes, you can extent the class by creating a partial class with the same name as the generated one in a new code file and code your extensions there.

Like:
In CustomerEntity.generated.cs:

public partial class CustomerEntity
{
  public string FirstName
  public string LastName
}

and in CustomerEntity.cs:

public partial class CustomerEntity
{
  public string FullName
  {
     get { return FirstName + " " + LastName; }
  }
}

if you want to implement PropertyChanged events you can modify the T4 template that generates the code files for the entities.
Take a look at this resource.

Regards

Henrik
0
Serge
Telerik team
answered on 26 Nov 2010, 04:36 PM
Hello Henrik and hello David,

I would agree that Henrik's suggestion is the way to go here, it is not necessary to inherit from our classes but only rather extend them through partials. If you want to modify the properties that we generate, you could do so by modifying the T4 templates to generate only fields and write your own properties in the partial class. This would mean however writing a lot of properties. 

I will strongly suggest to follow this help article and modify the templates to implement the INotifyPropertyChanged interface. This should be enough to get you started. I have also attached a part of templates that has the generation of properties stripped. Please have a look at the help article as it contains information on how to use custom templates.

 I hope this is helpful.

Regards,
Serge
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Tags
General Discussions
Asked by
David Ribb
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
Serge
Telerik team
Share this question
or