Ok, we need the following class:
public class User
{
public int Id; // Identifier
public User UserCreated; // Reference to User object - user who created this User object
public User UserUpdated; // Reference to User object - user who last updated this User object
// Now instantiation
// Variant 1 - bad because UserCreated and UserUpdated are both nulls on instantiaton, and getting User from DbContext requires objects to be not null
public User()
{
// any other init but UserCreated / UserUpdated
}
// Variant 2
public User() - bad because of apparent flaw - endless recursion , so program will run out of memory and will crash
{
UserCreated = new User();
UserUpdated = new User();
}
// Variant 3 - how I did it. We postpone creation of references until they are actually used
// add internal variables
internal User _userCreated;
internal User _userUpdated;
// Properties and Accessors
public User UserCreated
{
get
{
return _userCreated;
}
set
{
if ( _userCreated == null )
{
_userCreated = new User();
}
_userCreated = value;
}
}
public User UserUpdated
{
get
{
return _userUpdated;
}
set
{
if ( _userUpdated == null )
{
_userUpdated = new User();
}
_userUpdated = value;
}
}
public User() // like variant 1
{
// any other init but UserCreated / UserUpdated
}
}
Thursday, November 28, 2013
Monday, January 28, 2013
Kobo Vox died just after one year
Kobo Vox coviniently died just after one year - exactly one month after manufacturer warranty is expired.
Subscribe to:
Posts (Atom)