init lserp cs 5.0
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace Zhaizj.Framework.ORM {
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// email 批注,验证对象的属性是否是合法的 email
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class EmailAttribute : PatternAttribute {
|
||||
|
||||
public EmailAttribute() {
|
||||
Regexp = RegPattern.Email;
|
||||
}
|
||||
|
||||
public EmailAttribute( String msg ) {
|
||||
Regexp = RegPattern.Email;
|
||||
Message = msg;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Zhaizj.Framework.ORM {
|
||||
|
||||
/// <summary>
|
||||
/// 检查对象属性是否已经赋值,如果是字符串类型,empty的字符串也是不合法的
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class NotNullAttribute : ValidationAttribute {
|
||||
|
||||
public NotNullAttribute() {
|
||||
}
|
||||
|
||||
public NotNullAttribute( String msg ) {
|
||||
base.Message = msg;
|
||||
}
|
||||
|
||||
public override void Validate( String action, IEntity target, EntityPropertyInfo info, Result result ) {
|
||||
Object obj = target.get( info.Name );
|
||||
|
||||
Boolean isNull = false;
|
||||
if (info.Type == typeof( String )) {
|
||||
if (obj == null) {
|
||||
isNull = true;
|
||||
}
|
||||
else if (strUtil.IsNullOrEmpty( obj.ToString() )) {
|
||||
isNull = true;
|
||||
}
|
||||
}
|
||||
else if (obj == null) {
|
||||
isNull = true;
|
||||
}
|
||||
|
||||
if (isNull) {
|
||||
if (strUtil.HasText( this.Message )) {
|
||||
result.Add( this.Message );
|
||||
}
|
||||
else {
|
||||
EntityInfo ei = Entity.GetInfo( target );
|
||||
String str = "[" + ei.FullName + "] : property \"" + info.Name + "\" ";
|
||||
result.Add( str + "can not be null" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Zhaizj.Framework.ORM
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 检查对象属性是否已经赋值,如果是字符串类型,empty的字符串也是不合法的
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class NotZeroAttribute : ValidationAttribute
|
||||
{
|
||||
|
||||
public NotZeroAttribute()
|
||||
{
|
||||
}
|
||||
|
||||
public NotZeroAttribute(String msg)
|
||||
{
|
||||
base.Message = msg;
|
||||
}
|
||||
|
||||
public override void Validate(String action, IEntity target, EntityPropertyInfo info, Result result)
|
||||
{
|
||||
Object obj = target.get(info.Name);
|
||||
Boolean isInt = false;
|
||||
if (info.Type == typeof(int))
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
isInt = true;
|
||||
}
|
||||
else if (Convert.ToInt32(obj) == 0)
|
||||
{
|
||||
isInt = true;
|
||||
}
|
||||
}
|
||||
else if (Convert.ToInt32(obj) == 0)
|
||||
{
|
||||
isInt = true;
|
||||
}
|
||||
if (isInt)
|
||||
{
|
||||
if (strUtil.HasText(this.Message))
|
||||
{
|
||||
result.Add(this.Message);
|
||||
}
|
||||
else
|
||||
{
|
||||
EntityInfo ei = Entity.GetInfo(target);
|
||||
String str = "[" + ei.FullName + "] : property \"" + info.Name + "\" ";
|
||||
result.Add(str + "can not be null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Zhaizj.Framework.ORM {
|
||||
|
||||
/// <summary>
|
||||
/// 正则表达式批注,验证属性的对象是否符合指定的正则表达式
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class PatternAttribute : ValidationAttribute {
|
||||
|
||||
private String _regexp;
|
||||
|
||||
public PatternAttribute() {
|
||||
}
|
||||
|
||||
public PatternAttribute( String regexp ) {
|
||||
_regexp = regexp;
|
||||
}
|
||||
|
||||
public PatternAttribute( String regexp, String msg ) {
|
||||
_regexp = regexp;
|
||||
base.Message = msg;
|
||||
}
|
||||
|
||||
public String Regexp {
|
||||
get { return _regexp; }
|
||||
set { _regexp = value; }
|
||||
}
|
||||
|
||||
public override void Validate( String action, IEntity target, EntityPropertyInfo info, Result result ) {
|
||||
|
||||
if (!Regex.IsMatch( cvt.ToNotNull( target.get( info.Name ) ), this.Regexp, RegexOptions.Singleline )) {
|
||||
if (strUtil.HasText( this.Message )) {
|
||||
result.Add( this.Message );
|
||||
}
|
||||
|
||||
else {
|
||||
EntityInfo ei = Entity.GetInfo( target );
|
||||
String str = "[" + ei.FullName + "] : property \"" + info.Name + "\" ";
|
||||
result.Add( str + " is not match the format pattern : " + this.Regexp );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using Zhaizj.Framework.Data;
|
||||
using System.Data;
|
||||
using Zhaizj.Framework.Log;
|
||||
namespace Zhaizj.Framework.ORM {
|
||||
|
||||
/// <summary>
|
||||
/// 唯一性验证批注,验证对象的属性值是否在所有对象中是唯一的
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class UniqueAttribute : ValidationAttribute {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( UniqueAttribute ) );
|
||||
|
||||
public UniqueAttribute() {
|
||||
}
|
||||
|
||||
public UniqueAttribute( String msg ) {
|
||||
base.Message = msg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override void Validate( String action, IEntity target, EntityPropertyInfo info, Result result ) {
|
||||
Object obj = target.get( info.Name );
|
||||
|
||||
EntityInfo ei = Entity.GetInfo( target );
|
||||
int count = getCount( action, target, ei, info, obj );
|
||||
|
||||
if (count > 0) {
|
||||
if (strUtil.HasText( this.Message )) {
|
||||
result.Add( this.Message );
|
||||
}
|
||||
|
||||
else {
|
||||
String str = "[" + ei.FullName + "] : property \"" + info.Name + "\" ";
|
||||
result.Add( str + " should be unique, but it has been in database" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int getCount( String action, IEntity target, EntityInfo entityInfo, EntityPropertyInfo info, Object obj ) {
|
||||
|
||||
if (obj == null) return 1;
|
||||
|
||||
String usql;
|
||||
IDatabaseDialect dialect = entityInfo.Dialect;
|
||||
if (action.Equals( "update" )) {
|
||||
usql = String.Format( "select count(Id) from {0} where Id<>{3} and {1}={2}", entityInfo.TableName, info.ColumnName, dialect.GetParameter( info.Name ), target.Id );
|
||||
}
|
||||
else {
|
||||
usql = String.Format( "select count(Id) from {0} where {1}={2}", entityInfo.TableName, info.ColumnName, dialect.GetParameter( info.Name ) );
|
||||
}
|
||||
|
||||
logger.Info( LoggerUtil.SqlPrefix + " validate unique sql : " + usql );
|
||||
IDbCommand cmd = DataFactory.GetCommand( usql, DbContext.getConnection( entityInfo ) );
|
||||
DataFactory.SetParameter( cmd, info.ColumnName, obj );
|
||||
return cvt.ToInt( cmd.ExecuteScalar() );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Zhaizj.Framework.ORM {
|
||||
|
||||
/// <summary>
|
||||
/// 验证批注的抽象基类。如果要自定义验证批注,请通过继承此基类扩展
|
||||
/// </summary>
|
||||
[Serializable, AttributeUsage( AttributeTargets.Property )]
|
||||
public abstract class ValidationAttribute : Attribute {
|
||||
|
||||
private String _message;
|
||||
|
||||
/// <summary>
|
||||
/// 错误提示信息
|
||||
/// </summary>
|
||||
public String Message {
|
||||
get { return _message; }
|
||||
set { _message = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 多国语言化的错误提示信息
|
||||
/// </summary>
|
||||
public String Lang {
|
||||
set {
|
||||
_message = lang.get( value );
|
||||
}
|
||||
get { return _message; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 可以扩展的验证方法
|
||||
/// </summary>
|
||||
/// <param name="action">当前操作:update或insert</param>
|
||||
/// <param name="target">需要验证的实体对象</param>
|
||||
/// <param name="info">当前属性信息</param>
|
||||
/// <param name="result">验证结果</param>
|
||||
public abstract void Validate( String action, IEntity target, EntityPropertyInfo info, Result result );
|
||||
|
||||
public virtual void Validate( String action, Object target, String propertyName, Result result ) {
|
||||
if (target == null) return;
|
||||
IEntity entity = target as IEntity;
|
||||
if (entity == null) throw new NotImplementedException( "NotNullAttribute can only implement to IEntity. Currenty object's type:" + target.GetType().FullName );
|
||||
EntityPropertyInfo ep = Entity.GetInfo( entity ).GetProperty( propertyName );
|
||||
Validate( action, entity, ep, result );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user