Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit</strong></p> <p>I'm glad my answer helped a bit, let me update it slightly further. To use your question as a direct answer, all you would need to change is your field declaration from:</p> <pre><code>Person x = new Person(); </code></pre> <p>to</p> <pre><code>IPerson x = new Person(); </code></pre> <p>Your code-behind now know the properties and methods that are specified in your interface, and is a lot less coupled, as you could swap out <code>new Person()</code> for <code>new Student()</code> later-on. As long as the object implements the interface. <strong>Your code-behind should now work without any necessary changes.</strong></p> <p><em><strong>Side note</em></strong></p> <p>I would recommend considering lazy-loading the <code>x</code> person, and also using a property with a more recognisable name. N.B. this doesn't answer your question, but it's just a suggestion. :)</p> <pre><code>private IPerson _CurrentPerson = null; private IPerson CurrentPerson { get { if (this._CurrentPerson == null) { this._CurrentPerson = new Person(); } return this._CurrentPerson } set { this._CurrentPerson = value; } } </code></pre> <hr> <p>De-coupling is when two, or more, code blocks should not depend on each other. Inversion of Control is when the coupling of objects is bound at runtime, thus allowing for much more flexibility, and therefore less coupling, of objects and their instances. Inversion of control is best use in conjunction with interfaces. The interfaces define that <code>ClassA</code> will do <code>MethodX</code> and have <code>PropertyY</code>. Our main object does not care what object is returned at run-time, as log as it can fulfil an interface, it's happy.</p> <p>In your above example, you will want to interface your person class, maybe something like so:</p> <pre><code>public interface IPerson { string Name { get; set; } string PersonAddText(string text); } public class Person : IPerson { // your code here } </code></pre> <p>Then, within your main method calls, instead of explicitly using a <code>Person</code> object, you will use an instance of an object that implements the interface <code>IPerson</code>. The "hooking" up of the interface and the object can be achieved by various different libraries which will help in setting up your dependencies. In my experience I've used <a href="http://docs.structuremap.net/" rel="nofollow">StructureMap</a> and <a href="http://msdn.microsoft.com/en-us/library/ff648951.aspx" rel="nofollow">Microsoft's Enterprise Library</a>. They can be a bit fiddly to set-up, but once they are, you'll be able to do something like so...</p> <pre><code>public void MainMethod_InInterfaceLayer() { // container is an instance of UnityContainer Person newPerson = container.Resolve&lt;IPerson&gt;(); } </code></pre> <p>I know this ins't a full answer, but hopefully it'll help a bit. :)</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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