0

I have a complicated structure of tables and I need some work done extra so I am using partial classes on my entities from EF.

Lets say I have an entity called Person with attributes Name and LastName. Querying over the entity will return me a collection of persons with the name and lastname. Now I would like to have also a property FullName which I create using a partial class of the class person. Since all of the data is in the object I can easily get it.

public partial class Person
{
    public string FullName { get { return Name + " " LastName } }
}

Now things get complicated..... Lets Supose that I have also another Entity (Car) that is connected to Person.

Change the request so the Name of the Car is also a part of the fullName.

public partial class Person
{
    public string FullName { get { return Name + " " LastName + " - " + Car.Name } }
}

My problem is the following: since the EF Context is already disposed after the using I would like to have it executed in runtime while querying and stored inside the object but not inside the database. Something like a custom ToString method.

I need this for GUI purposes. Of course this is a simplified version of it and format string in the gui and query would be much easier, but they are not an option.

I need to find a way to have it automatically returned, or at least have a method automatically executed.

I could achieve this by overriding the constructor, or overloading it maybe, from where I could call a method. I would really prefer not to go through every single object of the query result and invoke the Method from a loop, or creating the context if it does not exist in the getter.

I repeat, this is a simplified version of the problem

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user853710
  • 1,715
  • 1
  • 13
  • 27
  • 1
    You don't specify what you are using to present this data, but generally speaking, you'd probably be better off wrapping your Model data (the EF stuff) in a ViewModel (or some other wrapping class) that has your presentation properties, plus properties and/or methods that update the appropriate properties in the Model layer. – Wonko the Sane Jul 02 '13 at 15:29
  • @ Wonko: That's exactly what sprung to my mind, why not use the ViewModel pattern. http://stackoverflow.com/questions/9326450/in-mvc-what-is-a-viewmodel – Paul Zahra Jul 02 '13 at 15:32
  • don't know it ;), and deadline is tomorrow for the sprint – user853710 Jul 02 '13 at 15:32
  • It's simple enough, just have another class PersonalCars which inherits your other two classes, Person and Car, and has it's own methods, properties etc, thereby extending your entities and more logically seperating them, i.e. Person doesn't care about a Car – Paul Zahra Jul 02 '13 at 15:36
  • @Paul You know that a class can inherrit only from one class? – user853710 Jul 02 '13 at 15:38
  • 1
    In this case, you would probably have a class that either derives from or has an instance of a Person object, with an instance of a Car object. I am not sure what you mean by "is connected to a person." This PersonViewModel class would then have the property FullName that uses the instances of Person and Car to return the string you want (perhaps using String.Format instead of "+"). YOu GUI would then reference this PersonViewModel instead of referencing the Person directly. – Wonko the Sane Jul 02 '13 at 17:39
  • by is connected I mean a foreign key referece in EF through mapping to the DB. – user853710 Jul 03 '13 at 11:55
  • 1
    Quite simply Interfaces are used to simulate multiple inheritance (Until MS pulls their finger out and does as PERL has done for years and implement multiple inheritance) - have a read of http://stackoverflow.com/questions/3199956/how-to-simulate-multiple-inheritance-in-c-sharp – Paul Zahra Aug 07 '13 at 14:39
  • True my friend. That's what I miss the most from my time working with Python – user853710 Aug 08 '13 at 09:09
  • I'm assuming you are using lazy loading, which is why the Car is not available. Make sure you query all the data you need to fill all your properties ([eager loading](https://msdn.microsoft.com/en-us/data/jj574232.aspx)). `From p In database.People.Include("Car") ...` – Sam Axe Aug 04 '15 at 20:11

0 Answers0