samedi 20 février 2016

Nhibernate's lazy loading and c# event

Suppose we have a domain model that looks like this:

public delegate void ActivationHandler();

public class Task
{
    public event ActivationHandler OnActivate;
    public IList<Step> Steps { get; private set; }
    public void Activate()
    {
        // do something
        if(OnActivate != null)
            OnActivate();
    }
}

public class Step
{
    private Task _task;
    public Task Task
    {
        get { return _task; }
        set
        {
            _task = value;
            if (_task != null)
                _task.OnActivate += OnTaskActivate;
        }
    }

    private void OnTaskActivate()
    {
        // do something 
    }
}

it's all good and well, except that these classes are mapped with NHibernate and the collection of Steps inside the Task class is lazy loaded by default.

If I *do not * access the Steps collection inside a Task, when I change the Task status the event handler in the Step class is not called, obviously, and this is making me really sad.

First of all I'd like to know if this is the correct approach to handle this kind of interactions between domain objects. [for reasons of why I'm asking this see the "Some background" section]

Secondly, how do I make sure the event handler in the Step class will get correctly attached? The first solution that comes to my mind is to remove the lazy loading from the collection but I may not be able to change this behaviour for political reasons. What other options do I have?

Some background

The logic for the Task activation was in our service layer but that caused a lot of pain with circular references since the TaskServiceLayer had to be injected in the StepServiceLayer and the StepServiceLayer had to be injected in the TaskServiceLayer (of course you may also wonder why we do have a StepServiceLayer in the first place, but). Anyway, I thought that the logic for the activation of a task (as in: change the Status properties and activate the first step) should really be in the domain classes while the service layer job should be to coordinate things like validation / call activation in the domain object / call the repository to save the instances / send notifications etc.

Aucun commentaire:

Enregistrer un commentaire