Skip to content

Commit

Permalink
Update demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
shuxinqin committed Jan 6, 2019
1 parent 7acd501 commit 156f87d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 21 deletions.
53 changes: 53 additions & 0 deletions src/ChloeDemo/MappingType`.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Chloe.Infrastructure
{
public class MappingType<T> : MappingTypeBase
{
DbType _dbType;
public MappingType()
{
}
public MappingType(DbType dbType)
{
this._dbType = dbType;
}
public override Type Type
{
get
{
return typeof(T);
}
}
public override DbType DbType
{
get
{
return this._dbType;
}
}
public override IDbDataParameter CreateDataParameter(IDbCommand cmd, DbParam param)
{
return base.CreateDataParameter(cmd, param);
}
public override object ReadFromDataReader(IDataReader reader, int ordinal)
{
if (reader.IsDBNull(ordinal))
return null;

var value = reader.GetValue(ordinal);

//数据库字段类型与属性类型不一致,则转换类型
if (value.GetType() != this.Type)
{
value = Convert.ChangeType(value, this.Type);
}

return value;
}
}
}
18 changes: 17 additions & 1 deletion src/ChloeDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Chloe.Infrastructure.Interception;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -16,7 +17,7 @@ public static void Main(string[] args)
IDbCommandInterceptor interceptor = new DbCommandInterceptor();
DbConfiguration.UseInterceptors(interceptor);

DbConfiguration.UseMappingType(new String_MappingType());
RegisterMappingType();

/* fluent mapping */
DbConfiguration.UseTypeBuilders(typeof(UserMap));
Expand All @@ -27,5 +28,20 @@ public static void Main(string[] args)
PostgreSQLDemo.Run();
OracleDemo.Run();
}

/// <summary>
/// 注册映射类型。当数据库字段类型与属性类型不一致时,映射时自动将数据类型转换成与属性类型一致的类型。
/// </summary>
static void RegisterMappingType()
{
DbConfiguration.UseMappingType(new String_MappingType());
DbConfiguration.UseMappingType(new MappingType<int>(DbType.Int32));
DbConfiguration.UseMappingType(new MappingType<long>(DbType.Int64));
DbConfiguration.UseMappingType(new MappingType<Int16>(DbType.Int16));
DbConfiguration.UseMappingType(new MappingType<byte>(DbType.Byte));
DbConfiguration.UseMappingType(new MappingType<double>(DbType.Double));
DbConfiguration.UseMappingType(new MappingType<float>(DbType.Single));
DbConfiguration.UseMappingType(new MappingType<decimal>(DbType.Decimal));
}
}
}
22 changes: 2 additions & 20 deletions src/ChloeDemo/String_MappingType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,10 @@

namespace ChloeDemo
{
class String_MappingType : MappingTypeBase, IMappingType
class String_MappingType : MappingType<string>, IMappingType
{
public override Type Type
public String_MappingType() : base(DbType.String)
{
get
{
return typeof(string);
}
}
public override DbType DbType
{
get
{
return DbType.String;
}
}
public override IDbDataParameter CreateDataParameter(IDbCommand cmd, DbParam param)
{
Expand Down Expand Up @@ -95,12 +84,5 @@ public override IDbDataParameter CreateDataParameter(IDbCommand cmd, DbParam par

return parameter;
}
public override object ReadFromDataReader(IDataReader reader, int ordinal)
{
if (reader.IsDBNull(ordinal))
return null;

return reader.GetString(ordinal);
}
}
}

0 comments on commit 156f87d

Please sign in to comment.