{"id":3989,"date":"2014-03-30T06:59:48","date_gmt":"2014-03-30T06:59:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/converting-to-ioc-how-to-handle-inner-objects-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:59:48","modified_gmt":"2014-03-30T06:59:48","slug":"converting-to-ioc-how-to-handle-inner-objects-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/converting-to-ioc-how-to-handle-inner-objects-collection-of-common-programming-errors\/","title":{"rendered":"Converting to IoC, how to handle inner objects-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve looked through a number of threads here (perhaps I&#8217;m using the wrong terminology), but I&#8217;m converting a Web Application over to use IoC via NInject. My other IoC projects were fairly small so it wasn&#8217;t a big issue, but what is the recommended practice for the following situation&#8230;<\/p>\n<p>My object&#8217;s structure looks like this&#8230;<\/p>\n<pre><code>public class Address\n{\n    public virtual string Street { get; set; }\n    public virtual City City { get; set; }\n}\n<\/code><\/pre>\n<p>City being it&#8217;s own object&#8230;<\/p>\n<pre><code>public class City\n{\n    public virtual string Name { get; set; }\n}\n<\/code><\/pre>\n<p>Now I have been handling the inevitable &#8220;Object is null&#8221; exception as follows&#8230;<\/p>\n<pre><code>public void DoThing()\n{\n    new Address address();\n    address.Street = _street;\n    address.City = new City();\n    address.City.Name = _cityName;\n    DoOtherThing(address);\n}\n<\/code><\/pre>\n<p>This was fine for the smaller projects&#8230; but now that we&#8217;re talking something bigger it becomes a real chore. (Add another object to my object, it could mean I need to do this in over 100 places, assuming I find them all)<\/p>\n<p>Prior to IoC this was handled easily by doing stuff like this&#8230;<\/p>\n<pre><code>public class Address\n{\n    private string street;\n    private City city;\n\n    public Address()\n    {\n        street = string.Empty;\n        City = new City();\n    }\n\n    public string Street \n    { \n        get { return street; } \n        set { street = value; }\n    }\n}\n<\/code><\/pre>\n<p>What I&#8217;m trying to figure out (and perhaps I&#8217;m just completely missing this detail in the guides I&#8217;ve read) is how can I do the equivalent in IoC in an elegant manner?<\/p>\n<p>The structure I&#8217;m following is based off the one presented by Tony Sneed at http:\/\/www.develop.com\/onionarchitecture<\/p>\n<p>The only notable difference is I don&#8217;t have anything at my services level yet because most of our stuff is simply reading from\/writing to the database with very little notable manipulation in between. (I will likely have things I&#8217;ll need to put in on the service level, but just not hit any yet, still not entirely clear exactly what belongs at that level verses the repository.)<\/p>\n<p>Let me know if there is anymore details I can provide to be helpful (The IoC is working fine, I just need to tackle the above mentioned issue to keep maintainability reasonable)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve looked through a number of threads here (perhaps I&#8217;m using the wrong terminology), but I&#8217;m converting a Web Application over to use IoC via NInject. My other IoC projects were fairly small so it wasn&#8217;t a big issue, but what is the recommended practice for the following situation&#8230; My object&#8217;s structure looks like this&#8230; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3989","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3989"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3989\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}