{"id":1769,"date":"2020-10-15T12:05:27","date_gmt":"2020-10-15T04:05:27","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1769"},"modified":"2022-02-16T21:48:43","modified_gmt":"2022-02-16T13:48:43","slug":"iris-dependency-injection-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/10\/15\/iris-dependency-injection-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris\u7684\u4f9d\u8cf4\u6ce8\u5165"},"content":{"rendered":"<h1>iris\u7684\u4f9d\u8cf4\u6ce8\u5165<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4ecb\u7d39\u4e00\u4e0b\u5176\u4ed6\u8a9e\u8a00\u4e5f\u6709\u7684\u8a2d\u8a08\u6982\u5ff5\uff0c\u5c31\u662f\u4f9d\u8cf4\u6ce8\u5165\uff0c\u4ee5\u53ca\u5728iris\u5982\u4f55\u5229\u7528\u9019\u7a2e\u8a2d\u8a08\u6982\u5ff5\u8b93\u7a0b\u5f0f\u7684\u7d44\u4ef6\u8077\u80fd\u5206\u5de5\uff0c\u4e26\u4e14\u80fd\u6bcf\u500b\u7d44\u4ef6\u5c08\u6ce8\u5728\u5404\u81ea\u7684\u908f\u8f2f\u958b\u767c\u3002<\/p>\n<p><!--more--><\/p>\n<h2>iris\u7684\u4f9d\u8cf4\u6ce8\u5165<\/h2>\n<p>\u672c\u7bc7\u4ecb\u7d39iris\u7684\u4f9d\u8cf4\u6ce8\u5165\uff0c\u9996\u5148\u5148\u7531\u4ec0\u9ebc\u662f\u4f9d\u8cf4\u6ce8\u5165\u958b\u59cb\uff0c\u8b93\u5927\u5bb6\u5c0d\u65bc\u9019\u7a2e\u8a2d\u8a08\u6982\u5ff5\u6709\u4e00\u4e9b\u521d\u6b65\u7684\u6982\u5ff5\uff0c\u7136\u5f8c\u518d\u5e36\u5230\u5982\u4f55\u5728iris\u5229\u7528\u4f9d\u8cf4\u6ce8\u5165\u7684\u65b9\u5f0f\u4f86\u958b\u767c\uff0c\u6240\u6709\u5148\u8ddf\u5927\u5bb6\u4ecb\u7d39\u4f9d\u8cf4\u6ce8\u5165\u3002<\/p>\n<h3>\u4f9d\u8cf4\u6ce8\u5165<\/h3>\n<p>\u63d0\u5230<a href=\"https:\/\/zh.m.wikipedia.org\/zh-tw\/\u4f9d\u8d56\u6ce8\u5165\" title=\"\u4f9d\u8cf4\u6ce8\u5165 wiki\" rel=\"nofollow noopener\" target=\"_blank\">\u4f9d\u8cf4\u6ce8\u5165<\/a>\u5c31\u4e0d\u80fd\u4e0d\u63d0\u53e6\u4e00\u500b\u6982\u5ff5\u5c31\u662f<a href=\"https:\/\/zh.m.wikipedia.org\/zh-tw\/\u63a7\u5236\u53cd\u8f6c\" title=\"\u63a7\u5236\u53cd\u8f49 wiki\" rel=\"nofollow noopener\" target=\"_blank\">\u63a7\u5236\u53cd\u8f49<\/a><code>IoC<\/code>\uff0c\u9019\u500b\u6982\u5ff5\u7684\u8d77\u56e0\u662f\u56e0\u70ba\u6709\u500b\u4eba\u60f3\u8aaa\u70ba\u4ec0\u9ebc\u90fd\u662f\u985e\u5225\u6c7a\u5b9a\u9700\u8981\u4ec0\u9ebc\u7d44\u4ef6\u5462\uff0c\u70ba\u4ec0\u9ebc\u4e0d\u80fd\u53cd\u904e\u4f86\u8b93\u5916\u90e8\u6c7a\u5b9a\u985e\u5225\u9700\u8981\u7684\u7d44\u4ef6\u5462\uff0c\u6240\u4ee5\u5c31\u6210\u4e86\u4eca\u5929\u8981\u8aaa\u660e\u7684\u4e3b\u984c\u4f9d\u8cf4\u6ce8\u5165\u3002<br \/>\n\u9019\u6642\u5019\u8b80\u8005\u6703\u597d\u5947\u90a3\u600e\u9ebc\u7531\u5916\u90e8\u6c7a\u5b9a\u985e\u5225\u8981\u76f8\u4f9d\u7684\u7d44\u4ef6\u5462\uff0c\u7c21\u55ae\u4f86\u8aaa\u985e\u5225\u53ea\u8981\u76f8\u4f9d\u4ecb\u9762<code>interface<\/code>\u5c31\u597d\u4e86\uff0c\u4e0d\u8981\u76f8\u4f9d\u5230\u5be6\u9ad4\uff0c\u5982\u6b64\u5c31\u53ef\u4ee5\u7531\u5916\u90e8\u4f86\u6c7a\u5b9a\u9019\u500b\u985e\u5225\u8981\u76f8\u4f9d\u7684\u5be6\u9ad4\u662f\u4ec0\u9ebc\uff0c\u63db\u4e00\u53e5\u8a71\u4f86\u8aaa\u5c31\u662f\u5728\u5916\u90e8\u4f86\u6ce8\u5165\u985e\u5225\u6240\u4f9d\u8cf4\u7684\u7d44\u4ef6\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u958b\u59cb\u4ecb\u7d39\u600e\u9ebc\u5728iris\u4e2d\u5be6\u4f5c\u4f9d\u8cf4\u6ce8\u5165\u7684\u8a2d\u8a08\u6982\u5ff5\u3002<\/p>\n<h3>iris\u7684\u4f9d\u8cf4\u6ce8\u5165<\/h3>\n<p>\u5728<a href=\"https:\/\/docs.iris-go.com\/iris\/dependency-injection\/dependency-injection\" title=\"iris\u7684\u76f8\u4f9d\u6ce8\u5165\u6587\u4ef6\" rel=\"nofollow noopener\" target=\"_blank\">iris\u7684\u4f9d\u8cf4\u6ce8\u5165\u6587\u4ef6<\/a>\u4e2d\u6709\u8a73\u7d30\u7684\u8aaa\u660e\u5982\u4f55\u5728iris\u4e2d\u7528\u4f9d\u8cf4\u6ce8\u5165\u7684\u65b9\u5f0f\u958b\u767c\uff0c\u4e0d\u904e\u5176\u5167\u5bb9\u6709\u9ede\u8907\u96dc\uff0c\u6240\u4ee5\u6211\u5011\u76f4\u63a5\u770b<a href=\"https:\/\/github.com\/kataras\/iris\/tree\/master\/_examples\/dependency-injection\/overview\" title=\"iris\u5b98\u65b9\u4f9d\u8cf4\u6ce8\u5165\u7684\u7bc4\u4f8b\" rel=\"nofollow noopener\" target=\"_blank\">iris\u5b98\u65b9\u4f9d\u8cf4\u6ce8\u5165\u7684\u7bc4\u4f8b<\/a>\uff0c\u96d6\u7136\u500b\u7bc4\u4f8b\u7684\u7a0b\u5f0f\u78bc\u5f88\u591a\uff0c\u4f46\u662f\u5c0d\u65bc\u4f9d\u8cf4\u6ce8\u5165\u7684\u5167\u5bb9\u53ea\u6709\u4e00\u4e9b\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u91dd\u5c0d\u9019\u4e9b\u5167\u5bb9\u8ddf\u5927\u5bb6\u8aaa\u660e\u3002<br \/>\n\u9996\u5148\u8acb\u5927\u5bb6\u770b\u4e00\u4e0b\u4e0b\u5217\u7684\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">\/\/ file: web\/routes\/movie.go\npackage routes\n\nimport (\n    &quot;errors&quot;\n\n    &quot;github.com\/kataras\/iris\/v12\/_examples\/dependency-injection\/overview\/datamodels&quot;\n    &quot;github.com\/kataras\/iris\/v12\/_examples\/dependency-injection\/overview\/services&quot;\n\n    &quot;github.com\/kataras\/iris\/v12&quot;\n)\n\n\/\/ Movies returns list of the movies.\n\/\/ Demo:\n\/\/ curl -i http:\/\/localhost:8080\/movies\nfunc Movies(service services.MovieService) (results []datamodels.Movie) {\n    return service.GetAll()\n}\n\n\/\/ MovieByID returns a movie.\n\/\/ Demo:\n\/\/ curl -i http:\/\/localhost:8080\/movies\/1\nfunc MovieByID(service services.MovieService, id uint64) (movie datamodels.Movie, found bool) {\n    return service.GetByID(id) \/\/ it will throw 404 if not found.\n}\n\n\/\/ UpdateMovieByID updates a movie.\n\/\/ Demo:\n\/\/ curl -i -X PUT -F &quot;genre=Thriller&quot; -F &quot;poster=@\/Users\/kataras\/Downloads\/out.gif&quot; http:\/\/localhost:8080\/movies\/1\nfunc UpdateMovieByID(ctx iris.Context, service services.MovieService, id uint64) (datamodels.Movie, error) {\n    \/\/ get the request data for poster and genre\n    file, info, err := ctx.FormFile(&quot;poster&quot;)\n    if err != nil {\n        return datamodels.Movie{}, errors.New(&quot;failed due form file &#039;poster&#039; missing&quot;)\n    }\n    \/\/ we don&#039;t need the file so close it now.\n    file.Close()\n\n    \/\/ imagine that is the url of the uploaded file...\n    poster := info.Filename\n    genre := ctx.FormValue(&quot;genre&quot;)\n\n    return service.UpdatePosterAndGenreByID(id, poster, genre)\n}\n\n\/\/ DeleteMovieByID deletes a movie.\n\/\/ Demo:\n\/\/ curl -i -X DELETE -u admin:password http:\/\/localhost:8080\/movies\/1\nfunc DeleteMovieByID(service services.MovieService, id uint64) interface{} {\n    wasDel := service.DeleteByID(id)\n    if wasDel {\n        \/\/ return the deleted movie&#039;s ID\n        return iris.Map{&quot;deleted&quot;: id}\n    }\n    \/\/ right here we can see that a method function can return any of those two types(map or int),\n    \/\/ we don&#039;t have to specify the return type to a specific type.\n    return iris.StatusBadRequest\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u662f\u9700\u8981\u50b3\u5165\u4e00\u500bMovieService\u7684<code>handler<\/code>\u800c\u9019\u500bMovieService\u5c31\u662f\u6211\u5011\u5b9a\u7fa9\u7684<code>interface<\/code>\u6240\u4ee5\u6211\u5011\u9019\u500b<code>handler<\/code>\u76f4\u63a5\u76f8\u4f9d\u9019\u500b<code>interface<\/code>\u800c\u4e0d\u5728\u4e4e\u9019<code>interface\u7684\u5be6\u4f5c\u662f\u4ec0\u9ebc\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u6211\u5011\u53ea\u8981\u8a3b\u518a\u5be6\u4f5c\u9019<\/code>interface`\u7684\u5be6\u9ad4\u4e26\u6ce8\u5165\u5373\u53ef\uff0c\u6240\u4ee5\u6211\u5011\u518d\u770b\u770b\u4e0b\u9762\u4f8b\u5b50\u4f86\u4e86\u89e3\u5982\u4f55\u6ce8\u5165\u3002<\/p>\n<pre><code class=\"language-go\">\/\/ file: main.go\npackage main\n\nimport (\n    &quot;github.com\/kataras\/iris\/v12\/_examples\/dependency-injection\/overview\/datasource&quot;\n    &quot;github.com\/kataras\/iris\/v12\/_examples\/dependency-injection\/overview\/repositories&quot;\n    &quot;github.com\/kataras\/iris\/v12\/_examples\/dependency-injection\/overview\/services&quot;\n    &quot;github.com\/kataras\/iris\/v12\/_examples\/dependency-injection\/overview\/web\/middleware&quot;\n    &quot;github.com\/kataras\/iris\/v12\/_examples\/dependency-injection\/overview\/web\/routes&quot;\n\n    &quot;github.com\/kataras\/iris\/v12&quot;\n)\n\nfunc main() {\n    app := iris.New()\n    app.Logger().SetLevel(&quot;debug&quot;)\n\n    \/\/ Load the template files.\n    app.RegisterView(iris.HTML(&quot;.\/web\/views&quot;, &quot;.html&quot;))\n\n    \/\/ Create our movie repository with some (memory) data from the datasource.\n    repo := repositories.NewMovieRepository(datasource.Movies)\n\n    app.Party(&quot;\/hello&quot;).ConfigureContainer(func(r *iris.APIContainer) {\n        r.Get(&quot;\/&quot;, routes.Hello)\n        r.Get(&quot;\/{name}&quot;, routes.HelloName)\n    })\n\n    app.Party(&quot;\/movies&quot;).ConfigureContainer(func(r *iris.APIContainer) {\n        \/\/ Create our movie service, we will bind it to the movie app&#039;s dependencies.\n        movieService := services.NewMovieService(repo)\n        r.RegisterDependency(movieService)\n\n        \/\/ Add the basic authentication(admin:password) middleware\n        \/\/ for the \/movies based requests.\n        r.Use(middleware.BasicAuth)\n\n        r.Get(&quot;\/&quot;, routes.Movies)\n        r.Get(&quot;\/{id:uint64}&quot;, routes.MovieByID)\n        r.Put(&quot;\/{id:uint64}&quot;, routes.UpdateMovieByID)\n        r.Delete(&quot;\/{id:uint64}&quot;, routes.DeleteMovieByID)\n    })\n\n    \/\/ http:\/\/localhost:8080\/hello\n    \/\/ http:\/\/localhost:8080\/hello\/iris\n    \/\/ http:\/\/localhost:8080\/movies (&quot;admin&quot;: &quot;password&quot;)\n    \/\/ http:\/\/localhost:8080\/movies\/1\n    app.Listen(\n        \/\/ Start the web server at localhost:8080\n        &quot;localhost:8080&quot;,\n        \/\/ enables faster json serialization and more:\n        iris.WithOptimizations,\n    )\n}<\/code><\/pre>\n<p>\u76f8\u4fe1\u5927\u5bb6\u90fd\u53ef\u4ee5\u770b\u5230\u6211\u5011\u7d81\u5b9a<code>handler<\/code>\u4e4b\u524d\u6703\u547c\u53eb<code>Party(&quot;\/movies&quot;).ConfigureContainer(func(r *iris.APIContainer)<\/code>\u9019\u4e00\u6bb5\uff0c\u4e26\u4e14\u547c\u53eb<code>r.RegisterDependency(movieService)<\/code>\u4f86\u547c\u53eb\u8a3b\u518a<code>service<\/code>\u5982\u6b64\u5728\u7d81\u5b9a<code>handler<\/code>\u4e4b\u5f8c\u7576<code>handler<\/code>\u88ab\u547c\u53eb\u6642\u6703\u8a3b\u518a\u8a72<code>service<\/code>\u7d66\u5176\u4f7f\u7528\u4e86\u3002<br \/>\n\u81f3\u65bc<code>service<\/code>\u4ee5\u53ca<code>repository<\/code>\u7684\u5be6\u4f5c\u5c31\u4e0d\u662f\u672c\u7bc7\u6587\u7ae0\u7684\u91cd\u9ede\uff0c\u9084\u8acb\u8b80\u8005\u5011\u81ea\u884c\u95b1\u8b80\u4e86\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u7bc7\u4ecb\u7d39\u4e86\u4f9d\u8cf4\u6ce8\u5165\u7684\u5167\u5bb9\u4ee5\u53ca\u5982\u4f55\u5728iris\u4e0a\u4f7f\u7528\u4f9d\u8cf4\u6ce8\u5165\u7684\u65b9\u5f0f\u9032\u884c\u958b\u767c\uff0c\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u53ef\u4ee5\u5e6b\u52a9\u5927\u5bb6\u90fd\u7a0b\u5f0f\u958b\u767c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>iris\u7684\u4f9d\u8cf4\u6ce8\u5165 \u672c\u7bc7\u6587\u7ae0\u4ecb\u7d39\u4e00\u4e0b\u5176\u4ed6\u8a9e\u8a00\u4e5f\u6709\u7684\u8a2d\u8a08\u6982\u5ff5\uff0c\u5c31\u662f\u4f9d\u8cf4\u6ce8\u5165\uff0c\u4ee5\u53ca\u5728iris\u5982\u4f55\u5229\u7528\u9019\u7a2e\u8a2d\u8a08\u6982\u5ff5\u8b93 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/10\/15\/iris-dependency-injection-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris\u7684\u4f9d\u8cf4\u6ce8\u5165\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81,84],"class_list":["post-1769","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go","tag-iris"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1769"}],"version-history":[{"count":35,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1769\/revisions"}],"predecessor-version":[{"id":2948,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1769\/revisions\/2948"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}