I'm seeing strange behavior with initialization of private members in an entity partial class. Check out the comments in the code below:
using System;
using System.Linq;
using IdeaBlade.EntityModel;
using IdeaBlade.Core;
namespace iCatalyst.DomainModel
{
public partial class Contact : IdeaBlade.EntityModel.Entity
{
public static Contact Create()
{
return Create(DomainModelEntityManager.DefaultManager);
}
public static Contact Create(EntityManager manager)
{
Contact contact = manager.CreateEntity<Contact>();
contact.ContactId = Guid.NewGuid();
// At this point, contact._businessPhone is null.
contact.EntityAspect.AddToManager();
// After the call to AddToManager(), contact._businessPhone has a value.
// However this *only* happens if the BusinessPhone property includes a comparison check for null against _businessPhone!
// See BusinessPhone property code below.
contact.Person = Person.Create();
return contact;
}
private Phone _businessPhone;
public Phone BusinessPhone
{
get
{
//if (_businessPhone.EntityAspect.IsNullEntity) // this throws a null reference exception since _businessPhone is null
if (null == _businessPhone || _businessPhone.EntityAspect.IsNullEntity) // with just this change, _businessPhone is never null, and _businessPhone.EntityAspect.IsNullEntity is always false!
{
_businessPhone = Phones.Where(p => p.PhoneType.Name == PhoneTypeEnum.StandardPhoneNames.Business).FirstOrDefault();
if (null == _businessPhone)
{
_businessPhone = Phone.Create(PhoneTypeEnum.StandardPhoneNames.Business);
Phones.Add(_businessPhone);
}
}
return _businessPhone;
}
}
}
}
|