Пример структуры сайта на ASP.Net

Базовый сервис

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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();
        }
 
        public void Dispose()
        {
            context.Dispose();
        }
    }
}


Сервис пользователей

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Example.Data.Entities;
 
namespace Example.Data.Services
{
    public class UserService : BaseService
    {
        public UserService()
            : base()
        {
 
        }
 
        public Account[] GetAllAccounts()
        {
            return this.context.Accounts.ToArray();
        }
 
        public Account GetAccountByEmail(string email)
        {
            return this.context.Accounts.SingleOrDefault(f => f.Email.Equals(email));
        }
 
        public int CreateNewAccount(string email, string password, string nickname)
        {
            var account = new Account
            {
                Email = email,
                Password = password,
                Nickname = nickname,
                CreationDate = DateTime.Now
            };
            this.context.Accounts.Add(account);
            this.context.SaveChanges();
            return account.Id;
        }
 
        public void RemoveAccountByEmail(string email)
        {
            var user = this.context.Accounts.Single(f => f.Email.Equals(email));
            this.context.Accounts.Remove(user);
            this.context.SaveChanges();
        }
    }
}

Контроллер

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Example.Models;
using Example.Data.Services;
 
namespace Example.Controllers
{
    public class DevController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
 
        public ActionResult Accounts()
        {
            using (var userService = new UserService())
            {
                var model = new AccountsViewModel { Accounts = userService.GetAllAccounts() };
                return View(model);
            }
        }
 
        public ActionResult AddUser()
        {
            using (var userService = new UserService())
            {
                userService.CreateNewAccount(
                    "TestAccEmail" + DateTime.Now.Ticks,
                    "TestAccPass" + DateTime.Now.Ticks,
                    "TestAccNick" + DateTime.Now.Ticks);
                return RedirectToAction("Accounts");
            }
        }
 
        public ActionResult RemoveUser(string email)
        {
            using (var userService = new UserService())
            {
                userService.RemoveAccountByEmail(email);
                return RedirectToAction("Accounts");
            }
        }
    }
}

Оставить комментарий


Примечание - Вы можете использовать эти HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>