Refactoring

pull/1/head
a.bozhenov 5 years ago
parent 90cb844e52
commit 391f8d43d4

@ -1,8 +1,34 @@
using System; using System;
using System.Data;
using System.Data.Common; using System.Data.Common;
namespace ZeroLevel.SqlServer namespace ZeroLevel.SqlServer
{ {
public class DbReader
: IDisposable
{
private readonly DbConnection _connection;
private readonly IDbCommand _command;
public DbReader(DbConnection connection, IDbCommand command)
{
_connection = connection;
_command = command;
}
public IDataReader GetReader()
{
return _command.ExecuteReader(CommandBehavior.CloseConnection);
}
public void Dispose()
{
_command.Dispose();
_connection.Close();
_connection.Dispose();
}
}
public static class DBReader public static class DBReader
{ {
public static T Read<T>(this DbDataReader reader, int index) public static T Read<T>(this DbDataReader reader, int index)

@ -34,10 +34,10 @@ namespace ZeroLevel.SqlServer
private TKey _id; private TKey _id;
private readonly IDbMapper<T> _mapper; private readonly IDbMapper<T> _mapper;
public IdentitySpecification(TKey id, bool poco) public IdentitySpecification(TKey id, bool mapOnlyMarkedMembers)
{ {
_id = id; _id = id;
_mapper = DbMapperFactory.Create<T>(poco); _mapper = DbMapperFactory.Create<T>(mapOnlyMarkedMembers);
} }
public override bool IsSatisfiedBy(T o) public override bool IsSatisfiedBy(T o)
@ -45,6 +45,6 @@ namespace ZeroLevel.SqlServer
return _mapper.Id(o).Equals(_id); return _mapper.Id(o).Equals(_id);
} }
public static ISpecification<T> Create(TKey id, bool poco) { return new IdentitySpecification<T, TKey>(id, poco); } public static ISpecification<T> Create(TKey id, bool mapOnlyMarkedMembers) { return new IdentitySpecification<T, TKey>(id, mapOnlyMarkedMembers); }
} }
} }

@ -4,7 +4,8 @@ using ZeroLevel.Specification;
namespace ZeroLevel.SqlServer namespace ZeroLevel.SqlServer
{ {
public class SqlIdentitySpecification<T> : IdentitySpecification<T>, ISqlServerSpecification public class SqlIdentitySpecification<T>
: IdentitySpecification<T>, ISqlServerSpecification
where T : IEntity where T : IEntity
{ {
public SqlIdentitySpecification(Guid id) : base(id) public SqlIdentitySpecification(Guid id) : base(id)

@ -15,7 +15,7 @@ namespace ZeroLevel.SqlServer
/// <summary> /// <summary>
/// В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember /// В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember
/// </summary> /// </summary>
private readonly bool _analizeAsPoco; private readonly bool _marked_only;
protected Func<DbField, object, object> typeConverter; protected Func<DbField, object, object> typeConverter;
public void SetTypeConverter(Func<IDbField, object, object> converter) public void SetTypeConverter(Func<IDbField, object, object> converter)
@ -56,9 +56,9 @@ namespace ZeroLevel.SqlServer
return IdentityField?.Getter(entity); return IdentityField?.Getter(entity);
} }
internal DbMapper(Type entityType, bool as_poco) internal DbMapper(Type entityType, bool mapOnlyMarkedMembers)
{ {
_analizeAsPoco = as_poco; _marked_only = mapOnlyMarkedMembers;
_entityType = entityType; _entityType = entityType;
BuildMapping(); BuildMapping();
} }
@ -74,7 +74,7 @@ namespace ZeroLevel.SqlServer
Do(members => Do(members =>
{ {
IEnumerable<MemberInfo> memberList; IEnumerable<MemberInfo> memberList;
if (false == _analizeAsPoco) if (_marked_only)
{ {
memberList = members.Where(m => null != Attribute.GetCustomAttribute(m, typeof(DbMemberAttribute))); memberList = members.Where(m => null != Attribute.GetCustomAttribute(m, typeof(DbMemberAttribute)));
} }

@ -11,9 +11,9 @@ namespace ZeroLevel.SqlServer
/// Создание маппера /// Создание маппера
/// </summary> /// </summary>
/// <param name="entityType">Тип представляющий модель данных</param> /// <param name="entityType">Тип представляющий модель данных</param>
/// <param name="asPoco">В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember</param> /// <param name="mapOnlyMarkedMembers">В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember</param>
/// <returns>Маппер</returns> /// <returns>Маппер</returns>
public static IDbMapper Create(Type entityType, bool asPoco = false) public static IDbMapper Create(Type entityType, bool mapOnlyMarkedMembers = false)
{ {
if (null == entityType) if (null == entityType)
throw new ArgumentNullException(nameof(entityType)); throw new ArgumentNullException(nameof(entityType));
@ -24,7 +24,7 @@ namespace ZeroLevel.SqlServer
var gt = typeof(IDbMapper<>); var gt = typeof(IDbMapper<>);
var rt = gt.MakeGenericType(new Type[] { entityType }); var rt = gt.MakeGenericType(new Type[] { entityType });
_mapperPool.Add(entityType, new DbMapper(rt, asPoco)); _mapperPool.Add(entityType, new DbMapper(rt, mapOnlyMarkedMembers));
} }
} }
return _mapperPool[entityType]; return _mapperPool[entityType];
@ -32,16 +32,16 @@ namespace ZeroLevel.SqlServer
/// <summary> /// <summary>
/// Создание маппера /// Создание маппера
/// </summary> /// </summary>
/// <param name="asPoco">В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember</param> /// <param name="mapOnlyMarkedMembers">В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember</param>
/// <returns>Маппер</returns> /// <returns>Маппер</returns>
public static IDbMapper<T> Create<T>(bool asPoco = false) public static IDbMapper<T> Create<T>(bool mapOnlyMarkedMembers = false)
{ {
var entityType = typeof(T); var entityType = typeof(T);
lock (_poolLocker) lock (_poolLocker)
{ {
if (false == _mapperPool.ContainsKey(entityType)) if (false == _mapperPool.ContainsKey(entityType))
{ {
_mapperPool.Add(entityType, new DbMapper<T>(asPoco)); _mapperPool.Add(entityType, new DbMapper<T>(mapOnlyMarkedMembers));
} }
} }
return (IDbMapper<T>)_mapperPool[entityType]; return (IDbMapper<T>)_mapperPool[entityType];

@ -6,7 +6,7 @@ namespace ZeroLevel.SqlServer
{ {
public class DbMapper<T> : DbMapper, IDbMapper<T> public class DbMapper<T> : DbMapper, IDbMapper<T>
{ {
public DbMapper(bool as_poco) : base(typeof(T), as_poco) public DbMapper(bool mapOnlyMarkedMembers) : base(typeof(T), mapOnlyMarkedMembers)
{ {
} }

@ -24,9 +24,9 @@ namespace ZeroLevel.SqlServer
} }
} }
public SqlDbMapper(bool entity_is_poco) : base(typeof(T).Name) public SqlDbMapper(bool mapOnlyMarkedMembers) : base(typeof(T).Name)
{ {
_mapper = DbMapperFactory.Create<T>(entity_is_poco); _mapper = DbMapperFactory.Create<T>(mapOnlyMarkedMembers);
} }
public T Deserialize(DataRow row) public T Deserialize(DataRow row)

@ -15,9 +15,9 @@ namespace ZeroLevel.SqlServer
} }
} }
public SqlDbMapper(Type entityType, bool as_poco = false) : base(entityType.Name) public SqlDbMapper(Type entityType, bool mapOnlyMarkedMembers = true) : base(entityType.Name)
{ {
_mapper = DbMapperFactory.Create(entityType, as_poco); _mapper = DbMapperFactory.Create(entityType, mapOnlyMarkedMembers);
} }
public object Deserialize(DataRow row) public object Deserialize(DataRow row)

@ -7,31 +7,6 @@ using System.Linq;
namespace ZeroLevel.SqlServer namespace ZeroLevel.SqlServer
{ {
public class DbReader
: IDisposable
{
private readonly DbConnection _connection;
private readonly IDbCommand _command;
public DbReader(DbConnection connection, IDbCommand command)
{
_connection = connection;
_command = command;
}
public IDataReader GetReader()
{
return _command.ExecuteReader(CommandBehavior.CloseConnection);
}
public void Dispose()
{
_command.Dispose();
_connection.Close();
_connection.Dispose();
}
}
public class SqlDbProvider : public class SqlDbProvider :
IDbProvider IDbProvider
{ {

@ -17,9 +17,9 @@ namespace ZeroLevel.SqlServer
#endregion #endregion
#region Ctors #region Ctors
public SqlDbRepository(SqlDbConnectionFactory connectionFactory, bool entity_is_poco = false) public SqlDbRepository(SqlDbConnectionFactory connectionFactory, bool mapOnlyMarkedMembers = true)
{ {
_mapper = new SqlDbMapper<T>(entity_is_poco); _mapper = new SqlDbMapper<T>(mapOnlyMarkedMembers);
_dbProvider = new SqlDbProvider(connectionFactory); _dbProvider = new SqlDbProvider(connectionFactory);
if (false == SqlDbProvider.CheckDatabaseExists(connectionFactory.Server, connectionFactory.Base)) if (false == SqlDbProvider.CheckDatabaseExists(connectionFactory.Server, connectionFactory.Base))
{ {

@ -3,6 +3,13 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<Platforms>AnyCPU;x64</Platforms> <Platforms>AnyCPU;x64</Platforms>
<Description>Light wrapper over ado.net</Description>
<Authors>ogoun</Authors>
<Copyright>Copyright Ogoun 2019</Copyright>
<PackageProjectUrl>https://github.com/ogoun/Zero</PackageProjectUrl>
<RepositoryUrl>https://github.com/ogoun/Zero</RepositoryUrl>
<PackageIconUrl>https://raw.githubusercontent.com/ogoun/Zero/master/zero.png</PackageIconUrl>
<RepositoryType>GitHub</RepositoryType>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

Loading…
Cancel
Save

Powered by TurnKey Linux.