New Posts New Posts RSS Feed: Private member initialization?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Private member initialization?

 Post Reply Post Reply
Author
chuckc View Drop Down
Groupie
Groupie


Joined: 27-Feb-2010
Posts: 54
Post Options Post Options   Quote chuckc Quote  Post ReplyReply Direct Link To This Post Topic: Private member initialization?
    Posted: 07-Apr-2010 at 12:14pm
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;
            }
        }

    }

}
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down