Ejemplo n.º 1
0
        public static void MapToObject(DbDataReader reader, object obj)
        {
            var type = obj.GetType();

            if (obj == null)
            {
                throw new ArgumentNullException(string.Format(@"KandaDbDataMapper.MapToObject<{0}>()", type.FullName));
            }

            var schema = reader.GetSchemaTable();

            var members = new List <MemberInfo>();

            members.AddRange(type.GetProperties((BindingFlags.Instance | BindingFlags.Public)));
            members.AddRange(type.GetFields((BindingFlags.Instance | BindingFlags.Public)));

            foreach (var member in members)
            {
                foreach (DataRow row in schema.Rows)
                {
                    var name  = (string)row[@"ColumnName"];
                    var value = reader[name];

                    var attributes = (KandaDataMappingAttribute[])member.GetCustomAttributes(typeof(KandaDataMappingAttribute), true);
                    if (attributes.Length > 1)
                    {
                        throw new Exception(string.Format(@"KandaDbDataMapper.MapToObject<{0}>()", type.FullName));
                    }
                    foreach (var attribute in attributes)
                    {
                        if (attribute.Ignore)
                        {
                            break;
                        }                                // 無視
                        if (attribute.MappingName != name)
                        {
                            continue;
                        }                                                // マッピング一致なし

                        value = ((value is DBNull) ? attribute.DefaultValue : value);
                        KandaDataMapper.SetValue(member, obj, value);
                        break;
                    }

                    if (0 < attributes.Length)
                    {
                        continue;
                    }                                        // MappingAttribute あり
                    if (member.Name != name)
                    {
                        continue;
                    }                                      // メンバー名一致なし

                    value = ((value is DBNull) ? null : value);
                    KandaDataMapper.SetValue(member, obj, value);
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public Authorization Find()
        {
            var connection  = default(DbConnection);
            var transaction = default(DbTransaction);

            try
            {
                connection = this._factory.CreateConnection();
                connection.Open();

                transaction = connection.BeginTransaction(IsolationLevel.Serializable);

                var found = KandaRepository.Authorizations.Find(this.ID, connection, transaction);

                KandaDataMapper.MapToObject(found, this._entity);

                this.Memberships = KandaRepository.MembershipAuthorizations.Get(new MembershipAuthorizationsCriteria()
                {
                    AuthorizationID = this.ID,
                }, connection, transaction);
                this.Roles = KandaRepository.RoleAuthorizations.Get(new RoleAuthorizationsCriteria()
                {
                    AuthorizationID = this.ID,
                }, connection, transaction);

                transaction.Commit();

                if (this.Found != null)
                {
                    this.Found(this, this._entity);
                }

                return(this);
            }
            catch (Exception)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                throw;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public UserHistory Find()
        {
            var connection  = default(DbConnection);
            var transaction = default(DbTransaction);

            try
            {
                connection = this._factory.CreateConnection();
                connection.Open();

                transaction = connection.BeginTransaction(IsolationLevel.Serializable);

                var found = KandaRepository.UserHistories.Find(this.UserID, this.Revision, connection, transaction);
                KandaDataMapper.MapToObject(found, this._entity);

                this.Attributes = KandaRepository.UserHistoryAttributes.Get(new UserHistoryAttributesCriteria()
                {
                    UserID = this.UserID, Revision = this.Revision
                }, connection, transaction);

                transaction.Commit();

                if (this.Found != null)
                {
                    this.Found(this, this._entity);
                }

                return(this);
            }
            catch
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                throw;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }
Ejemplo n.º 4
0
        public static void MapToParameters(DbCommand command, object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(string.Format(@"KandaDbDataMapper.MapToParameters()"));
            }

            var type = obj.GetType();

            var members = new List <MemberInfo>();

            members.AddRange(type.GetProperties((BindingFlags.Instance | BindingFlags.Public)));
            members.AddRange(type.GetFields((BindingFlags.Instance | BindingFlags.Public)));

            foreach (var member in members)
            {
                var attributes = (KandaDbParameterMappingAttribute[])member.GetCustomAttributes(typeof(KandaDbParameterMappingAttribute), true);
                if (1 < attributes.Length)
                {
                    throw new Exception(string.Format(@"KandaDbDataMapper.MapToParameters()"));
                }

                foreach (KandaDbParameterMappingAttribute attribute in attributes)
                {
                    if (attribute.Ignore)
                    {
                        continue;
                    }
                    var m         = member.GetType();
                    var parameter = command.CreateParameter();
                    parameter.ParameterName = attribute.MappingName;
                    parameter.DbType        = attribute.DbType;
                    parameter.Direction     = attribute.Direction;
                    //parameter.IsNullable = attribute.IsNullable;
                    //parameter.Value = ((parameter.Direction == ParameterDirection.Input) ? KandaDataMapper.GetValue(member, obj, DBNull.Value) : attribute.DefaultValue);
                    parameter.Value = KandaDataMapper.GetValue(member, obj, attribute.DefaultValue);

                    command.Parameters.Add(parameter);
                }
            }
        }
        public async Task <MembershipEntity> FindAsync(MembershipsCriteria criteria, DbConnection connection, DbTransaction transaction, CancellationToken token)
        {
            var reader = default(DbDataReader);

            try
            {
                reader = await MembershipsGateway.SelectAsync(criteria, connection, transaction, token);

                var entity = await reader.ReadAsync()
                    ? KandaDataMapper.MapToObject <MembershipEntity>(reader)
                    : MembershipEntity.Empty;

                return(entity);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public Membership Find()
        {
            var result      = this;
            var connection  = default(DbConnection);
            var transaction = default(DbTransaction);

            try
            {
                connection = this._factory.CreateConnection();
                connection.Open();

                transaction = connection.BeginTransaction(IsolationLevel.Serializable);

                var found = default(MembershipEntity);
                if (0 < this.ID)
                {
                    found = KandaRepository.Memberships.Find(this.ID, connection, transaction);
                }                                                                                                // ID で 取得
                else if (this._entity.Password == null)
                {
                    found = KandaRepository.Memberships.Find(this._entity.Name, connection, transaction);
                }                                                                                                                                 // 名前で取得
                else
                {
                    found = KandaRepository.Memberships.Find(this._entity.Name, this._entity.Password, connection, transaction);
                }                                                                                                                     // ログイン

                KandaDataMapper.MapToObject(found, this._entity);

                this.Users = KandaRepository.MembershipUsers.Get(this.ID, connection, transaction);
                this.Roles = KandaRepository.MembershipRoles.Get(new MembershipRolesCriteria()
                {
                    MembershipID = this.ID,
                }, connection, transaction);
                this.Authorizations = KandaRepository.MembershipAuthorizations.Get(new MembershipAuthorizationsCriteria()
                {
                    MembershipID = this.ID,
                }, connection, transaction);

                transaction.Commit();

                if (this.Found != null)
                {
                    this.Found(this, found);
                }

                return(result);
            }
            catch
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                throw;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }