0

How do I map a null to an object using valueinjecter? It throws an error when I do this.

Do I need to use something else other than InjectFrom()?

more info

I have a repository that returns an object domain. I then want to map this domain to a viewmodel. The repository will return null if the object doesn't exist. I cannot map a null object to the viewmodel. How do I accomplish this?

Omu
  • 69,856
  • 92
  • 277
  • 407
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
  • could you explain what are you trying to achieve, source.InjectFrom(null), are you trying to set all properties to null or default values or ... ??? – Omu Mar 14 '11 at 17:36
  • @omu edited, I want to assign the whole source object to null if the parameter is null. – Shawn Mclean Mar 14 '11 at 22:14

1 Answers1

0

for your scenario usually I would throw an error that the user tries to edit an entity that doesn't exist, but anyway, you could do it like this:

var o = repo.get(7);
var vm = new ViewModel();

if (o != null) vm.InjectFrom(o);

or

vm.InjectFrom(o ?? new Entity());
Omu
  • 69,856
  • 92
  • 277
  • 407