服务层,设计时添加了接口与实现,而面向接口编程也是好处多多。来上图看服务层结构
Zy.Xn.IServices是接口,Zy.Xn.Services是接口的实现,在对应的项目下建立了System文件夹,系统框架的方法放在此文件夹下,所有接口服务需要继承 IService 类(依赖注入释放资源),在Zy.Xn.Service的实现类中需要继承 ServiceBase 类(提供EF context上下文)
在项目中很大的好处是,不需要知道具体业务,在页面设计未出前,就能直接开发 增、删、改方法,缩短整体开发时间
描述完它们的作用后,上一段用户接口与服务的代码:
IUserService:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zy.Utility.Core;
using Zy.Xn.Model.System;
namespace Zy.Xn.IServices
{
public interface IUserService : IService
{
/// <summary>
/// 创建用户
/// </summary>
/// <param name="user"></param>
/// <returns>用户Id</returns>
int CreateUser(User user);
/// <summary>
/// 修改用户
/// </summary>
/// <param name="user"></param>
void UpdateUser(User user);
/// <summary>
/// 删除用户
/// </summary>
/// <param name="id"></param>
void DeleteUser(int id);
/// <summary>
/// 获取用户
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
User GetUser(int id);
/// <summary>
/// 获取用户
/// </summary>
/// <param name="loginUserName"></param>
/// <returns></returns>
User GetUser(string loginUserName);
/// <summary>
/// 获取组织机构下的用户
/// </summary>
/// <param name="deptId"></param>
/// <returns></returns>
IEnumerable<User> GetUsers(int deptId);
/// <summary>
/// 获取组织机构下的用户列表
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
PageModel<User> GetPageUsers(UserQuery query);
}
}
UserService:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zy.Utility.Core;
using Zy.Xn.IServices;
using Zy.Xn.Model.System;
namespace Zy.Xn.Services
{
public class UserService : ServiceBase, IUserService
{
/// <summary>
/// 创建用户
/// </summary>
/// <param name="user"></param>
public int CreateUser(User user)
{
user.LoginEncryptKey = "key";
user.IsDel = false;
user.CreateTime = DateTime.Now;
context.Users.Add(user);
context.SaveChanges();
return user.Id;
}
/// <summary>
/// 修改用户
/// </summary>
/// <param name="user"></param>
public void UpdateUser(User user)
{
var userModel = context.Users.FirstOrDefault(item => item.Id == user.Id);
if (userModel != null)
{
userModel.UserName = user.UserName;
userModel.UserNumber = user.UserNumber;
userModel.LoginUserName = user.LoginUserName;
userModel.LoginPassword = user.LoginPassword;
userModel.LoginEncryptKey = user.LoginEncryptKey;
userModel.Sex = user.Sex;
userModel.State = user.State;
userModel.SortIndex = user.SortIndex;
context.SaveChanges();
}
}
/// <summary>
/// 删除用户
/// </summary>
/// <param name="id"></param>
public void DeleteUser(int id)
{
var user = context.Users.FirstOrDefault(item => item.Id == id);
if (user != null)
{
//user.IsDel = true;
context.Users.Remove(user);
context.SaveChanges();
//删除用户角色信息
var roleUsers = context.RoleUsers.Where(item => item.UserId == id).ToArray();
context.RoleUsers.RemoveRange(roleUsers);
context.SaveChanges();
}
}
/// <summary>
/// 获取用户
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public User GetUser(int id)
{
return context.Users.FirstOrDefault(item => item.Id == id);
}
/// <summary>
/// 获取用户
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public User GetUser(string loginUserName)
{
return context.Users.FirstOrDefault(item => item.LoginUserName == loginUserName);
}
/// <summary>
/// 获取组织机构下的用户
/// </summary>
/// <param name="deptId"></param>
/// <returns></returns>
public IEnumerable<User> GetUsers(int deptId)
{
return context.Users.Where(item => item.DeptId == deptId).ToArray();
}
/// <summary>
/// 获取组织机构下的用户列表
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public PageModel<User> GetPageUsers(UserQuery query)
{
var users = context.Users.Where(item => item.DeptId == query.DeptId && item.IsDel == false);
if (!string.IsNullOrWhiteSpace(query.UserName))
{
users = users.Where(item => item.UserName.Contains(query.UserName));
}
if (!string.IsNullOrWhiteSpace(query.Sex))
{
var sex = (User.UserSexs)Convert.ToInt32(query.Sex);
users = users.Where(item => item.Sex == sex);
}
if (!string.IsNullOrWhiteSpace(query.State))
{
var state = (User.UserStates)Convert.ToInt32(query.State);
users = users.Where(item => item.State == state);
}
int total = users.Count();
users = users.OrderByDescending(item => item.Id).Skip(query.PageSize * (query.PageNo - 1)).Take(query.PageSize);
var page = new PageModel<User>()
{
Total = total,
Data = users.ToArray()
};
return page;
}
}
}
注意:以上只是编写了接口和实现,那怎样在控制器中调用接口,接口自动调用实现方法获取结果呢,这就需要用到autofac依赖注入了,不对之处欢迎指正,谢谢