<repo_name>liangl/Demo<file_sep>/Keyyum/Keyyum/Keyyum.DAL/UserRepository.cs
﻿using Keyyum.IDAL;
using Keyyum.Models;
using System.Linq;


namespace Keyyum.DAL
{
    class UserRepository:BaseRepository<User>,InterfaceUserRepository 
    {
        
    }
}
<file_sep>/Keyyum/Keyyum/Keyyum.BLL/UserService.cs
﻿using Keyyum.DAL;
using Keyyum.IBLL;
using Keyyum.Models;
using System.Linq;


namespace Keyyum.BLL
{
    public class UserService:BaseService<User>,InterfaceUserService
    {
        public UserService() : base(RepositoryFactory.UserRepository) { }
        public bool Exist(string userName)
        {
            return CurrentRepository.Exist(u => u.UserName == userName);
        }

        public User Find(System.Guid  userID)
        {
            return CurrentRepository.Find(u => u.UserID ==userID );
        }

        public User Find(string userName)
        {
            return CurrentRepository.Find(u => u.UserName == userName);
        }

        public IQueryable<User> FindPageList(int pageIndex, int pageSize, out int totalRecord, int order)
        {
            bool _isAsc = true;
            string _orderName = string.Empty;
            switch (order)
            {
                case 0:
                    _isAsc = true;
                    _orderName = "UserID";
                    break;
                case 1:
                    _isAsc = false;
                    _orderName = "UserID";
                    break;
                case 2:
                    _isAsc = true;
                    _orderName = "RegistrationTime";
                    break;
                case 3:
                    _isAsc = false;
                    _orderName = "RegistrationTime";
                    break;
                case 4:
                    _isAsc = true ;
                    _orderName = "LoginTime";
                    break;
                case 5:
                    _isAsc = false ;
                    _orderName = "LoginTime";
                    break;
                default :
                    _isAsc = false ;
                    _orderName = "UserID";
                    break;                   
            }
            return CurrentRepository.FindPageList(pageIndex, pageSize, out totalRecord, u => true, _orderName, _isAsc);
        }
    }
}
<file_sep>/Keyyum/Keyyum/Keyyum.IDAL/InterfaceBaseRepository.cs
﻿using System;
using System.Linq;
using System.Linq.Expressions;


namespace Keyyum.IDAL
{
    /// <summary>
    /// 接口基类
    /// <remarks >
    /// 创建：2014.03.17<br/>
    /// 修改：
    /// </remarks>
    /// </summary>
    /// <typeparam name="T">类型</typeparam>
    public interface InterfaceBaseRepository<T>
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>添加后的数据实体</returns>
        T Add(T entity);

        /// <summary>
        /// 查询记录数
        /// </summary>
        /// <param name="predicate">条件表达式</param>
        /// <returns>记录数</returns>
        int Count(Expression<Func<T, bool>> predicate);

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>是否成功</returns>
        bool Update(T entity);

        bool Delete(T entity);

        bool Exist(Expression<Func<T,bool>> anyLambda);

        T Find(Expression<Func<T, bool>> whereLambda);

        IQueryable<T> FindList(Expression<Func<T, bool>> whereLamdba,string orderName, bool isAsc);

        IQueryable<T> FindPageList(int pageIndex, int pageSize, out int totalRecord, Expression<Func<T, bool>> whereLamdba,string orderName, bool isAsc);

    }
}
<file_sep>/Keyyum/Keyyum/Keyyum.BLL/BaseService.cs
﻿using Keyyum.IBLL;
using Keyyum.IDAL;

namespace Keyyum.BLL
{
    public abstract class BaseService<T>:InterfaceBaseService<T> where T:class 
    {
       // private IDAL.InterfaceUserRepository interfaceUserRepository;

        protected InterfaceBaseRepository<T> CurrentRepository { get; set; }

        public BaseService(InterfaceBaseRepository<T> currentRepository) { CurrentRepository = currentRepository; }

        //public BaseService(IDAL.InterfaceUserRepository interfaceUserRepository)
        //{
        //    // TODO: Complete member initialization
        //    this.interfaceUserRepository = interfaceUserRepository;
        //}
        public T Add(T entity)
        {
            return CurrentRepository.Add(entity);
        }

        public bool Update(T entity)
        {
            return CurrentRepository.Update(entity);
        }

        public bool Delete(T entity)
        {
           return  CurrentRepository.Delete(entity);
        }
    }
}
<file_sep>/Keyyum/Keyyum/Keyyum.Models/User.cs
﻿using System;
using System.ComponentModel.DataAnnotations;

namespace Keyyum.Models
{
    /// <summary>
    /// 用户模型
    /// <remarks >
    /// 创建：2014年3月17日
    /// 修改：
    /// </remarks>
    /// </summary>
   public  class User
    {
       [Key]
       public Guid UserID { get; set; }

       /// <summary>
       /// 用户名
       /// </summary>
       [Required(ErrorMessage = "必填")]
       [StringLength(20, MinimumLength = 4, ErrorMessage = "{1}到{0}个字符")]
       [Display(Name = "用户名")]
       public string UserName { get; set; }
      
       /// <summary>
       /// 用户组ID
       /// </summary>
       //[Required(ErrorMessage = "必填")]
       //[Display(Name = "用户组ID")]
//       public Guid GroupID { get; set; }
       
       /// <summary>
       /// 显示名称
       /// </summary>
       [Required(ErrorMessage = "必填")]
       [StringLength(20, MinimumLength = 2, ErrorMessage = "{1}到{0}个字符")]
       [Display(Name = "显示名")]
       public string DisplayName { get; set; }
       
       /// <summary>
       /// 密码
       /// </summary>
       [Required(ErrorMessage = "必填")]
       [Display(Name = "密码")]
       [StringLength(20, MinimumLength = 6, ErrorMessage = "密码必须为{1}到{0}个字符")]
       [DataType(DataType.Password)]
       public string Password { get; set; }

       /// <summary>
       /// 邮箱
       /// </summary>
       /// 
       [Required(ErrorMessage = "必填")]
       [Display(Name = "邮箱")]
       [DataType(DataType.EmailAddress)]
       public string Email { get; set; }
        
       /// <summary>
       /// 用户状态<br/>
       /// 0正常，1锁定，2未通过邮件验证，3未通过管理员
       /// </summary>
       public int Status { get; set; }

       /// <summary>
       /// 注册时间
       /// </summary>
       public DateTime RegistrationTime { get; set; }

       /// <summary>
       /// 上次登陆时间
       /// </summary>
       public DateTime LoginTime { get; set; }

       /// <summary>
       /// 上次登陆IP地址
       /// </summary>
       /// 
       [Display(Name="上次登陆的IP地址")]
       public string LoginIP { get; set; }

       public virtual Role Group { get; set; }


    }
}
<file_sep>/Keyyum/Keyyum/Keyyum.DAL/RepositoryFactory.cs
﻿using Keyyum.IDAL;

namespace Keyyum.DAL
{
    public static class RepositoryFactory
    {
        public static InterfaceUserRepository UserRepository { get { return new UserRepository(); } }
    }
}
<file_sep>/Keyyum/Keyyum/Keyyum.IBLL/InterfaceBaseService.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Keyyum.IBLL
{
    public interface InterfaceBaseService<T> where T:class 
    {
        T Add(T entity);
        bool Update(T entity);

        bool Delete(T entity);
    }
}
<file_sep>/Keyyum/Keyyum/Keyyum.IDAL/InterfaceUserRepository.cs
﻿using Keyyum.Models;

namespace Keyyum.IDAL
{
    /// <summary>
    /// 用户接口
    /// <remarks >创建：2014.03.17</remarks>
    /// </summary>
    public interface  InterfaceUserRepository:InterfaceBaseRepository<User>
    {
    }
}
<file_sep>/Keyyum/Keyyum/Keyyum.DAL/ContextFactory.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace Keyyum.DAL
{
    public class ContextFactory
    {
        public static KeyyumDbContext GetCurrentContext()
        {
            KeyyumDbContext _nContext = CallContext.GetData("KeyyumContext") as KeyyumDbContext;
            if (_nContext == null)
            {
                _nContext = new KeyyumDbContext();
                CallContext.SetData("KeyyumContext", _nContext);
            }
            return _nContext;
        }
    }
}
<file_sep>/Keyyum/Keyyum/Keyyum.DAL/KeyyumDbContext.cs
﻿using Keyyum.Models;
using System.Data.Entity;


namespace Keyyum.DAL
{
    public class KeyyumDbContext:DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<UserConfig> UserConfig { get; set; }

        public DbSet<UserRoleRelation> UserRoleRelations { get; set; }
        public KeyyumDbContext()
            : base("DefaultConnection")
        {
            Database.CreateIfNotExists();
        }
    }
}
