Design-Pattern to Prevent Unauthorized Objects

I often get some preliminary warnings regarding "unassigned objects created using the" new "expression." The following code snippet should demonstrate the situation:

I use a helper class ( Observer.cs ) that controls some properties from another class ( MonitoredClass.cs ). When a property changes, the observer class writes the changed value to another data class ( DataClass.cs ).

simplified code:

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 now I use my Observer class as follows:

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

than I get a warning of a stepwise "possible unassigned object created by a" new "expression."

My question now is if there is a better solution / template for developing this observer. From the coarse, I can single out a new instance of the observer in a private field. But I have a field that is never used. Or I can set the properties monitoredClassInstance and dataClassInstance with properties instead of passing them in the constructor. But this only prevents the warning, but does not really change the architecture.

Thank you in advance for your advice, opinions, samples, etc.

+7
source share
2 answers

It may be as it is. Of course, this only works because you bound the event handler, thereby binding the Observer's lifetime to what was observed in MonitoredClass. If you did not bind an event handler, then the Observer will not reference it, and it (in the end) will collect garbage.

Thinking about this, it might be easier 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; } } 

You can then suppress the warning inside Observe (), and the people who call it will not worry about it.

+8
source
 public class Observer { private MonitoredClass _monitoredClass; private DataClass _dataClass; public void Setup(MonitoredClass monitoredClass, DataClass dataClass) { _monitoredClass = monitoredClass; _dataClass = dataClass; _monitoredClass.PropertyChanged+=MonitoredClassPropertyChanged; } private void MonitoredClassPropertyChanged(..) { _dataClass.LastProperty1Value = _monitoredClass.Property1; } } 

and

 Observer o = new Observer(); o.Setup( foo, bar ); 

This will not only prevent the warning, but also give you the opportunity to implement other observer methods, such as

  public void Close() { _monitoredClass.PropertyChanged-=MonitoredClassPropertyChanged; } 

if you want to explicitly ban subscription to subscription.

+2
source

All Articles