3

I have 2 classes, User and ExtendedUser. ExtendedUser inherits from User and just adds 2 int fields - Nothing fancy.

I have a list of User objects - List<User> and would like to go over the list and create a list of ExtendedUser objects, where I'd like to just fill fill the missing int fields.

I thought about using the Select method of Linq to create the new objects, but it won't copy the User to ExtendedUser - so I'll need to recreate those objects, which doesn't make sense for me.

EDIT:

User is an Entity, where as ExtendedUser is a custom class inheriting from that entity (but not an entity by itself).

It seems the easiest solution here would be composition, though conceptually - inheritance would have worked better.

Any smart solutions here? :)

Thanks!

H H
  • 263,252
  • 30
  • 330
  • 514
Roman
  • 4,443
  • 14
  • 56
  • 81
  • The issue is that User object is created by Entity Framework, which also includes a lot of functionality for this object - So I can't easily copy it. – Roman Nov 20 '15 at 20:21
  • 1
    How about `ExtendedUser`? Is it created by `EntityFramework` too? – DanielS Nov 20 '15 at 20:31
  • When ExtendedUser is an entity (as it should be) this is trivial with `OfType<>`. When you don't want it in the Db, consider composition instead of inheritance. But do provide the extra information. – H H Nov 20 '15 at 20:47
  • @HenkHolterman - ExtendedUser is NOT an entity, just a custom class. I did go with composition for now as I can't find a straight forward enough solution otherwise. – Roman Nov 20 '15 at 21:02
  • Yes inheritance between an entity and a non-entity is pretty strange concept, not easy to deal with. So backtrack how you got to that solution and ask if you really need it. Adding 2 dummy fields (and maybe a discriminator) to the entity might be an alternative. – H H Nov 20 '15 at 21:05
  • And do edit the question with this info. Comments don't count here. – H H Nov 20 '15 at 21:07
  • Easy solution is to simply add your two properties into `User`. Decorate them with `[NotMapped]`. Then use `ObjectMaterialized` event and in it initalize those two properties.Indeed `ExtendedUser` is no longer needed. – CodeNotFound Nov 20 '15 at 21:14
  • Yes, `[Notmapped]` might provide sort of an answer but should there really be 2 types to start with? Iow, does your app also use User objects that are not ExtendedUser? – H H Nov 20 '15 at 21:58

1 Answers1

4

... of Linq to create the new objects, but it won't copy the User to ExtendedUser - so I'll need to recreate those objects, which doesn't make sense for me.

This is not a shortcoming of Linq.

It is a general principal in strongly typed OOP that you cannot simply convert base class objects to derived objects. Such a conversion always involves copying the members and that raises the issue of identity (which clone is the real one?).

In other words, when an object is supposed to be of class B it should never have started life as type A, even if B derives from A. So do revisit your design and overall solution.

When you still think you need this, then what you have is a situation like converting entities to DTOs and vice versa. And you can use the usual tools like AutoMapper.

H H
  • 263,252
  • 30
  • 330
  • 514