I am using lazy loading to load information about the support ticket which a ticket message belongs to.
TicketMessage is the child of Ticket. In my TicketMessage class I have the following code to lazy load the Ticket object when the Ticket property of the TicketMessage object is referred to:
// Lazy loading of Ticket object
private Ticket _Ticket { get; set; }
public Ticket Ticket
{
get
{
return this._Ticket ?? (this._Ticket = new Ticket(TicketID, ClientID, ConnectionString, Person.PersonID));
}
}
However I am experiencing behaviour that indicates that this object is being every time the TicketMessage class is instantiated, thus resulting in some unwanted null reference exceptions. Is there anything blatantly wrong with my lazy loading pattern?
I would say your NRE problem is more likely to be
Personbeing null rather than an issue with thenull-coalescingoperation.That being said, I don’t particularly like the idea of doing an assignment in that way. From a readability point of view it would be much clearer written as: