{"id":3249,"date":"2020-01-24T09:48:50","date_gmt":"2020-01-24T08:48:50","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=3249"},"modified":"2022-07-25T08:33:24","modified_gmt":"2022-07-25T07:33:24","slug":"c-autofac-example","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-autofac-example\/","title":{"rendered":"C# Autofac example"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using Autofac;\n\nnamespace AutoFacTester\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            Run(Wire());\n        }\n\n        static IContainer Wire()\n        {\n            var builder = new ContainerBuilder();\n\n            \/\/builder.RegisterAssemblyTypes(typeof(ConsoleOutput).Assembly)\n            \/\/    .Where(t => typeof(IStartable).IsAssignableFrom(t))\n            \/\/    .As&lt;IStartable>()\n            \/\/    .SingleInstance();\n\n            \/\/builder.Register(c => new ConsoleOutput()).As&lt;IOutput>().SingleInstance();\n            builder.RegisterType&lt;ConsoleOutput>().As&lt;IOutput>().SingleInstance();\n            builder.RegisterType&lt;TodayWriter>().As&lt;IDateWriter>();\n            return builder.Build();\n        }\n\n        static void Run(IContainer container) \n        {\n            using (var scope = container.BeginLifetimeScope())\n            {\n                var writer = scope.Resolve&lt;IDateWriter>();\n                writer.WriteDate();\n            }\n        }\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-3249","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=3249"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3249\/revisions"}],"predecessor-version":[{"id":3250,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3249\/revisions\/3250"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=3249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=3249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=3249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}