From 391f8d43d4fd62867121df41c2d3514d20350061 Mon Sep 17 00:00:00 2001 From: "a.bozhenov" Date: Tue, 29 Oct 2019 21:56:38 +0300 Subject: [PATCH] Refactoring --- ZeroLevel.SQL/DBReader.cs | 26 +++++++++++++++++++ ZeroLevel.SQL/DDD/IdentitySpecification.cs | 6 ++--- ZeroLevel.SQL/DDD/SqlIdentitySpecification.cs | 3 ++- ZeroLevel.SQL/DbMapper.cs | 8 +++--- ZeroLevel.SQL/DbMapperFactory.cs | 12 ++++----- ZeroLevel.SQL/GenericDbMapper.cs | 2 +- ZeroLevel.SQL/GenericSqlDbMapper.cs | 4 +-- ZeroLevel.SQL/SqlDbMapper.cs | 4 +-- ZeroLevel.SQL/SqlDbProvider.cs | 25 ------------------ ZeroLevel.SQL/SqlDbRepository.cs | 4 +-- ZeroLevel.SQL/ZeroLevel.SQL.csproj | 7 +++++ 11 files changed, 55 insertions(+), 46 deletions(-) diff --git a/ZeroLevel.SQL/DBReader.cs b/ZeroLevel.SQL/DBReader.cs index e50cb8d..9d23222 100644 --- a/ZeroLevel.SQL/DBReader.cs +++ b/ZeroLevel.SQL/DBReader.cs @@ -1,8 +1,34 @@ using System; +using System.Data; using System.Data.Common; 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 T Read(this DbDataReader reader, int index) diff --git a/ZeroLevel.SQL/DDD/IdentitySpecification.cs b/ZeroLevel.SQL/DDD/IdentitySpecification.cs index 999a0d6..ed1642a 100644 --- a/ZeroLevel.SQL/DDD/IdentitySpecification.cs +++ b/ZeroLevel.SQL/DDD/IdentitySpecification.cs @@ -34,10 +34,10 @@ namespace ZeroLevel.SqlServer private TKey _id; private readonly IDbMapper _mapper; - public IdentitySpecification(TKey id, bool poco) + public IdentitySpecification(TKey id, bool mapOnlyMarkedMembers) { _id = id; - _mapper = DbMapperFactory.Create(poco); + _mapper = DbMapperFactory.Create(mapOnlyMarkedMembers); } public override bool IsSatisfiedBy(T o) @@ -45,6 +45,6 @@ namespace ZeroLevel.SqlServer return _mapper.Id(o).Equals(_id); } - public static ISpecification Create(TKey id, bool poco) { return new IdentitySpecification(id, poco); } + public static ISpecification Create(TKey id, bool mapOnlyMarkedMembers) { return new IdentitySpecification(id, mapOnlyMarkedMembers); } } } diff --git a/ZeroLevel.SQL/DDD/SqlIdentitySpecification.cs b/ZeroLevel.SQL/DDD/SqlIdentitySpecification.cs index 9e41c75..04b6fed 100644 --- a/ZeroLevel.SQL/DDD/SqlIdentitySpecification.cs +++ b/ZeroLevel.SQL/DDD/SqlIdentitySpecification.cs @@ -4,7 +4,8 @@ using ZeroLevel.Specification; namespace ZeroLevel.SqlServer { - public class SqlIdentitySpecification : IdentitySpecification, ISqlServerSpecification + public class SqlIdentitySpecification + : IdentitySpecification, ISqlServerSpecification where T : IEntity { public SqlIdentitySpecification(Guid id) : base(id) diff --git a/ZeroLevel.SQL/DbMapper.cs b/ZeroLevel.SQL/DbMapper.cs index 238b9ac..c5b8065 100644 --- a/ZeroLevel.SQL/DbMapper.cs +++ b/ZeroLevel.SQL/DbMapper.cs @@ -15,7 +15,7 @@ namespace ZeroLevel.SqlServer /// /// В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember /// - private readonly bool _analizeAsPoco; + private readonly bool _marked_only; protected Func typeConverter; public void SetTypeConverter(Func converter) @@ -56,9 +56,9 @@ namespace ZeroLevel.SqlServer 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; BuildMapping(); } @@ -74,7 +74,7 @@ namespace ZeroLevel.SqlServer Do(members => { IEnumerable memberList; - if (false == _analizeAsPoco) + if (_marked_only) { memberList = members.Where(m => null != Attribute.GetCustomAttribute(m, typeof(DbMemberAttribute))); } diff --git a/ZeroLevel.SQL/DbMapperFactory.cs b/ZeroLevel.SQL/DbMapperFactory.cs index 5e103af..fc1fad8 100644 --- a/ZeroLevel.SQL/DbMapperFactory.cs +++ b/ZeroLevel.SQL/DbMapperFactory.cs @@ -11,9 +11,9 @@ namespace ZeroLevel.SqlServer /// Создание маппера /// /// Тип представляющий модель данных - /// В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember + /// В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember /// Маппер - public static IDbMapper Create(Type entityType, bool asPoco = false) + public static IDbMapper Create(Type entityType, bool mapOnlyMarkedMembers = false) { if (null == entityType) throw new ArgumentNullException(nameof(entityType)); @@ -24,7 +24,7 @@ namespace ZeroLevel.SqlServer var gt = typeof(IDbMapper<>); var rt = gt.MakeGenericType(new Type[] { entityType }); - _mapperPool.Add(entityType, new DbMapper(rt, asPoco)); + _mapperPool.Add(entityType, new DbMapper(rt, mapOnlyMarkedMembers)); } } return _mapperPool[entityType]; @@ -32,16 +32,16 @@ namespace ZeroLevel.SqlServer /// /// Создание маппера /// - /// В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember + /// В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember /// Маппер - public static IDbMapper Create(bool asPoco = false) + public static IDbMapper Create(bool mapOnlyMarkedMembers = false) { var entityType = typeof(T); lock (_poolLocker) { if (false == _mapperPool.ContainsKey(entityType)) { - _mapperPool.Add(entityType, new DbMapper(asPoco)); + _mapperPool.Add(entityType, new DbMapper(mapOnlyMarkedMembers)); } } return (IDbMapper)_mapperPool[entityType]; diff --git a/ZeroLevel.SQL/GenericDbMapper.cs b/ZeroLevel.SQL/GenericDbMapper.cs index 431f9b3..b69a3d7 100644 --- a/ZeroLevel.SQL/GenericDbMapper.cs +++ b/ZeroLevel.SQL/GenericDbMapper.cs @@ -6,7 +6,7 @@ namespace ZeroLevel.SqlServer { public class DbMapper : DbMapper, IDbMapper { - public DbMapper(bool as_poco) : base(typeof(T), as_poco) + public DbMapper(bool mapOnlyMarkedMembers) : base(typeof(T), mapOnlyMarkedMembers) { } diff --git a/ZeroLevel.SQL/GenericSqlDbMapper.cs b/ZeroLevel.SQL/GenericSqlDbMapper.cs index a2c4be8..ca8d6a8 100644 --- a/ZeroLevel.SQL/GenericSqlDbMapper.cs +++ b/ZeroLevel.SQL/GenericSqlDbMapper.cs @@ -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(entity_is_poco); + _mapper = DbMapperFactory.Create(mapOnlyMarkedMembers); } public T Deserialize(DataRow row) diff --git a/ZeroLevel.SQL/SqlDbMapper.cs b/ZeroLevel.SQL/SqlDbMapper.cs index 477c9cb..c8ca8af 100644 --- a/ZeroLevel.SQL/SqlDbMapper.cs +++ b/ZeroLevel.SQL/SqlDbMapper.cs @@ -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) diff --git a/ZeroLevel.SQL/SqlDbProvider.cs b/ZeroLevel.SQL/SqlDbProvider.cs index 11ded64..98a5c79 100644 --- a/ZeroLevel.SQL/SqlDbProvider.cs +++ b/ZeroLevel.SQL/SqlDbProvider.cs @@ -7,31 +7,6 @@ using System.Linq; 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 : IDbProvider { diff --git a/ZeroLevel.SQL/SqlDbRepository.cs b/ZeroLevel.SQL/SqlDbRepository.cs index 567049c..da01f8f 100644 --- a/ZeroLevel.SQL/SqlDbRepository.cs +++ b/ZeroLevel.SQL/SqlDbRepository.cs @@ -17,9 +17,9 @@ namespace ZeroLevel.SqlServer #endregion #region Ctors - public SqlDbRepository(SqlDbConnectionFactory connectionFactory, bool entity_is_poco = false) + public SqlDbRepository(SqlDbConnectionFactory connectionFactory, bool mapOnlyMarkedMembers = true) { - _mapper = new SqlDbMapper(entity_is_poco); + _mapper = new SqlDbMapper(mapOnlyMarkedMembers); _dbProvider = new SqlDbProvider(connectionFactory); if (false == SqlDbProvider.CheckDatabaseExists(connectionFactory.Server, connectionFactory.Base)) { diff --git a/ZeroLevel.SQL/ZeroLevel.SQL.csproj b/ZeroLevel.SQL/ZeroLevel.SQL.csproj index 1949889..8e98957 100644 --- a/ZeroLevel.SQL/ZeroLevel.SQL.csproj +++ b/ZeroLevel.SQL/ZeroLevel.SQL.csproj @@ -3,6 +3,13 @@ netstandard2.0 AnyCPU;x64 + Light wrapper over ado.net + ogoun + Copyright Ogoun 2019 + https://github.com/ogoun/Zero + https://github.com/ogoun/Zero + https://raw.githubusercontent.com/ogoun/Zero/master/zero.png + GitHub