一个简单的用户信息处理理组件
用户信息描述类
Code
using System;
using System.Collections.Generic;
using System.Text;
using Smark.Data.Mappings;
using Smark.Data.Validates;
namespace Component.Users
{
/*
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Users]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Users]
GO
CREATE TABLE [dbo].[Users] (
[UserID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[UserName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[FullName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[SysPassWord] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[Enabled] [bit] NULL ,
[CreateDate] [datetime] NULL ,
[EMail] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Address] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ZipCode] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Phone] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Region] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Country] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[City] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Remark] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Sex] [int] NULL
) ON [PRIMARY]
GO */
/// <summary>
/// 用户表
/// </summary>
[Table("Users")]
interface IUser
{
/// <summary>
/// 用户ID
/// </summary>
[ID]
[UID]
string UserID { get; set; }
/// <summary>
/// 用户名称
/// </summary>
[Column]
[NotNull("用户名不能为空!")]
[Length("5", "16", "用户名长度必须5-16个字符!")]
[Unique("该用户名已经给其他用户使用!")]
string UserName { get; set; }
/// <summary>
/// 姓名
/// </summary>
[Column]
string FullName { get; set; }
/// <summary>
/// 性别
/// </summary>
[DefaultEnum]
[Column]
SexType Sex { get; set; }
/// <summary>
/// 系统密码
/// </summary>
[Column]
[NotNull("密码不能为空!")]
[Length("5", "16", "密码长度必须5-16个字符!")]
[Encrypt]
string SysPassWord { get; set; }
/// <summary>
/// 有效性
/// </summary>
[Column]
[Enabled]
bool Enabled { get; set; }
/// <summary>
/// 录入日期
/// </summary>
[Column]
[NowDate]
DateTime CreateDate { get; set; }
/// <summary>
/// 联系邮件地址
/// </summary>
[Column]
[NotNull("邮件地址不能为空!")]
[EMail("非法邮件地址!")]
[Unique("该邮件地址已经给其他用户使用!")]
string EMail { get; set; }
/// <summary>
/// 联系地址
/// </summary>
[Column]
string Address { get; set; }
/// <summary>
/// 邮政编码
/// </summary>
[Column]
string ZipCode { get; set; }
/// <summary>
/// 联系电话
/// </summary>
[Column]
string Phone { get; set; }
/// <summary>
/// 所在区域
/// </summary>
[Column]
string Region { get; set; }
/// <summary>
/// 所在家国
/// </summary>
[Column]
string Country { get; set; }
/// <summary>
/// 所在城市
/// </summary>
[Column]
string City { get; set; }
/// <summary>
/// 备注
/// </summary>
[Column]
string Remark { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Smark.Data.Mappings;
using Smark.Data.Validates;
namespace Component.Users
{
/*
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Users]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Users]
GO
CREATE TABLE [dbo].[Users] (
[UserID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[UserName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[FullName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[SysPassWord] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[Enabled] [bit] NULL ,
[CreateDate] [datetime] NULL ,
[EMail] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Address] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ZipCode] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Phone] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Region] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Country] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[City] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Remark] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Sex] [int] NULL
) ON [PRIMARY]
GO */
/// <summary>
/// 用户表
/// </summary>
[Table("Users")]
interface IUser
{
/// <summary>
/// 用户ID
/// </summary>
[ID]
[UID]
string UserID { get; set; }
/// <summary>
/// 用户名称
/// </summary>
[Column]
[NotNull("用户名不能为空!")]
[Length("5", "16", "用户名长度必须5-16个字符!")]
[Unique("该用户名已经给其他用户使用!")]
string UserName { get; set; }
/// <summary>
/// 姓名
/// </summary>
[Column]
string FullName { get; set; }
/// <summary>
/// 性别
/// </summary>
[DefaultEnum]
[Column]
SexType Sex { get; set; }
/// <summary>
/// 系统密码
/// </summary>
[Column]
[NotNull("密码不能为空!")]
[Length("5", "16", "密码长度必须5-16个字符!")]
[Encrypt]
string SysPassWord { get; set; }
/// <summary>
/// 有效性
/// </summary>
[Column]
[Enabled]
bool Enabled { get; set; }
/// <summary>
/// 录入日期
/// </summary>
[Column]
[NowDate]
DateTime CreateDate { get; set; }
/// <summary>
/// 联系邮件地址
/// </summary>
[Column]
[NotNull("邮件地址不能为空!")]
[EMail("非法邮件地址!")]
[Unique("该邮件地址已经给其他用户使用!")]
string EMail { get; set; }
/// <summary>
/// 联系地址
/// </summary>
[Column]
string Address { get; set; }
/// <summary>
/// 邮政编码
/// </summary>
[Column]
string ZipCode { get; set; }
/// <summary>
/// 联系电话
/// </summary>
[Column]
string Phone { get; set; }
/// <summary>
/// 所在区域
/// </summary>
[Column]
string Region { get; set; }
/// <summary>
/// 所在家国
/// </summary>
[Column]
string Country { get; set; }
/// <summary>
/// 所在城市
/// </summary>
[Column]
string City { get; set; }
/// <summary>
/// 备注
/// </summary>
[Column]
string Remark { get; set; }
}
}
用户操作逻辑类
Code
using System;
using System.Collections.Generic;
using System.Text;
using Smark.Data;
namespace Component.Users
{
public class UserService
{
static object LOCK_ADD = new object();
public UserAccessState Add(User user)
{
lock (LOCK_ADD)
{
user.EntityState._Loaded = false;
user.Save();
return UserAccessState.处理成功;
}
}
public User GetByID(string userid)
{
return GetByExp(User.userID == userid);
}
public User GetByName(string username)
{
return GetByExp(User.userName == username);
}
public User GetByEMail(string email)
{
return GetByExp(User.eMail == email);
}
private User GetByExp(Expression exp)
{
return exp.ListFirst<User>();
}
public IList<User> List(string username, string email, Smark.Data.Region region,out int recordcount)
{
Expression exp = null;
if (!string.IsNullOrEmpty(username))
exp &= User.userName.Like(username + "%");
if (!string.IsNullOrEmpty(email))
exp &= User.eMail.Like(email + "%");
recordcount = exp.Count<User>();
return exp.List<User>(region);
}
public void Enabled(bool enabled, params string[] userid)
{
Expression exp = User.userID == userid;
exp.Edit<User>(
User.enabled.NewValue(enabled));
}
public User Login(string username, string pwd)
{
Expression exp = User.userName == username;
User user = exp.ListFirst<User>();
if (user == null)
return null;
if (user.SysPassWord.ToLower() == pwd.ToLower())
return user;
return null;
}
public UserAccessState ChangePwd(string username, string opwd, string npwd)
{
Expression exp = User.userName == username;
User user = exp.ListFirst<User>();
if (user == null)
return UserAccessState.用户名或密码不正确;
if (user.SysPassWord.ToLower() != opwd.ToLower())
return UserAccessState.用户名或密码不正确;
user.SysPassWord = npwd;
user.Save();
return UserAccessState.处理成功;
}
public void ChangePwd(string npwd,params string[] userid)
{
Expression exp = User.userID == userid;
exp.Edit<User>(
User.sysPassWord.NewValue(npwd));
}
}
public enum UserAccessState
{
处理成功,
用户名已经存,
邮件已经被其他用户使用,
用户名或密码不正确
}
public class UserServiceException : Exception
{
public UserServiceException() { }
public UserServiceException(string err) : base(err) { }
public UserServiceException(string err, Exception baseexc) : base(err, baseexc) { }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Smark.Data;
namespace Component.Users
{
public class UserService
{
static object LOCK_ADD = new object();
public UserAccessState Add(User user)
{
lock (LOCK_ADD)
{
user.EntityState._Loaded = false;
user.Save();
return UserAccessState.处理成功;
}
}
public User GetByID(string userid)
{
return GetByExp(User.userID == userid);
}
public User GetByName(string username)
{
return GetByExp(User.userName == username);
}
public User GetByEMail(string email)
{
return GetByExp(User.eMail == email);
}
private User GetByExp(Expression exp)
{
return exp.ListFirst<User>();
}
public IList<User> List(string username, string email, Smark.Data.Region region,out int recordcount)
{
Expression exp = null;
if (!string.IsNullOrEmpty(username))
exp &= User.userName.Like(username + "%");
if (!string.IsNullOrEmpty(email))
exp &= User.eMail.Like(email + "%");
recordcount = exp.Count<User>();
return exp.List<User>(region);
}
public void Enabled(bool enabled, params string[] userid)
{
Expression exp = User.userID == userid;
exp.Edit<User>(
User.enabled.NewValue(enabled));
}
public User Login(string username, string pwd)
{
Expression exp = User.userName == username;
User user = exp.ListFirst<User>();
if (user == null)
return null;
if (user.SysPassWord.ToLower() == pwd.ToLower())
return user;
return null;
}
public UserAccessState ChangePwd(string username, string opwd, string npwd)
{
Expression exp = User.userName == username;
User user = exp.ListFirst<User>();
if (user == null)
return UserAccessState.用户名或密码不正确;
if (user.SysPassWord.ToLower() != opwd.ToLower())
return UserAccessState.用户名或密码不正确;
user.SysPassWord = npwd;
user.Save();
return UserAccessState.处理成功;
}
public void ChangePwd(string npwd,params string[] userid)
{
Expression exp = User.userID == userid;
exp.Edit<User>(
User.sysPassWord.NewValue(npwd));
}
}
public enum UserAccessState
{
处理成功,
用户名已经存,
邮件已经被其他用户使用,
用户名或密码不正确
}
public class UserServiceException : Exception
{
public UserServiceException() { }
public UserServiceException(string err) : base(err) { }
public UserServiceException(string err, Exception baseexc) : base(err, baseexc) { }
}
}