{"id":109,"date":"2012-06-21T18:12:20","date_gmt":"2012-06-21T14:12:20","guid":{"rendered":"http:\/\/infobs.athn.ru\/?p=109"},"modified":"2012-06-21T18:12:20","modified_gmt":"2012-06-21T14:12:20","slug":"primer-strukturyi-sayta-na-asp-net","status":"publish","type":"post","link":"https:\/\/infobase.athn.ru\/program\/2012\/06\/primer-strukturyi-sayta-na-asp-net\/","title":{"rendered":"\u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0441\u0430\u0439\u0442\u0430 \u043d\u0430 ASP.Net"},"content":{"rendered":"<p>\u0411\u0430\u0437\u043e\u0432\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441<\/p>\n<pre>using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\n\nnamespace Example.Data.Services\n{\n    public abstract class BaseService : IDisposable\n    {\n        protected ExampleContext context;\n\n        protected BaseService()\n        {\n            context = new ExampleContext();\n        }\n\n        public void Dispose()\n        {\n            context.Dispose();\n        }\n    }\n}<\/pre>\n<p><!--more--><br \/>\n\u0421\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/p>\n<pre>using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing Example.Data.Entities;\n\nnamespace Example.Data.Services\n{\n    public class UserService : BaseService\n    {\n        public UserService()\n            : base()\n        {\n         \n        }\n\n        public Account[] GetAllAccounts()\n        {\n            return this.context.Accounts.ToArray();\n        }\n\n        public Account GetAccountByEmail(string email)\n        {\n            return this.context.Accounts.SingleOrDefault(f =&gt; f.Email.Equals(email));\n        }\n\n        public int CreateNewAccount(string email, string password, string nickname)\n        {\n            var account = new Account\n            {\n                Email = email,\n                Password = password,\n                Nickname = nickname,\n                CreationDate = DateTime.Now\n            };\n            this.context.Accounts.Add(account);\n            this.context.SaveChanges();\n            return account.Id;\n        }\n\n        public void RemoveAccountByEmail(string email)\n        {\n            var user = this.context.Accounts.Single(f =&gt; f.Email.Equals(email));\n            this.context.Accounts.Remove(user);\n            this.context.SaveChanges();\n        }\n    }\n}<\/pre>\n<p>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440<\/p>\n<pre>using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Web;\nusing System.Web.Mvc;\nusing Example.Models;\nusing Example.Data.Services;\n\nnamespace Example.Controllers\n{\n    public class DevController : Controller\n    {\n        public ActionResult Index()\n        {\n            return View();\n        }\n\n        public ActionResult Accounts()\n        {\n            using (var userService = new UserService())\n            {\n                var model = new AccountsViewModel { Accounts = userService.GetAllAccounts() };\n                return View(model);\n            }\n        }\n\n        public ActionResult AddUser()\n        {\n            using (var userService = new UserService())\n            {\n                userService.CreateNewAccount(\n                    \"TestAccEmail\" + DateTime.Now.Ticks,\n                    \"TestAccPass\" + DateTime.Now.Ticks,\n                    \"TestAccNick\" + DateTime.Now.Ticks);\n                return RedirectToAction(\"Accounts\");\n            }\n        }\n\n        public ActionResult RemoveUser(string email)\n        {\n            using (var userService = new UserService())\n            {\n                userService.RemoveAccountByEmail(email);\n                return RedirectToAction(\"Accounts\");\n            }\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0411\u0430\u0437\u043e\u0432\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Example.Data.Services { public abstract class BaseService : IDisposable { protected ExampleContext context; protected BaseService() { context = new ExampleContext(); <a class=\"more-link\" href=\"https:\/\/infobase.athn.ru\/program\/2012\/06\/primer-strukturyi-sayta-na-asp-net\/\">Continue Reading \u2192<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,32],"tags":[140,268,282],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-aspnet","category-csharp","tag-arhitektura","tag-268","tag-struktura"],"_links":{"self":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":0,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"wp:attachment":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}