public class DTOa
{
public int Id { get; set;}
public string FirstName { get; set;}
}
public class DTOb: DTOa
{
public string Email { get; set;}
public string Password { get; set; }
}
public class a
{
protected DTOa _DTO = new DTOa();
public int Id
{
get
{
return _DTO.Id;
}
set
{
_DTO.Id = value;
}
}
public string FirstName
{
get
{
return _DTO.FirstName;
}
set
{
_DTO.FirstName = value;
}
}
public DTOa ToValueObject()
{
return _DTO;
}
}
public class b: a
{
protected DTOb _DTO = new DTOb();
public string Email
{
get
{
return _DTO.Email;
}
set
{
_DTO.Email = value;
}
}
public string Password
{
get
{
return _DTO.Password;
}
set
{
_DTO.Password = value;
}
}
public DTOb ToValueObject()
{
return _DTO;
}
}
now let's execute the following code
public function test()
{
var a = new a();
var b = new b();
b.Id = 100;
b.FirstName = "Jim";
b.Email = "[email protected]";
b.Password = "test";
Console.WriteLine(b.ToValueObject().Dump());
}
the problem is that
I expect b.ToValueObject have all properties set, but in reality only get properties from DTOb class (so I FirstName and Id properties are NULL, however I set the explicitly)
dump:
{ Email: [email protected], Password: test, Id: 0 }
Any ideas why ID is not set and FirstName is not set? DTOb is inherited from DTOa and thus "Should" include all the properties from DTOa. It's working on the code level, so if I write console.WriteLine(b.Firstname) - I'll get the value correct, but when I call ToValueObject() method - it got deleted.
OKay here is working example:
public class DTOa : IDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
}
public class DTOb : DTOa, IDTO
{
public string Email { get; set; }
public string Password { get; set; }
}
public class a
{
protected IDTO _DTO;
public a()
{
_DTO = new DTOa();
}
public int Id
{
get
{
return (_DTO as DTOa).Id;
}
set
{
(_DTO as DTOa).Id = value;
}
}
public string FirstName
{
get
{
return (_DTO as DTOa).FirstName;
}
set
{
(_DTO as DTOa).FirstName = value;
}
}
public DTOa ToValueObject()
{
return (_DTO as DTOa);
}
}
public class b : a
{
public b()
{
_DTO = new DTOb();
}
public string Email
{
get
{
return (_DTO as DTOb).Email;
}
set
{
(_DTO as DTOb).Email = value;
}
}
public string Password
{
get
{
return (_DTO as DTOb).Password;
}
set
{
(_DTO as DTOb).Password = value;
}
}
public DTOb ToValueObject()
{
return _DTO as DTOb;
}
}