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

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

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();
        }
    }
}


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

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();
        }
    }
}

Контроллер

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");
            }
        }
    }
}

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *