Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle object configuration with Spring.NET
    text
    copied!<p>I'm using Spring.NET to wire up a medium-complexity WPF application. One of the objects represents the configuration settings of the environment the application runs on. The class, named <code>MachineData</code>, is basically a cache for values stored across the host machine: it checks whether a specific MSSql instance is installed and gets some values from there, it checks whether another specific program is installed and gets some of the app.config settings from there, etc. These values are required by many other objects within the application, but only need to be retrieved once, at application startup.</p> <p>I have the <code>MachineData</code> object and all objects using it defined as follows for Spring.NET:</p> <pre><code>&lt;object id="MachineData" type="IMachineData, SomeProject" singleton="true"/&gt; &lt;object id="SomeOtherObject"&gt; &lt;constructor-arg name="data" ref="MachineData"/&gt; &lt;/object&gt; &lt;!-- A bunch of other objects w/ dependencies --&gt; &lt;object id="MainApp" type="MainApplication"&gt; &lt;property name="OtherObject" ref="SomeOtherObject"/&gt; &lt;!-- and so on --&gt; &lt;/object&gt; </code></pre> <p>In <code>MachineData.cs</code>, I have:</p> <pre><code>public MachineData() { Init(); } private void Init() { // Query database for settings, check app settings of other applications } </code></pre> <p>And in <code>App.xaml.cs</code>:</p> <pre><code>var ctx = ContextRegistry.GetContext(); MainApplication mainApp = (MainApplication)ctx.GetObject("MainApp"); </code></pre> <p>Which works perfectly fine, but would it be preferable to do the following:</p> <pre><code>// in MachineData.cs public MachineData() { } public void Init() { // Query database for settings, check app settings of other applications } // in App.xaml.cs var ctx = ContextRegistry.GetContext(); MainApplication mainApp = (MainApplication)ctx.GetObject("MainApp"); var data = (IMachineData)ctx.GetObject("MachineData"); data.Init(); </code></pre> <p>Both would have the same desired effect, however the first option introduces more points of failure into the <code>ContextRegistry.GetContext()</code> call, while the second one requires exposing <code>Init()</code> to the <code>IMachineData</code> interface. Where should this initialization logic be handled?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload