
{"id":4535,"date":"2019-04-17T22:35:47","date_gmt":"2019-04-18T02:35:47","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4535"},"modified":"2019-04-17T22:36:19","modified_gmt":"2019-04-18T02:36:19","slug":"unity-container-and-double-singleton","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4535","title":{"rendered":"Unity Container and double singleton"},"content":{"rendered":"\n<p>It turns out that after re-registering type as a singleton in Unity calls to container.Resolve() return <em>a&nbsp;new&nbsp;instance<\/em>. This may lead and have led to ugly bugs if your code relies on having only one instance of the type.  Proofing test:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing NUnit.Framework;\nusing Unity;\nusing Unity.Lifetime;\n\nnamespace DoubleSingletonRegistration\n{\n    &#x5B;TestFixture]\n    public class DoubleSingletonTest\n    {\n        public class Foo {}\n\n        &#x5B;Test]\n        public void NewInstanceReturnedAfterReregistration()\n        {\n            var container = new UnityContainer();\n\n            container.RegisterType&lt;Foo&gt;(new ContainerControlledLifetimeManager());\n            var foo1 = container.Resolve&lt;Foo&gt;();\n            var foo2 = container.Resolve&lt;Foo&gt;();\n\n            container.RegisterType&lt;Foo&gt;(new ContainerControlledLifetimeManager());\n            var foo3 = container.Resolve&lt;Foo&gt;();\n            var foo4 = container.Resolve&lt;Foo&gt;();\n\n            Assert.AreSame(foo1, foo2);\n            Assert.AreNotSame(foo2, foo3); \/\/ new instance returned after re-registration!\n            Assert.AreSame(foo3, foo4);\n        }\n    }\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>It turns out that after re-registering type as a singleton in Unity calls to container.Resolve() return a&nbsp;new&nbsp;instance. This may lead and have led to ugly bugs if your code relies <a href=\"https:\/\/ikriv.com\/blog\/?p=4535\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[3],"tags":[],"class_list":["entry","author-ikriv","post-4535","post","type-post","status-publish","format-standard","category-dotnet"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4535"}],"version-history":[{"count":2,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4535\/revisions"}],"predecessor-version":[{"id":4537,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4535\/revisions\/4537"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}