{"id":496268,"date":"2026-09-27T20:37:13","date_gmt":"2026-09-27T20:37:13","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=496268"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=496268","title":{"rendered":"\u0412\u0441\u0435 (\u044f \u043d\u0430\u0434\u0435\u044e\u0441\u044c) \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u0432 go"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0422\u0430\u043a \u0443\u0436 \u0432\u044b\u0448\u043b\u043e, \u0447\u0442\u043e \u044f \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u043d\u0435 \u043c\u043e\u0433\u0443 \u043f\u043e\u0434\u043e\u0431\u0440\u0430\u0442\u044c \u043b\u0443\u0447\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u0443 \u043e\u0431\u0437\u043e\u0440\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e. \u042f \u0440\u0430\u0437\u0431\u0435\u0440\u0443 \u0432\u0441\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0435 \u043c\u043d\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u0438 \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043b\u0443\u0447\u0448\u0438\u0439.<\/p>\n<h2>\u0412\u0432\u043e\u0434\u043d\u044b\u0435<\/h2>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0430\u043c\u044b\u0439 \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u043b\u044e\u0431\u043e\u0439 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0411\u0414. \u0423 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 &#8212;  \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0438 \u0437\u0430\u043a\u0430\u0437\u044b. \u041a\u0430\u0436\u0434\u0430\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0441\u0432\u043e\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u041c\u044b \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f (\u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e) \u043d\u0435 \u043c\u0435\u0448\u0430\u0442\u044c \u0441\u043b\u043e\u0439 \u0411\u0414 \u0441\u043e \u0441\u043b\u043e\u0435\u043c \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u0438 \u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c\u044b\u0439 \u043a\u043e\u0434.<\/p>\n<p>\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u043a\u043e\u0434\u0430 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u044b \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0435\u0440\u0435\u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0442\u044c.<\/p>\n<p>\u0421\u0442\u043e\u043b\u043a\u043d\u0443\u0432\u0448\u0438\u0441\u044c \u0441 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438, \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u0437 \u044d\u0442\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432:<\/p>\n<ol>\n<li>\n<p><a href=\"#1\" rel=\"noopener nofollow\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 \u0441\u043b\u043e\u0435 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#2\" rel=\"noopener nofollow\">\u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#3\" rel=\"noopener nofollow\">\u0410\u0433\u0440\u0435\u0433\u0430\u0442\u044b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#4\" rel=\"noopener nofollow\">\u041e\u0434\u0438\u043d \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043d\u0430 \u0432\u0441\u0451<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#5\" rel=\"noopener nofollow\">CQRS (ver. 1)<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#6\" rel=\"noopener nofollow\">CQRS (ver. 2)<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#7\" rel=\"noopener nofollow\">UOW (unit of work)<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#8\" rel=\"noopener nofollow\">Callback<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#end\" rel=\"noopener nofollow\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/a><\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"1\" id=\"1\"><\/a><\/p>\n<h2>1. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 \u0441\u043b\u043e\u0435 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438<\/h2>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0411\u0414 \u0438 \u044e\u0437\u0435\u0440\u0430, \u0438 \u0437\u0430\u043a\u0430\u0437. \u0422\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"go\">package handlerimport (  \"database\/sql\"  \"model\"  \"context\")type UsersRepository {  Save(ctx context.Context, user model.User, tx *sql.Tx)}type OrdersRepository {  Save(ctx context.Context, order model.Order, tx *sql.Tx)}type CreateHandler struct {  usersDb UsersRepository  ordersDb OrdersRepository  db *sql.Db}func (h *CreateHandler) Create(user model.User, order model.Order) error {  tx := h.db.Begin()  defer tx.Rollback()  h.usersDb.Save(user, tx)  h.ordersDb.Save(order, tx)  return tx.Commit()}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:87px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041f\u043b\u044e\u0441 \u0443 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d: \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0439\u043c\u0451\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043b\u044e\u0431\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f.<\/p>\n<p>\u041d\u043e \u043c\u0438\u043d\u0443\u0441\u043e\u0432 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435:<\/p>\n<ul>\n<li>\n<p>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u043c\u044b \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0435 \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>Save<\/code> \u0438\u0437 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0431\u0435\u0437 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438?<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0441\u043b\u043e\u0439 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u043f\u0440\u043e\u0442\u0435\u043a\u043b\u0430 \u043b\u043e\u0433\u0438\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u0435\u0445\u0430\u0442\u044c \u0441 <code>database\/sql<\/code> \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432 \u0441\u043b\u043e\u0435 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438<\/p>\n<\/li>\n<li>\n<p>\u0412\u0435\u043b\u0438\u043a\u0430 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u0442\u0430\u043a \u043a\u0430\u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0431\u044b\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <code>tx.Commit<\/code> \u0438\u043b\u0438 <code>tx.Rollback<\/code><\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0438\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u043e \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0438\u0437\u043e\u043b\u044f\u0446\u0438\u0438 \u043a\u043e\u0434 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u0438\u043b\u044c\u043d\u043e \u043c\u0435\u043d\u0435\u0435 \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u043c<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u044f \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0438 \u0432\u0438\u0436\u0443 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445. \u0418 \u043f\u043e\u0434\u043e\u0437\u0440\u0435\u0432\u0430\u044e, \u0447\u0442\u043e \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u0431\u0443\u0434\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u0437\u0430\u0449\u0438\u0442\u043d\u0438\u043a\u043e\u0432 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430, \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0435 go \u043e\u0447\u0435\u043d\u044c \u043b\u044e\u0431\u044f\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0443.<\/p>\n<p><a class=\"anchor\" name=\"2\" id=\"2\"><\/a><\/p>\n<h2>2. \u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439<\/h2>\n<p>\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431, \u0432\u0438\u0434\u0438\u043c\u043e, \u043d\u0435 \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u043d\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u044b \u0438\u0437 \u0430\u0432\u0438\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0441\u0432\u043e\u0439 <a href=\"https:\/\/github.com\/avito-tech\/go-transaction-manager\" rel=\"noopener nofollow\">\u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439<\/a>. \u041e\u043d \u0445\u0440\u0430\u043d\u0438\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0438\u043c\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">package handlerimport (  \"model\"  \"context\"  \"github.com\/avito-tech\/go-transaction-manager\/trm\/v2\/manager\")type UsersRepository {  Save(ctx context.Context, user model.User)}type OrdersRepository {  Save(ctx context.Context, order model.Order)}type CreateHandler struct {  usersDb UsersRepository  ordersDb OrdersRepository  txManager manager.Manager}func (h *CreateHandler) Create(ctx context.Context, user model.User, order model.Order) error {  \/\/ \u043c\u0435\u0442\u043e\u0434 Do \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0435\u0451 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442  \/\/ \u0435\u0441\u043b\u0438 \u043a\u043e\u043b\u0431\u044d\u043a \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443, \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f  return h.txManager.Do(ctx, func(ctx context.Context) error {    h.usersDb.Save(ctx, user)    h.ordersDb.Save(ctx, order)    return nil  })}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435 \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u043d\u0435 \u043a\u0440\u0438\u0442\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0445\u043e\u0442\u044c \u043e\u043d \u0438 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f. \u041a\u043e\u0434 \u0432\u0441\u0451 \u0435\u0449\u0451 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0439, \u043f\u0440\u043e \u043e\u0442\u043a\u0430\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0434\u0443\u043c\u0430\u0442\u044c \u043d\u0435 \u043d\u0430\u0434\u043e, \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0438 \u0432\u043d\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439. \u041d\u043e \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043b\u0438\u0447\u043d\u043e \u043c\u0435\u043d\u044f \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043e\u0442\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u0442. \u0412 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 go \u043f\u043e \u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0432\u043e\u0434\u0443 \u0441\u043a\u0430\u0437\u0430\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435: <\/p>\n<blockquote>\n<p>Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions.<\/p>\n<\/blockquote>\n<p>\u0424\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a request-scoped data, \u043d\u043e \u0438\u043c\u0445\u043e \u044d\u0442\u043e \u043f\u0440\u0438\u0442\u044f\u0433\u0438\u0432\u0430\u043d\u0438\u0435 \u0437\u0430 \u0443\u0448\u0438.<\/p>\n<p>\u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438, \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u0432 \u043b\u044e\u0431\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u0443\u0434\u043e\u0431\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u043d\u0451\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u0425\u043e\u0442\u044f \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0435\u0433\u043e \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0446\u0435\u043b\u0435\u0439.<\/p>\n<p><a class=\"anchor\" name=\"3\" id=\"3\"><\/a><\/p>\n<h2>3. \u0410\u0433\u0440\u0435\u0433\u0430\u0442\u044b<\/h2>\n<p>\u041c\u044b \u0432\u0441\u0435 \u043f\u0440\u0438\u0432\u044b\u043a\u043b\u0438, \u0447\u0442\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0441\u0432\u043e\u0439 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439. \u0414\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f <code>UsersRepository<\/code>, \u0434\u043b\u044f \u0437\u0430\u043a\u0430\u0437\u043e\u0432 &#8212; <code>OrdersRepository<\/code> \u0438 \u0442. \u0434. \u041d\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u044d\u0442\u043e \u043d\u0435 \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0437\u043e\u043b\u043e\u0442\u043e\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u043d\u0430\u0440\u0443\u0448\u0430\u0442\u044c. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043f\u0440\u0430\u0432\u0435\u0434\u043b\u0438\u0432\u043e \u0437\u0430\u043c\u0435\u0442\u044f\u0442, \u0447\u0442\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043d\u0430\u0434\u043e \u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0435 \u043f\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c \u0432 \u0411\u0414, \u0430 \u043f\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c. <\/p>\n<p>\u0415\u0441\u043b\u0438 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u043a\u0430\u0437\u0430, \u0438 \u044e\u0437\u0435\u0440\u0430, \u0442\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0434\u0435\u043b\u0438\u0442\u044c \u044d\u0442\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043d\u0430 \u0434\u0432\u0435? \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0430\u0433\u0440\u0435\u0433\u0430\u0442, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0437\u0430\u043a\u0430\u0437\u0430 \u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0438 \u043d\u0430\u0448 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">package handlerimport (  \"model\"  \"context\")type UsersRepository {  Save(ctx context.Context, user model.UserWithOrder)}type CreateHandler struct {  usersDb UsersRepository}func (h *CreateHandler) Create(ctx context.Context, user model.User, order model.Order) error {  userWithOrder := model.NewUserWithOrder(user, order)  h.usersDb.Save(userWithOrder)  return nil}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041c\u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434. \u042f \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0435\u043c\u0443, \u043d\u043e \u0442\u0443\u0442 \u0442\u043e\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u043d\u044e\u0430\u043d\u0441\u044b. \u0418\u043d\u043e\u0433\u0434\u0430 \u0432\u0441\u0451 \u0436\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f, \u0447\u0442\u043e \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0435 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0437\u0430\u043a\u0430\u0437 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0418 \u0442\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u043a\u0430\u043a-\u0442\u043e \u0448\u0430\u0440\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0437\u0430\u043a\u0430\u0437\u0430 \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f\u043c\u0438, \u043b\u0438\u0431\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043b\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u043d\u0430\u0435\u0442 \u043f\u0440\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438, \u043d\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u043f\u0440\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443. \u0412\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">package storageimport (  \"database\/sql\"  \"model\"  \"context\")type UsersRepository {  Save(ctx context.Context, user model.User, tx *sql.Tx) }type OrdersRepository {  Save(ctx context.Context, order model.Order, tx *sql.Tx)}type Storage struct {}\/\/ \u0438\u0437 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434\/\/ \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0432 \u043d\u0451\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f func (s *Storage) Create(ctx context.Context, user model.UserWithOrder) error {  tx := h.db.Begin()  defer tx.Rollback()  h.usersDb.Save(user.User, tx)  h.ordersDb.Save(user.Order, tx)  return tx.Commit()}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p><a class=\"anchor\" name=\"4\" id=\"4\"><\/a><\/p>\n<h2>4. \u041e\u0434\u0438\u043d \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043d\u0430 \u0432\u0441\u0451<\/h2>\n<p>\u0421\u0440\u0430\u0437\u0443 \u043e\u0433\u043e\u0432\u043e\u0440\u044e\u0441\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u044d\u043a\u0441\u0442\u0440\u0435\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443 \u043c\u043d\u043e\u0433\u0438\u0445 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043e\u0442\u0442\u043e\u0440\u0436\u0435\u043d\u0438\u0435. \u041d\u043e \u0443 \u043d\u0435\u0433\u043e \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0441\u0432\u043e\u0438\u0445 \u043f\u043b\u044e\u0441\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435 \u043c\u043e\u0433\u0443 \u043d\u0435 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u043d\u0451\u043c. \u0418\u0434\u0435\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f: \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043e\u0434\u0438\u043d \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043d\u0430 \u0432\u0441\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439:<\/p>\n<pre><code class=\"go\">package handlerimport (  \"model\"  \"context\")type Repository interface{  SaveUser(ctx context.Context, user model.User)  SaveOrder(ctx context.Context, order model.Order)  InTx(ctx context.Context, f func(ctx context.Context, tx Repository) error)}type CreateHandler struct {  db Repository}func (h *CreateHandler) Create(ctx context.Context, user model.User, order model.Order) error {  return h.db.InTx(ctx, func(ctx context.Context, tx Repository) error {    tx.SaveUser(ctx, user)    tx.SaveOrder(ctx, order)    return nil  })}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0414\u0430, \u043c\u043d\u043e\u0433\u0438\u043c \u0442\u0430\u043a\u043e\u0435 \u043d\u0435 \u0437\u0430\u0439\u0434\u0451\u0442. \u041d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0443 \u043c\u043d\u043e\u0433\u0438\u0445 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u0441\u0440\u0430\u0437\u0443 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442 \u043c\u044b\u0441\u043b\u0438 \u043f\u0440\u043e god object, big ball of mud (\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u043e\u043c\u043e\u043a \u0433\u0440\u044f\u0437\u0438), \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0435 SOLID \u0438 \u043f\u0440. \u041d\u043e \u043c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0435 \u0442\u0430\u043a \u0443\u0436 \u0438 \u043f\u043b\u043e\u0445. \u042f \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0439\u043c\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0431\u0435\u0440\u0443\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445.<\/p>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 <a href=\"https:\/\/entgo.io\/\" rel=\"noopener nofollow\">ent<\/a>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043a\u043e\u0441\u0438\u0442 \u043f\u043e\u0434 entity framework.  <\/p>\n<p><a class=\"anchor\" name=\"5\" id=\"5\"><\/a><\/p>\n<h2>5. CQRS (ver. 1)<\/h2>\n<p>\u041c\u043e\u0436\u043d\u043e \u0447\u0443\u0442\u044c \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0438 \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u043c \u0432\u0441\u0435 \u043d\u0430\u0448\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0411\u0414 \u043d\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u044b (command) \u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b (query), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d executor:<\/p>\n<pre><code class=\"go\">package cqrsimport (  \"model\"  \"context\"  \"command\"  \"database\/sql\")type Command interface {Handle(ctx context.Context, conn *sql.Conn) error}type Query[T any] interface {Handle(ctx context.Context, conn *sql.Conn) (T, error)}type Executor interface {RunCommand(ctx context.Context, command Command) errorRunQuery(ctx context.Context, query Query[any]) (any, error)InTx(ctx context.Context, f func(ctx context.Context, e Executor) error) error}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0418 \u0442\u043e\u0433\u0434\u0430 \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">package handlerimport (  \"model\"  \"context\"  \"command\")type Executor interface{  RunCommand(ctx context.Context, command Command) error  InTx(ctx context.Context, f func(ctx context.Context, e Executor) error) error}type CreateHandler struct {  db Executor}func (h *CreateHandler) Create(ctx context.Context, user model.User, order model.Order) error {  return h.db.InTx(ctx, func(ctx context.Context, tx Executor) error {    tx.RunCommand(ctx, command.SaveUser{User: user})    tx.RunCommand(ctx, command.SaveOrder{Order: order}))    return nil  })}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442 \u043e\u0434\u043d\u043e\u0433\u043e \u0433\u0438\u0433\u0430\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u0433\u0434\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c\u0438. \u041d\u043e \u043d\u0430\u0440\u0443\u0436\u0443 \u0442\u043e\u0440\u0447\u0430\u0442 \u043c\u0435\u0442\u043e\u0434\u044b <code>Handle<\/code>, \u0433\u0434\u0435 \u0432 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435 \u043d\u0430\u0440\u0443\u0436\u0443 \u0442\u043e\u0440\u0447\u0438\u0442 \u0442\u0438\u043f <code>sql.Tx<\/code>. \u0414\u0430, \u0444\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0435\u0433\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u043d\u043e \u0447\u0443\u0432\u0441\u0442\u0432\u043e \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0437\u0430\u0434\u0435\u0442\u043e.<\/p>\n<p>\u0412 C# \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0432\u0435\u0449\u0435\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 <a href=\"https:\/\/mediatr.io\/\" rel=\"noopener nofollow\">MediatR<\/a>. \u0412 go \u043d\u0435 \u043d\u0430\u0448\u0451\u043b \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u0432. \u0427\u0442\u043e-\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 <a href=\"https:\/\/docs.sqlc.dev\/en\/latest\/howto\/transactions.html\" rel=\"noopener nofollow\">sqlc<\/a>. \u0418\u043c\u0445\u043e \u044d\u0442\u043e \u043e\u0434\u043d\u0430 \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0442\u043e\u043b\u043a\u043e\u0432\u044b\u0445 \u043b\u0438\u0431 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414, \u043d\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u043f\u0443\u0433\u0430\u0435\u0442 \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043a\u0430\u043a \u044f\u0432\u043b\u0435\u043d\u0438\u0435.<\/p>\n<p><a class=\"anchor\" name=\"6\" id=\"6\"><\/a><\/p>\n<h2>6. CQRS (ver. 2)<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 <code>Handle<\/code>, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442:<\/p>\n<pre><code class=\"go\">package handlerimport (  \"model\"  \"context\"  \"command\")type Executor interface {RunCommand(ctx context.Context, command any) errorInTx(ctx context.Context, f func(ctx context.Context, e Executor) error) error}type CreateHandler struct {  db Executor}func (h *CreateHandler) Create(ctx context.Context, user model.User, order model.Order) error {  return h.db.InTx(ctx, func(ctx context.Context, tx Executor) error {    tx.RunCommand(ctx, command.SaveUser{User: user})    tx.RunCommand(ctx, command.SaveOrder{Order: order}))    return nil  })}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043d\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u0430 <code>Handle<\/code> \u0441 \u0442\u043e\u0440\u0447\u0430\u0449\u0438\u043c \u043d\u0430\u0440\u0443\u0436\u0443 \u0442\u0438\u043f\u043e\u043c <code>sql.Tx<\/code>. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0445\u0435\u043d\u0434\u043b\u0435\u0440:<\/p>\n<pre><code class=\"go\">package cqrsimport (  \"context\"  \"command\")type SaveUserHandler struct {}func (h *SaveUserHandler) Handle(ctx context.Context, query command.SaveUser) error {  \/\/ \u0442\u0443\u0442 \u043b\u043e\u0433\u0438\u043a\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044e\u0437\u0435\u0440\u0430 \u0432 \u0411\u0414}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u0430\u043d\u0434 \u0438 \u0438\u0445 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u043e\u0432. \u041f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u044e, \u043b\u0438\u0431\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e, \u043b\u0438\u0431\u043e \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043f\u0440\u0438 \u0441\u0431\u043e\u0440\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0439 \u0445\u0435\u043d\u0434\u043b\u0435\u0440 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u043c\u043c\u0430\u043d\u0434\u044b. \u041d\u0443 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>any<\/code> \u0432 \u043a\u043e\u0434\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432\u0441\u0435\u0433\u0434\u0430.<\/p>\n<p>\u0418\u0437 \u0432\u0441\u0435\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u044d\u0442\u043e\u0442 \u043c\u043d\u0435, \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445.<\/p>\n<p><a class=\"anchor\" name=\"7\" id=\"7\"><\/a><\/p>\n<h2>7. UOW (unit of work)<\/h2>\n<p>\u0427\u0435\u0441\u0442\u043d\u043e \u0441\u043a\u0430\u0436\u0443, \u0447\u0442\u043e \u044f \u044d\u0442\u0438\u043c \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u043c \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u043d\u0438 \u0432 go, \u043d\u0438 \u0432 C#. \u0412 \u043a\u043d\u0438\u0433\u0430\u0445 \u043f\u0440\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u044f \u0442\u043e\u0436\u0435 \u0435\u0433\u043e \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b (\u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u0431\u044b\u043b \u043e\u0431 \u044d\u0442\u043e\u043c). \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0434\u0435\u0441\u044c \u044f \u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0441\u044c \u043d\u0430 \u0447\u0430\u0442\u0433\u043f\u0442 \u0438 \u043c\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u0447\u0435\u043c\u0443-\u0442\u043e \u043d\u0435 \u0441\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u043d\u0435\u043d\u0438\u0438.<\/p>\n<p>\u041d\u0430 \u0445\u0430\u0431\u0440\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c hello_my_name_is_dany <a href=\"https:\/\/habr.com\/ru\/articles\/784186\/#comment_26326122\" rel=\"noopener nofollow\">\u043f\u0438\u0448\u0435\u0442<\/a>, \u0447\u0442\u043e UoW \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b\u0438 \u0434\u043b\u044f \u0448\u0430\u0440\u0438\u043d\u0433\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u043d\u0435\u043a\u0442\u0430 \u043c\u0435\u0436\u0434\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f\u043c\u0438, \u0430 \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 &#8212; \u044d\u0442\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u043f\u0440\u0438\u044f\u0442\u043d\u044b\u0439 \u0431\u043e\u043d\u0443\u0441.<\/p>\n<p>\u0424\u0430\u0443\u043b\u0435\u0440 \u043d\u0430 \u0441\u0432\u043e\u0451\u043c \u0441\u0430\u0439\u0442\u0435 \u043f\u0438\u0448\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<blockquote>\n<p>A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you&#8217;re done, it figures out everything that needs to be done to alter the database as a result of your work.<\/p>\n<\/blockquote>\n<p>\u0418\u043c\u0445\u043e \u0437\u0432\u0443\u0447\u0438\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u043b\u044b\u0432\u0447\u0430\u0442\u043e. \u0414\u0430 \u0438 \u043f\u0440\u043e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0441\u043a\u0430\u0437\u0430\u043d\u043e. \u0412 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435 \u0445\u043e\u0447\u0443 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e \u0432 \u0440\u0435\u0441\u0451\u0440\u0447 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 UoW, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443:<\/p>\n<pre><code class=\"go\">package handlerimport (  \"context\"  \"model\")type UsersRepository {  Save(ctx context.Context, user model.User)}type OrdersRepository {  Save(ctx context.Context, order model.Order)}type UnitOfWork interface {  func (u *UnitOfWork) GetUsersRepository() UsersRepository  func (u *UnitOfWork) GetOrdersRepository() OrdersRepository  func (u *UnitOfWork) Transaction(fn func() error)}type CreateHandler struct {  usersDb UsersRepository  ordersDb OrdersRepository  uow UnitOfWork}func (h *CreateHandler) Create(ctx context.Context, user model.User, order model.Order) error {  return h.uow.Transaction(func() {    h.uow.GetUsersRepository().Save(ctx, user)    h.uow.GetOrdersRepository().Save(ctx, order)    return nil  })}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u042f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 UnitOfWork, \u043d\u043e \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0442\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">package uowtype UoW struct {  users UsersRepository \/\/ \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441  orders OrdersRepository \/\/ \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441  db *slx.Db}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0441\u0451 \u0432 \u0442\u043e\u043c \u0436\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043e\u0442 <a href=\"https:\/\/habr.com\/ru\/articles\/784186\/#comment_26326122\" rel=\"noopener nofollow\">hello_my_name_is_dany<\/a>.<\/p>\n<p>\u041f\u043e \u0441\u0443\u0442\u0438 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0437\u044f\u043b\u0438 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u043e\u043c\u0435\u0440 4, \u043d\u043e \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0433\u0438\u0433\u0430\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0430\u0434\u0430\u043f\u0442\u0435\u0440 <code>UoW<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0445\u0440\u0430\u043d\u0438\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0432\u0441\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438. \u041c\u0438\u043d\u0443\u0441\u044b \u043f\u043e \u0441\u0443\u0442\u0438 \u0442\u0430\u043a\u0438\u0435 \u0436\u0435: god object, big ball of mud (\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u043e\u043c\u043e\u043a \u0433\u0440\u044f\u0437\u0438), \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0435 SOLID \u0438 \u043f\u0440. <\/p>\n<p><a class=\"anchor\" name=\"8\" id=\"8\"><\/a><\/p>\n<h2>8. Callback<\/h2>\n<p>\u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0434\u0440\u0443\u0433\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443. \u0422\u0435\u043f\u0435\u0440\u044c \u0432 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0435 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0437\u0430\u043b\u043e\u0447\u0438\u0442\u044c \u0435\u0433\u043e, \u0430 \u043f\u043e\u0442\u043e\u043c \u0434\u0435\u0430\u043a\u0442\u0438\u0432\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e, \u0435\u0441\u043b\u0438 \u043e\u043d \u043d\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0438\u043b \u043d\u0438 \u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u043a\u0443\u043f\u043a\u0438. \u0422\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"go\">package handlerimport (  \"context\"  \"model\")type UsersRepository interface {  SelectForUpdate(ctx context.Context, userID int, func(u *model.User)) error}type UpdateHandler struct {  users UsersRepository}func (h *CreateHandler) Create(ctx context.Context, userID int) error {  return h.users.SelectForUpdate(ctx, userID, func(u *model.User) {    if u.OrdersCount == 0 { \/\/ \u0434\u0430, \u043f\u043e\u043b\u0435 \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0437\u0430\u043a\u0430\u0437\u043e\u0432 \u043b\u0435\u0436\u0438\u0442 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 users, \u0442\u0430\u043a \u0443\u0436 \u0432\u044b\u0448\u043b\u043e      u.Active = false    }  })}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>SelectForUpdate<\/code> \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<ul>\n<li>\n<p>\u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 <code>SELECT FOR UPDATE<\/code> \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u044e\u0437\u0435\u0440\u0430<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043a\u043e\u043b\u0431\u044d\u043a \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0430<\/p>\n<\/li>\n<li>\n<p>\u0441\u0440\u0430\u0432\u043d\u0438\u0442, \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f \u043b\u0438 \u043d\u0430\u0448 \u044e\u0437\u0435\u0440, \u0438 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0430\u043f\u0434\u0435\u0439\u0442<\/p>\n<\/li>\n<\/ul>\n<p>\u0417\u0434\u0435\u0441\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0451, \u043d\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0445\u0440\u0443\u043f\u043a\u0438\u0439 \u0438\u0437 \u0432\u0441\u0435\u0445. <\/p>\n<p>\u041f\u043e \u043c\u0435\u0440\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u0435\u0439 \u0432 <code>model.User<\/code> \u043d\u0430\u0434\u043e \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0435, \u0447\u0442\u043e \u0433\u0434\u0435-\u0442\u043e \u0435\u0441\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u044e\u0437\u0435\u0440\u0430, \u0433\u0434\u0435 \u043d\u0430\u0434\u043e \u044d\u0442\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0430\u0442\u044c. \u0414\u0430, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u0438 \u0434\u0430\u0436\u0435 \u043d\u0430\u0432\u0435\u0441\u0438\u0442\u044c \u0435\u0451 \u043d\u0430 \u0445\u0443\u043a \u043f\u0435\u0440\u0435\u0434 \u043a\u043e\u043c\u043c\u0438\u0442\u043e\u043c, \u043d\u043e \u044d\u0442\u043e \u043e\u0449\u0443\u0449\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043e\u0432\u0435\u0440\u0445\u0435\u0434.<\/p>\n<p>\u041a \u0442\u043e\u043c\u0443 \u0436\u0435 \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043f\u043b\u043e\u0445\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u0430\u043c\u0438. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442:<\/p>\n<pre><code class=\"go\">type UserWithOrder struct {  User User  Orders []Order}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041d\u0430\u0434\u043e \u043b\u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043b\u043e\u0447\u0438\u0442\u044c \u0437\u0430\u043a\u0430\u0437\u044b? \u0415\u0441\u043b\u0438 \u0434\u0430, \u0442\u043e \u0434\u043e \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0441\u0435\u043b\u0435\u043a\u0442\u0430 \u044e\u0437\u0435\u0440\u0430? \u0410 \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u0432 \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0430\u0434\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043b\u043e\u043a\u043e\u0432? \u041d\u0443 \u0438 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u044f, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0434\u0440\u0443\u0433\u0443\u044e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432 \u044d\u0442\u043e\u0439 \u0436\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/p>\n<p>\u0423 \u043c\u0435\u043d\u044f \u0431\u044b\u043b \u043e\u043f\u044b\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435, \u0433\u0434\u0435 \u0432\u0441\u0435 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u044b \u0431\u044b\u043b\u0438 \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0435, \u0447\u0442\u043e \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0431\u044b\u043b\u0430 \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0430. \u0422\u0430\u043c \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u0431\u044b \u0437\u0430\u0448\u0451\u043b. \u041d\u043e \u0443\u0432\u044b, \u044d\u0442\u043e \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0443\u0437\u043a\u0438\u0439 \u043a\u0435\u0439\u0441.<\/p>\n<p><a class=\"anchor\" name=\"end\" id=\"end\"><\/a><\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u044f \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u043b, \u043d\u043e \u0432\u0441\u0435 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0435 \u044f \u0432\u0440\u043e\u0434\u0435 \u0431\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b. \u0427\u0435\u0441\u0442\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0432\u0441\u0451 \u0435\u0449\u0451 \u043d\u0435 \u0437\u043d\u0430\u044e, \u043a\u0430\u043a\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445 \u043b\u0443\u0447\u0448\u0435 \u0434\u0440\u0443\u0433\u0438\u0445. \u041c\u043d\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u0439 \u0438 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0431\u0435\u0437 \u043f\u0440\u043e\u0442\u0435\u043a\u0430\u043d\u0438\u044f \u0441\u043b\u043e\u0451\u0432, \u043d\u043e \u043d\u0435 \u043f\u043e\u0445\u043e\u0436\u0435, \u0447\u0442\u043e \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e.<\/p>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u0447\u0430\u0441\u0442\u043e \u043f\u0440\u043e\u0441\u044f\u0442 \u043e\u0431\u0440\u0430\u0442\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c \u0440\u0430\u0434\u0438 \u0442\u0440\u0430\u0444\u0438\u043a\u0430 \u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432. \u041c\u043d\u0435 \u043d\u0430 \u044d\u0442\u043e \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e, \u043d\u043e \u0432\u043e\u0442 \u043c\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0430 \u043c\u043d\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e. \u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432\u0430\u0448\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u044b, \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u0432 go.<\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1087238\/\">https:\/\/habr.com\/ru\/articles\/1087238\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0422\u0430\u043a \u0443\u0436 \u0432\u044b\u0448\u043b\u043e, \u0447\u0442\u043e \u044f \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u043d\u0435 \u043c\u043e\u0433\u0443 \u043f\u043e\u0434\u043e\u0431\u0440\u0430\u0442\u044c \u043b\u0443\u0447\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u0443 \u043e\u0431\u0437\u043e\u0440\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e. \u042f \u0440\u0430\u0437\u0431\u0435\u0440\u0443 \u0432\u0441\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0435 \u043c\u043d\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u0438 \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043b\u0443\u0447\u0448\u0438\u0439.\u0412\u0432\u043e\u0434\u043d\u044b\u0435\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0430\u043c\u044b\u0439 \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u043b\u044e\u0431\u043e\u0439 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0411\u0414. \u0423 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 &#8212;  \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0438 \u0437\u0430\u043a\u0430\u0437\u044b. \u041a\u0430\u0436\u0434\u0430\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0441\u0432\u043e\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u041c\u044b \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f (\u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e) \u043d\u0435 \u043c\u0435\u0448\u0430\u0442\u044c \u0441\u043b\u043e\u0439 \u0411\u0414 \u0441\u043e \u0441\u043b\u043e\u0435\u043c \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u0438 \u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c\u044b\u0439 \u043a\u043e\u0434.\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u043a\u043e\u0434\u0430 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u044b \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0435\u0440\u0435\u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0442\u044c.\u0421\u0442\u043e\u043b\u043a\u043d\u0443\u0432\u0448\u0438\u0441\u044c \u0441 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438, \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u0437 \u044d\u0442\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432:\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 \u0441\u043b\u043e\u0435 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438\u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439\u0410\u0433\u0440\u0435\u0433\u0430\u0442\u044b\u041e\u0434\u0438\u043d \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043d\u0430 \u0432\u0441\u0451CQRS (ver. 1)CQRS (ver. 2)UOW (unit of work)Callback\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u04351. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 \u0441\u043b\u043e\u0435 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0411\u0414 \u0438 \u044e\u0437\u0435\u0440\u0430, \u0438 \u0437\u0430\u043a\u0430\u0437. \u0422\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:package handlerimport (  &#171;database\/sql&#187;  &#171;model&#187;  &#171;context&#187;)type UsersRepository {  Save(ctx context.Context, user model.User, tx *sql.Tx)}type OrdersRepository {  Save(ctx context.Context, order model.Order, tx *sql.Tx)}type CreateHandler struct {  usersDb UsersRepository  ordersDb OrdersRepository  db *sql.Db}func (h *CreateHandler) Create(user model.User, order model.Order) error {  tx := h.db.Begin()  defer tx.Rollback()  h.usersDb.Save(user, tx)  h.ordersDb.Save(order, tx)  return tx.Commit()}\u041f\u043b\u044e\u0441 \u0443 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d: \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0439\u043c\u0451\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043b\u044e\u0431\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f.\u041d\u043e \u043c\u0438\u043d\u0443\u0441\u043e\u0432 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435:\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u043c\u044b \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0435 \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 Save \u0438\u0437 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0431\u0435\u0437 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438?\u0412 \u0441\u043b\u043e\u0439 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u043f\u0440\u043e\u0442\u0435\u043a\u043b\u0430 \u043b\u043e\u0433\u0438\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414\u0415\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u0435\u0445\u0430\u0442\u044c \u0441 database\/sql \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432 \u0441\u043b\u043e\u0435 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438\u0412\u0435\u043b\u0438\u043a\u0430 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u0442\u0430\u043a \u043a\u0430\u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0431\u044b\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c tx.Commit \u0438\u043b\u0438 tx.Rollback\u041f\u0440\u0438 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0438\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u043e \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0438\u0437\u043e\u043b\u044f\u0446\u0438\u0438 \u043a\u043e\u0434 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u0438\u043b\u044c\u043d\u043e \u043c\u0435\u043d\u0435\u0435 \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u043c\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u044f \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0438 \u0432\u0438\u0436\u0443 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445. \u0418 \u043f\u043e\u0434\u043e\u0437\u0440\u0435\u0432\u0430\u044e, \u0447\u0442\u043e \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u0431\u0443\u0434\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u0437\u0430\u0449\u0438\u0442\u043d\u0438\u043a\u043e\u0432 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430, \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0435 go \u043e\u0447\u0435\u043d\u044c \u043b\u044e\u0431\u044f\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0443.2. \u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431, \u0432\u0438\u0434\u0438\u043c\u043e, \u043d\u0435 \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u043d\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u044b \u0438\u0437 \u0430\u0432\u0438\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0441\u0432\u043e\u0439 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439. \u041e\u043d \u0445\u0440\u0430\u043d\u0438\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0438\u043c\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:package handlerimport (  &#171;model&#187;  &#171;context&#187;  &#171;github.com\/avito-tech\/go-transaction-manager\/trm\/v2\/manager&#187;)type UsersRepository {  Save(ctx context.Context, user model.User)}type OrdersRepository {  Save(ctx context.Context, order model.Order)}type CreateHandler struct {  usersDb UsersRepository  ordersDb OrdersRepository  txManager manager.Manager}func (h *CreateHandler) Create(ctx context.Context, user model.User, order model.Order) error {  \/\/ \u043c\u0435\u0442\u043e\u0434 Do \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0435\u0451 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442  \/\/ \u0435\u0441\u043b\u0438 \u043a\u043e\u043b\u0431\u044d\u043a \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443, \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f  return h.txManager.Do(ctx, func(ctx context.Context) error {    h.usersDb.Save(ctx, user)    h.ordersDb.Save(ctx, order)    return nil  })}\u0412\u043e\u043e\u0431\u0449\u0435 \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u043d\u0435 \u043a\u0440\u0438\u0442\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0445\u043e\u0442\u044c \u043e\u043d \u0438 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f. \u041a\u043e\u0434 \u0432\u0441\u0451 \u0435\u0449\u0451 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u0439, \u043f\u0440\u043e \u043e\u0442\u043a\u0430\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0434\u0443\u043c\u0430\u0442\u044c \u043d\u0435 \u043d\u0430\u0434\u043e, \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0438 \u0432\u043d\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439. \u041d\u043e \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043b\u0438\u0447\u043d\u043e \u043c\u0435\u043d\u044f \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043e\u0442\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u0442. \u0412 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 go \u043f\u043e \u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0432\u043e\u0434\u0443 \u0441\u043a\u0430\u0437\u0430\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435: Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions.\u0424\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a request-scoped data, \u043d\u043e \u0438\u043c\u0445\u043e \u044d\u0442\u043e \u043f\u0440\u0438\u0442\u044f\u0433\u0438\u0432\u0430\u043d\u0438\u0435 \u0437\u0430 \u0443\u0448\u0438.\u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438, \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u0432 \u043b\u044e\u0431\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u0443\u0434\u043e\u0431\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u043d\u0451\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u0425\u043e\u0442\u044f \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0435\u0433\u043e \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0446\u0435\u043b\u0435\u0439.3. \u0410\u0433\u0440\u0435\u0433\u0430\u0442\u044b\u041c\u044b \u0432\u0441\u0435 \u043f\u0440\u0438\u0432\u044b\u043a\u043b\u0438, \u0447\u0442\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0441\u0432\u043e\u0439 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439. \u0414\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f UsersRepository, \u0434\u043b\u044f \u0437\u0430\u043a\u0430\u0437\u043e\u0432 &#8212; OrdersRepository \u0438 \u0442. \u0434. \u041d\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u044d\u0442\u043e \u043d\u0435 \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0437\u043e\u043b\u043e\u0442\u043e\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u043d\u0430\u0440\u0443\u0448\u0430\u0442\u044c. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043f\u0440\u0430\u0432\u0435\u0434\u043b\u0438\u0432\u043e \u0437\u0430\u043c\u0435\u0442\u044f\u0442, \u0447\u0442\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043d\u0430\u0434\u043e \u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0435 \u043f\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c \u0432 \u0411\u0414, \u0430 \u043f\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c. \u0415\u0441\u043b\u0438 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u043a\u0430\u0437\u0430, \u0438 \u044e\u0437\u0435\u0440\u0430, \u0442\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0434\u0435\u043b\u0438\u0442\u044c \u044d\u0442\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043d\u0430 \u0434\u0432\u0435? \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0430\u0433\u0440\u0435\u0433\u0430\u0442, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0437\u0430\u043a\u0430\u0437\u0430 \u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0438 \u043d\u0430\u0448 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a:package handlerimport (  &#171;model&#187;  &#171;context&#187;)type UsersRepository {  Save(ctx context.Context, user model.UserWithOrder)}type CreateHandler struct {  usersDb UsersRepository}func (h *CreateHandler) Create(ctx context.Context, user model.User, order model.Order) error {  userWithOrder := model.NewUserWithOrder(user, order)  h.usersDb.Save(userWithOrder)  return nil}\u041c\u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434. \u042f \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0435\u043c\u0443, \u043d\u043e \u0442\u0443\u0442 \u0442\u043e\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u043d\u044e\u0430\u043d\u0441\u044b. \u0418\u043d\u043e\u0433\u0434\u0430 \u0432\u0441\u0451 \u0436\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f, \u0447\u0442\u043e \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0435 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0437\u0430\u043a\u0430\u0437 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0418 \u0442\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u043a\u0430\u043a-\u0442\u043e \u0448\u0430\u0440\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0437\u0430\u043a\u0430\u0437\u0430 \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f\u043c\u0438, \u043b\u0438\u0431\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043b\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u043d\u0430\u0435\u0442 \u043f\u0440\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438, \u043d\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u043f\u0440\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443. \u0412\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:package storageimport (  &#171;database\/sql&#187;  &#171;model&#187;  &#171;context&#187;)type UsersRepository {  Save(ctx context.Context, user model.User, tx *sql.Tx) }type OrdersRepository {  Save(ctx context.Context, order model.Order, tx *sql.Tx)}type Storage struct {}\/\/ \u0438\u0437 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434\/\/ \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0432 \u043d\u0451\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f func (s *Storage) Create(ctx context.Context, user model.UserWithOrder) error {  tx := h.db.Begin()  defer tx.Rollback()  h.usersDb.Save(user.User, tx)  h.ordersDb.Save(user.Order, tx)  return tx.Commit()}4. \u041e\u0434\u0438\u043d \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043d\u0430 \u0432\u0441\u0451\u0421\u0440\u0430\u0437\u0443 \u043e\u0433\u043e\u0432\u043e\u0440\u044e\u0441\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u044d\u043a\u0441\u0442\u0440\u0435\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443 \u043c\u043d\u043e\u0433\u0438\u0445 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043e\u0442\u0442\u043e\u0440\u0436\u0435\u043d\u0438\u0435. \u041d\u043e \u0443 \u043d\u0435\u0433\u043e \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0441\u0432\u043e\u0438\u0445 \u043f\u043b\u044e\u0441\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435 \u043c\u043e\u0433\u0443 \u043d\u0435 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u043d\u0451\u043c. \u0418\u0434\u0435\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f: \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043e\u0434\u0438\u043d \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043d\u0430 \u0432\u0441\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439:package handlerimport (  &#171;model&#187;  &#171;context&#187;)type Repository interface{  SaveUser(ctx context.Context, user model.User)  SaveOrder(ctx context.Context, order model.Order)  InTx(ctx context.Context, f func(ctx context.Context, tx Repository) error)}type CreateHandler struct {  db Repository}func (h *CreateHandler) Create(ctx context.Context, user model.User, order model.Order) error {  return h.db.InTx(ctx, func(ctx context.Context, tx Repository) error {    tx.SaveUser(ctx, user)    tx.SaveOrder(ctx, order)    return nil  })}\u0414\u0430, \u043c\u043d\u043e\u0433\u0438\u043c \u0442\u0430\u043a\u043e\u0435 \u043d\u0435 \u0437\u0430\u0439\u0434\u0451\u0442. \u041d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0443 \u043c\u043d\u043e\u0433\u0438\u0445 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u0441\u0440\u0430\u0437\u0443 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442 \u043c\u044b\u0441\u043b\u0438 \u043f\u0440\u043e god object, big ball of mud (\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u043e\u043c\u043e\u043a \u0433\u0440\u044f\u0437\u0438), \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0435 SOLID \u0438 \u043f\u0440. \u041d\u043e \u043c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0435 \u0442\u0430\u043a \u0443\u0436 \u0438 \u043f\u043b\u043e\u0445. \u042f \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0439\u043c\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0431\u0435\u0440\u0443\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445.\u041a\u0441\u0442\u0430\u0442\u0438, \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 ent, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043a\u043e\u0441\u0438\u0442 \u043f\u043e\u0434 entity framework.  5. CQRS (ver. 1)\u041c\u043e\u0436\u043d\u043e \u0447\u0443\u0442\u044c \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0438 \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u043c \u0432\u0441\u0435 \u043d\u0430\u0448\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0411\u0414 \u043d\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u044b (command) \u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b (query), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d executor:package cqrsimport (  &#171;model&#187;  &#171;context&#187;  &#171;command&#187;  &#171;database\/sql&#187;)type Command interface {Handle(ctx context.Context, conn *sql.Conn) error}type Query[T any] interface {Handle(ctx context.Context, conn *sql.Conn) (T, error)}type Executor interface {RunCommand(ctx context.Context, command Command) errorRunQuery(ctx context.Context, query Query[any]) (any, error)InTx(ctx context.Context, f func(ctx context.Context, e Executor) error) error}\u0418 \u0442\u043e\u0433\u0434\u0430 \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:package handlerimport (  &#171;model&#187;  &#171;context&#187;  &#171;command&#187;)type Executor interface{  RunCommand(ctx context.Context, command Command) error  InTx(ctx context.Context, f func(ctx context.Context, e Executor) error) error}type CreateHandler struct {  db Executor}func (h *CreateHandler) Create(ctx context.Context, user model.User, order model.Order) error {  return h.db.InTx(ctx, func(ctx context.Context, tx Executor) error {    tx.RunCommand(ctx, command.SaveUser{User: user})    tx.RunCommand(ctx, command.SaveOrder{Order: order}))    return nil  })}\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442 \u043e\u0434\u043d\u043e\u0433\u043e \u0433\u0438\u0433\u0430\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u0433\u0434\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c\u0438. \u041d\u043e \u043d\u0430\u0440\u0443\u0436\u0443 \u0442\u043e\u0440\u0447\u0430\u0442 \u043c\u0435\u0442\u043e\u0434\u044b Handle, \u0433\u0434\u0435 \u0432 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435 \u043d\u0430\u0440\u0443\u0436\u0443 \u0442\u043e\u0440\u0447\u0438\u0442 \u0442\u0438\u043f sql.Tx. \u0414\u0430, \u0444\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0435\u0433\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u043d\u043e \u0447\u0443\u0432\u0441\u0442\u0432\u043e \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0437\u0430\u0434\u0435\u0442\u043e.\u0412 C# \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0432\u0435\u0449\u0435\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 MediatR. \u0412 go \u043d\u0435 \u043d\u0430\u0448\u0451\u043b \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u0432. \u0427\u0442\u043e-\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 sqlc. \u0418\u043c\u0445\u043e \u044d\u0442\u043e \u043e\u0434\u043d\u0430 \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0442\u043e\u043b\u043a\u043e\u0432\u044b\u0445 \u043b\u0438\u0431 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414, \u043d\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u043f\u0443\u0433\u0430\u0435\u0442 \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043a\u0430\u043a \u044f\u0432\u043b\u0435\u043d\u0438\u0435.6. CQRS (ver. 2)\u0415\u0441\u043b\u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 Handle, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442:package handlerimport (  &#171;model&#187;  &#171;context&#187;  &#171;command&#187;)type Executor interface {RunCommand(ctx context.Context, command any) errorInTx(ctx context.Context, f func(ctx context.Context, e Executor) error) error}type CreateHandler struct {  db Executor}func (h *CreateHandler) Create(ctx context.Context, user model.User, order model.Order) error {  return h.db.InTx(ctx, func(ctx context.Context, tx Executor) error {    tx.RunCommand(ctx, command.SaveUser{User: user})    tx.RunCommand(ctx, command.SaveOrder{Order: order}))    return nil  })}\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043d\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u0430 Handle \u0441 \u0442\u043e\u0440\u0447\u0430\u0449\u0438\u043c \u043d\u0430\u0440\u0443\u0436\u0443 \u0442\u0438\u043f\u043e\u043c sql.Tx. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0445\u0435\u043d\u0434\u043b\u0435\u0440:package cqrsimport (  &#171;context&#187;  &#171;command&#187;)type SaveUserHandler struct {}func (h *SaveUserHandler) Handle(ctx context.Context, query command.SaveUser) error {  \/\/ \u0442\u0443\u0442 \u043b\u043e\u0433\u0438\u043a\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044e\u0437\u0435\u0440\u0430 \u0432 \u0411\u0414}\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u0430\u043d\u0434 \u0438 \u0438\u0445 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u043e\u0432. \u041f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u044e, \u043b\u0438\u0431\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-496268","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/496268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=496268"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/496268\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=496268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=496268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=496268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}