{"id":3063,"date":"2025-11-12T11:32:13","date_gmt":"2025-11-12T04:32:13","guid":{"rendered":"https:\/\/portal-atlasx.cdg.co.th\/home\/?post_type=docs&#038;p=3063"},"modified":"2025-11-12T11:32:26","modified_gmt":"2025-11-12T04:32:26","slug":"dependency-injection","status":"publish","type":"docs","link":"https:\/\/portal-atlasx.cdg.co.th\/home\/docs\/ios\/services\/dependency-injection\/","title":{"rendered":"Dependency Injection"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"dependency-injection\">Dependency Injection<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"properties\">Properties<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Name<\/th><th>Description<\/th><th>Type<\/th><th>Default<\/th><\/tr><\/thead><tbody><tr><td>container<\/td><td>\u0e15\u0e31\u0e27\u0e41\u0e1b\u0e23\u0e17\u0e35\u0e48\u0e40\u0e01\u0e47\u0e1a Dependency \u0e17\u0e31\u0e49\u0e07\u0e2b\u0e21\u0e14\u0e02\u0e2d\u0e07 Service \u0e44\u0e27\u0e49 \u0e42\u0e14\u0e22\u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e40\u0e23\u0e35\u0e22\u0e01 Dependency \u0e44\u0e14\u0e49\u0e1c\u0e48\u0e32\u0e19\u0e15\u0e31\u0e27\u0e41\u0e1b\u0e23\u0e19\u0e35\u0e49<\/td><td>Container<\/td><td>Container \u0e17\u0e35\u0e48 Register Dependency \u0e02\u0e2d\u0e07 HttpService \u0e41\u0e25\u0e30 LocalDBService \u0e40\u0e2d\u0e32\u0e44\u0e27\u0e49<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"method-overview\">Method Overview<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Name<\/th><th>Description<\/th><th>Return<\/th><\/tr><\/thead><tbody><tr><td>register()<\/td><td>\u0e01\u0e32\u0e23 Register Dependency \u0e02\u0e2d\u0e07 Service \u0e44\u0e27\u0e49\u0e43\u0e19 Container<\/td><td>void<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"method-details\">Method Details<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-Fira-Code\" style=\"font-size:.875rem;font-family:Code-Pro-Fira-Code,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>static func register&lt;T>(classProtocol: T.Type, classObject: T)<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #001080\">static<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">func<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">register<\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #267F99\">T<\/span><span style=\"color: #000000\">&gt;(<\/span><span style=\"color: #001080\">classProtocol<\/span><span style=\"color: #000000\">: <\/span><span style=\"color: #0070C1\">T<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">Type<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #001080\">classObject<\/span><span style=\"color: #000000\">: <\/span><span style=\"color: #0070C1\">T<\/span><span style=\"color: #000000\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u0e01\u0e32\u0e23 Register Dependency \u0e02\u0e2d\u0e07 Service \u0e44\u0e27\u0e49\u0e43\u0e19 Container<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"parameters\">Parameters<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Name<\/th><th>Description<\/th><th>Type<\/th><\/tr><\/thead><tbody><tr><td>T<\/td><td>Class \u0e02\u0e2d\u0e07 Service<\/td><td>Class<\/td><\/tr><tr><td>classProtocol<\/td><td>Protocol \u0e02\u0e2d\u0e07 Class<\/td><td>T.Type<\/td><\/tr><tr><td>classObject<\/td><td>Object \u0e02\u0e2d\u0e07 Class<\/td><td>T<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-Fira-Code\" style=\"font-size:.875rem;font-family:Code-Pro-Fira-Code,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/\/ Protocol\nprotocol YourServiceProtocol {\n  func yourFunction()\n}\n\n\/\/ Class\nclass YourService: NSObject, YourServiceProtocol {\n\n  static let sharedInstance = YourService()\n  \n  func yourFunction() {}\n\n}\n\n\/\/ Register YourService\nDI.register(classProtocol: YourServiceProtocol.self, classObject: YourService.sharedInstance)\n\n\/\/ Get YourService from container\nlet yourService = DI.container.resolve(YourServiceProtocol.self)!\n\/\/ Can call your function\nyourService.yourFunction()<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ Protocol<\/span><\/span>\n<span class=\"line\"><span style=\"color: #001080\">protocol<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">YourServiceProtocol<\/span><span style=\"color: #000000\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">func<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">yourFunction<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Class<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">YourService<\/span><span style=\"color: #000000\">: <\/span><span style=\"color: #267F99\">NSObject<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #267F99\">YourServiceProtocol<\/span><span style=\"color: #000000\"> {<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">let<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">sharedInstance<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #795E26\">YourService<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">func<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">yourFunction<\/span><span style=\"color: #000000\">() {}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Register YourService<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0070C1\">DI<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">register<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">classProtocol<\/span><span style=\"color: #000000\">: <\/span><span style=\"color: #001080\">YourServiceProtocol<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">self<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #001080\">classObject<\/span><span style=\"color: #000000\">: <\/span><span style=\"color: #001080\">YourService<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">sharedInstance<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Get YourService from container<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">let<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">yourService<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #0070C1\">DI<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">container<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">resolve<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">YourServiceProtocol<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">self<\/span><span style=\"color: #000000\">)!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Can call your function<\/span><\/span>\n<span class=\"line\"><span style=\"color: #001080\">yourService<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">yourFunction<\/span><span style=\"color: #000000\">()<\/span><\/span><\/code><\/pre><\/div>\n","protected":false},"featured_media":0,"parent":3056,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"nav_title":"","positive":"","negative":"","footnotes":""},"docs_category":[],"class_list":["post-3063","docs","type-docs","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/portal-atlasx.cdg.co.th\/home\/wp-json\/wp\/v2\/docs\/3063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/portal-atlasx.cdg.co.th\/home\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/portal-atlasx.cdg.co.th\/home\/wp-json\/wp\/v2\/types\/docs"}],"replies":[{"embeddable":true,"href":"https:\/\/portal-atlasx.cdg.co.th\/home\/wp-json\/wp\/v2\/comments?post=3063"}],"version-history":[{"count":2,"href":"https:\/\/portal-atlasx.cdg.co.th\/home\/wp-json\/wp\/v2\/docs\/3063\/revisions"}],"predecessor-version":[{"id":3065,"href":"https:\/\/portal-atlasx.cdg.co.th\/home\/wp-json\/wp\/v2\/docs\/3063\/revisions\/3065"}],"up":[{"embeddable":true,"href":"https:\/\/portal-atlasx.cdg.co.th\/home\/wp-json\/wp\/v2\/docs\/3056"}],"wp:attachment":[{"href":"https:\/\/portal-atlasx.cdg.co.th\/home\/wp-json\/wp\/v2\/media?parent=3063"}],"wp:term":[{"taxonomy":"docs_category","embeddable":true,"href":"https:\/\/portal-atlasx.cdg.co.th\/home\/wp-json\/wp\/v2\/docs_category?post=3063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}