0

Using EF with Winforms in C#. I’d like to add full custom properties to our entities, using partial classes. All entities already have partial classes with validation stuff and some more so I’d just add the properties that I need. By full property I mean property with getter and setter so not just a computed/readonly property. I want to this mostly to get around working directly with some DB mapped properties which are badly designed or have other problems. For example, one case would be like this:

// entity class, generated
public partial class Customer
{
  public string Spot {get;set}
}

// partial class, manually changed
public partial class Customer
{
  public int? xxxSpot 
          { get   {  return Int32.Parse(Spot.Trim()); }  // some code omitted 
          { set   { Spot = value.ToString().PadLeft(5); }
}

So my custom properties will be built around existing, DB mapped properties of the entity. I’d like to use these custom properties like normal ones, ie to bind them to UI controls and so on. I’ve tried one and so far it works great. Is this a good idea? If not, why ? And what else should I consider when doing this?

user628661
  • 81
  • 1
  • 1
  • 9

1 Answers1

1

You have answered your own question - it works and there is no reason why to not do that. If you want to improve design of your entities you can even try to change visibility of your mapped properties to ensure that other classes must use only your custom properties with additional logic.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670