Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzhiyuan committed May 15, 2024
1 parent 16b807e commit 1a9d338
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 297 deletions.
8 changes: 6 additions & 2 deletions src/NuClear.Dapper.MySql/MySqlRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

namespace NuClear.Dapper.MySql
{
public abstract class MySqlRepositoryBase<T> : RepositoryBase<T>
where T : IEntity
public abstract class MySqlRepositoryBase<TKey, TEntity> : RepositoryBase<TKey, TEntity>
where TEntity : IEntity<TKey>
{
protected override IAggrQuerySqlParser AggrQuerySqlParser => new MySqlAggrQuerySqlParser();
protected override ICriterionSqlParser CriterionSqlParser => new MySqlCriterionSqlParser();
protected override ISqlParser SqlParser => new MySqlSqlParser();
}
public abstract class MySqlRepositoryBaseWithLongKey<TEntity> : MySqlRepositoryBase<long, TEntity>
where TEntity : IEntityWithLongKey
{
}
}
8 changes: 6 additions & 2 deletions src/NuClear.Dapper.SqlServer/SqlServerRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

namespace NuClear.Dapper.SqlServer
{
public abstract class SqlServerRepositoryBase<T> : RepositoryBase<T>
where T : IEntity
public abstract class SqlServerRepositoryBase<TKey, TEntity> : RepositoryBase<TKey, TEntity>
where TEntity : IEntity<TKey>
{
protected override IAggrQuerySqlParser AggrQuerySqlParser => new SqlServerAggrQuerySqlParser();
protected override ICriterionSqlParser CriterionSqlParser => new SqlServerCriterionSqlParser();
protected override ISqlParser SqlParser => new SqlServerSqlParser();
}
public abstract class SqlServerRepositoryBaseWithLongKey<TEntity> : SqlServerRepositoryBase<long, TEntity>
where TEntity : IEntityWithLongKey
{
}
}
8 changes: 2 additions & 6 deletions src/NuClear.Dapper.UnitTest/TestOrder.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NuClear.Dapper.UnitTest
namespace NuClear.Dapper.UnitTest
{
public class TestOrder : IEntity
{
public string Id { get; set; }
public long Id { get; set; }
}
}
120 changes: 3 additions & 117 deletions src/NuClear.Dapper/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -1,86 +1,21 @@
using NuClear.Snowflake;
using System;
using System;

namespace NuClear.Dapper
{
/// <summary>
/// 所有实体的基类
/// </summary>
public class BaseEntity : IEntity
{
public BaseEntity()
{
Id = Guid.NewGuid().ToString("N");

CreateTime = DateTime.Now;
LastUpdateTime = DateTime.Now;
}

public string Id { get; set; }

/// <summary>
/// 数据的创建时间
/// </summary>
public DateTime CreateTime { get; set; }

/// <summary>
/// 最后更新时间
/// </summary>
public DateTime LastUpdateTime { get; set; }
}

public class BaseEntity<T> : IEntity
public class BaseEntity<TKey> : IEntity<TKey>
{
public BaseEntity()
{
CreateTime = DateTime.Now;
LastUpdateTime = DateTime.Now;
}

public T Id { get; set; }

/// <summary>
/// 数据的创建时间
/// </summary>
public DateTime CreateTime { get; set; }

/// <summary>
/// 最后更新时间
/// </summary>
public DateTime LastUpdateTime { get; set; }
string IEntity.Id
{
get
{
return this.Id.ToString();
}
set
{
T ret = (T)Convert.ChangeType("-1", typeof(T));

try
{
ret = (T)Convert.ChangeType(value, typeof(T));
}
catch { }

this.Id = ret;
}
}
}

public class BaseEntityNum : IEntity
{
public BaseEntityNum()
{
Id = IdGenerator.NextId();

CreateTime = DateTime.Now;
LastUpdateTime = DateTime.Now;
}
public TKey Id { get; set; }

public long Id { get; set; }
string IEntity.Id { get => Id.ToString(); set => Convert.ToUInt64(Id); }
/// <summary>
/// 数据的创建时间
/// </summary>
Expand All @@ -91,53 +26,4 @@ public BaseEntityNum()
/// </summary>
public DateTime LastUpdateTime { get; set; }
}

public class BaseEntityMySql : IEntity
{
public BaseEntityMySql()
{
Id = IdGenerator.NextId();

CreateTimeTxt = DateTime.Now;
CreateTime = CreateTimeTxt.ToTimeStamp();
LastUpdateTimeTxt = DateTime.Now;
LastUpdateTime = LastUpdateTimeTxt.ToTimeStamp();
}

public long Id { get; set; }
string IEntity.Id { get => Id.ToString(); set => Convert.ToUInt64(Id); }
/// <summary>
/// 数据的创建时间
/// </summary>
public DateTime CreateTimeTxt { get; set; }
public long CreateTime { get; set; }


/// <summary>
/// 最后更新时间
/// </summary>
public DateTime LastUpdateTimeTxt { get; set; }
public long LastUpdateTime { get; set; }
}

public static class IdGenerator
{
private static IdWorker idWorker = new IdWorker(1, 1);

public static long NextId()
{
return idWorker.NextId();
}

public static long ToTimeStamp(this DateTime time)
{
DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc),
TimeZoneInfo.Local);
long stamp = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
return stamp;
}
}



}
14 changes: 14 additions & 0 deletions src/NuClear.Dapper/BaseEntityWithLongKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using NuClear.Snowflake;

namespace NuClear.Dapper
{
public class BaseEntityWithLongKey : BaseEntity<long>, IEntity
{
internal static readonly IdWorker idWorker = IdWorker.Create();
public BaseEntityWithLongKey()
: base()
{
Id = idWorker.NextId();
}
}
}
20 changes: 15 additions & 5 deletions src/NuClear.Dapper/Context/ContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public IDbTransaction Transaction
get { return _transaction; }
}



protected abstract void CreateConnection();

protected ContextBase()
Expand All @@ -53,8 +51,7 @@ public void BeginTransaction()
if (_isTransactionStarted)
throw new InvalidOperationException("已开启事务.");

if (_connection.State == ConnectionState.Closed)
_connection.Open();
EnsureOpenConnection();

_transaction = _connection.BeginTransaction();

Expand All @@ -63,6 +60,14 @@ public void BeginTransaction()
DebugPrint("事务开启.");
}

private void EnsureOpenConnection()
{
if (_connection.State == ConnectionState.Closed)
{
_connection.Open();
}
}

public void Commit()
{
if (!_isTransactionStarted)
Expand All @@ -80,7 +85,9 @@ public void Commit()
public void Rollback()
{
if (!_isTransactionStarted)
{
throw new InvalidOperationException("当前无事务.");
}

_transaction.Rollback();
_transaction.Dispose();
Expand All @@ -99,7 +106,10 @@ protected virtual void Dispose(bool disposing)
{
if (_isTransactionStarted)
{
try { Rollback(); }
try
{
Rollback();
}
catch
{
DebugPrint("事务已开启,释放数据库连接回滚.");
Expand Down
7 changes: 6 additions & 1 deletion src/NuClear.Dapper/Context/IContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ namespace NuClear.Dapper.Context
{
public interface IContext : IDisposable
{

/// <summary>
/// Id
/// </summary>
string Id { get; }

/// <summary>
/// 数据库连接
/// </summary>
IDbConnection Connection { get; }

/// <summary>
/// 数据库事务
/// </summary>
IDbTransaction Transaction { get; }

/// <summary>
/// 是否已开启事务
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/NuClear.Dapper/Extensions/SqlParameterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static object ToDynamicObject(this List<SqlParameter> parameters)
{
dic.Add(item.ParameterName, item.Value);
}
return obj as object;
return obj;
}
}
}
13 changes: 7 additions & 6 deletions src/NuClear.Dapper/IEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
/// </summary>
public interface IEntity
{
/// <summary>
/// 主键
/// </summary>
string Id { get; set; }
}

public interface IEntity<T>
public interface IEntityWithLongKey : IEntity<long>
{

}

public interface IEntity<TKey> : IEntity
{
/// <summary>
/// 主键
/// </summary>
T Id { get; set; }
TKey Id { get; set; }
}
}
19 changes: 9 additions & 10 deletions src/NuClear.Dapper/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

namespace NuClear.Dapper
{
public interface IRepository<TEntity> : IDisposable where TEntity : IEntity
public interface IRepository<TKey, TEntity> : IDisposable
where TEntity : IEntity<TKey>
{
TEntity FirstOrDefault(string id);
TEntity FirstOrDefault(TKey id);

TEntity FirstOrDefault(Query query, Sort sort = null);

Expand All @@ -21,7 +22,7 @@ public interface IRepository<TEntity> : IDisposable where TEntity : IEntity
/// <param name="query"></param>
/// <returns></returns>
int Count(Query query);
bool Exists(string id);
bool Exists(TKey id);
bool Exists(Query query);
IEnumerable<TEntity> Query(Query query, Sort sort = null);
IEnumerable<TEntity> Query(string query, object parameters = null);
Expand All @@ -38,19 +39,17 @@ public interface IRepository<TEntity> : IDisposable where TEntity : IEntity
IEnumerable<TReturn> Query<TFirst, TSecond, TReturn>(string sql, Func<TFirst, TSecond, TReturn> map, object param = null, string split = null) where TReturn : class;
IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TReturn>(string sql, Func<TFirst, TSecond, TThird, TReturn> map, object param = null, string split = null) where TReturn : class;



void Insert(TEntity entity);
void InsertBatch(IEnumerable<TEntity> entities);
int Execute(string sql, object parameters = null, CommandType? commandType = null);
void Delete(string id);
void Delete(TKey id);
void Delete(TEntity entity);
void DeleteBatch(params string[] ids);
void DeleteBatch(params TKey[] ids);
void DeleteBatch(IEnumerable<TEntity> entities);
void Update(TEntity entity);
void UpdateBatch(IEnumerable<TEntity> entities);

Task<TEntity> FirstOrDefaultAsync(string id);
Task<TEntity> FirstOrDefaultAsync(TKey id);

Task<TEntity> FirstOrDefaultAsync(Query query, Sort sort = null);

Expand All @@ -62,7 +61,7 @@ public interface IRepository<TEntity> : IDisposable where TEntity : IEntity
/// <param name="query"></param>
/// <returns></returns>
Task<int> CountAsync(Query query);
Task<bool> ExistsAsync(string id);
Task<bool> ExistsAsync(TKey id);
Task<bool> ExistsAsync(Query query);
Task<IEnumerable<TEntity>> QueryAsync(Query query, Sort sort = null);
Task<IEnumerable<TEntity>> QueryAsync(string query, object parameters = null);
Expand All @@ -77,7 +76,7 @@ public interface IRepository<TEntity> : IDisposable where TEntity : IEntity
Task<(IEnumerable<TAny>, int)> QueryPagedAsync<TAny>(string countSql, object countParameters, string query, object parameters) where TAny : class;
Task InsertAsync(TEntity entity);
Task<int> ExecuteAsync(string sql, object parameters = null, CommandType? commandType = null);
Task DeleteAsync(string id);
Task DeleteAsync(TKey id);
Task DeleteAsync(TEntity entity);
Task UpdateAsync(TEntity entity);

Expand Down
Loading

0 comments on commit 1a9d338

Please sign in to comment.