Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design- Pattern to prevent unassigned objects

I often get some resharper warnings regarding "unassigned objects created with 'new' expression". Following code snipped shall demonstrate the situation:

I am using a helper class(Observer.cs) which monitors some properties from an other class (MonitoredClass.cs). When a property changes, the observer class writes the changed value into a other data class (DataClass.cs).

simplified code snipped:

MonitoredClass.cs:

public class MonitoredClass : INotifyPropertyChanged
{
   // simplified: in fact property calls OnPropertyChange(..)
   public string Property1 { get; set; }
}

DataClass.cs:

public class DataClass
{
   public string LastProperty1Value { get; set; }
}

Observer.cs:

public class Observer
{
   private MonitoredClass _monitoredClass;
   private DataClass _dataClass;
   public Observer(MonitoredClass monitoredClass, DataClass dataClass)
   {
      _monitoredClass = monitoredClass;
      _dataClass = dataClass;
      _monitoredClass.PropertyChanged+=MonitoredClassPropertyChanged;
   }

   private void MonitoredClassPropertyChanged(..)
   {
      _dataClass.LastProperty1Value = _monitoredClass.Property1;
   }
}

So far so good.

If I use now my Observer class from above as follows:

...
new Observer(monitoredClassInstance, dataClassInstance);
...

than I get a resharper warning "possible unassigned object created by 'new' expression".

My question now is, if there is a better solution/pattern to design this observer. Of coarse, i can allocate the new observer instance to a private field. But than I have a field which is never used. Or I can set the monitoredClassInstance and dataClassInstance with properties instead of pass them in the constructor. But this only prevents the warning, but in fact does not change the architecture.

Thanks in advance for your advice, opinion, pattern etc.

like image 492
rhe1980 Avatar asked Jan 08 '13 14:01

rhe1980


1 Answers

It may be fine as it is. Of course, it only works because you have attached an event handler, thereby tying the lifetime of the Observer to that of the MonitoredClass. If you had not attached an event handler, then the Observer would have no references to it and it would (eventually) be garbage collected.

Thinking about it, it might therefore be clearer to make the constructor private and write a public static factory method to create it:

public class Observer
{
    private MonitoredClass _monitoredClass;
    private DataClass _dataClass;

    public static void Observe(MonitoredClass monitoredClass, DataClass dataClass)
    {
        new Observer(monitoredClass, dataClass);
    }

    private Observer(MonitoredClass monitoredClass, DataClass dataClass)
    {
        _monitoredClass = monitoredClass;
        _dataClass = dataClass;
        _monitoredClass.PropertyChanged+=MonitoredClassPropertyChanged;
    }

    private void MonitoredClassPropertyChanged(..)
    {
        _dataClass.LastProperty1Value = _monitoredClass.Property1;
    }
}

Then you can suppress the warning inside Observe() and people who call it won't need to worry about it.

like image 124
Matthew Watson Avatar answered Sep 20 '22 21:09

Matthew Watson