Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a class in its constructor C# - Does it smell?
    primarykey
    data
    text
    <p>Does the code below smell? I'm refactoring some code and have discovered this circular relationship where foo needs a class which needs an interface which foo itself implements.</p> <p>In the real code, foo is a Silverlight UserControl and ifoo has methods to do UI type things like raise a dialog box (eg <code>ShowMessage</code>). The <code>needsAnIfoo</code> class is a (kind of) controller which uses the ifoo interface whenever it want's to do anything with the UI. I have different "themed" UI's which implement iFoo and have the same boiler plate code in their constructor. The <code>needsAnIfoo</code> has various properties which are databound to the UI (so it's kind of a model too.)</p> <p>It compiles and runs fine, but I'm wondering if there's a better way.</p> <p>So, does it smell?</p> <pre><code> interface ifoo { void bar(); } class foo : ifoo { readonly needsAnIfoo _needsAnIfoo; internal foo() { _needsAnIfoo = new needsAnIfoo(this); } #region ifoo Members public void bar() { throw new NotImplementedException(); } #endregion } class needsAnIfoo { readonly ifoo _myfoo; public needsAnIfoo(ifoo foo) { _myfoo = foo; } } static void Main(string[] args) { foo foo = new foo(); } </code></pre> <p>Perhaps I should new up the <code>needsAnIfoo</code> without passing the iFoo in the constructor and then give it the <code>iFoo</code> in an <code>Initialize</code> method. But this looks very odd:</p> <pre><code> foo foo = new foo(); needsAnIfoo needsAnIfoo = new needsAnIfoo(foo); foo.Initialise(needsAnIfoo); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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