/// <summary>
        /// 
        /// </summary>
        /// <param name="criteria"></param>
        /// <param name="connection"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public MembershipEntity Find(MembershipsCriteria criteria, DbConnection connection, DbTransaction transaction)
        {
            var password = criteria.Password;
            if (password == null) { this.DoNothing(); }
            else
            {
                if (criteria.Password is SecureString) { password = ((SecureString)criteria.Password).GetString(); }

                var hash = KandaHashAlgorithm.ComputeHash( typeof(SHA512Managed).FullName, (string)password, Encoding.Unicode);
                password = hash;
            }
            criteria.Password = password;

            var reader = default(KandaDbDataReader);

            try
            {
                reader = MembershipsGateway.Select(criteria, connection, transaction);

                var found = (reader.Read() ? KandaDbDataMapper.MapToObject<MembershipEntity>(reader) : MembershipEntity.Empty);

                return found;
            }
            finally
            {
                if (reader != null) { reader.Close(); }
            }
        }
	    public async void FindAsyncFact()
		{
            var connection = this.Factory.CreateConnection();
		    var transaction = default(DbTransaction);
			var criteria = new MembershipsCriteria()
			{
			    ID = 10003,
				Enabled = true,
			};

		    try
		    {
		        await connection.OpenAsync();
		        transaction = connection.BeginTransaction();

		        var entity = await KandaRepository.Memberships.FindAsync(criteria, connection, transaction, CancellationToken.None);

				transaction.Commit();

				Assert.NotEqual(criteria.ID, entity.ID);
		    }
		    catch
		    {
                if (transaction != null) { transaction.Rollback();  }
		        throw;
		    }
		    finally
		    {
                if (connection != null) { connection.Close(); }
		    }
		}
        public static async Task<DbDataReader> SelectAsync(MembershipsCriteria criteria, DbConnection connection, DbTransaction transaction, CancellationToken token)
        {
            var reader = _factory.CreateReader(connection, transaction);
            reader.CommandText = @"usp_SelectMemberships";

			KandaDbDataMapper.MapToParameters(reader, criteria);

            var result = await reader.ExecuteReaderAsync(token);

            return reader;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="criteria"></param>
        /// <param name="connection"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public static KandaDbDataReader Select(MembershipsCriteria criteria, DbConnection connection, DbTransaction transaction)
        {
            var reader = KandaTableDataGateway._factory.CreateReader(connection, transaction);

            reader.CommandText = @"usp_SelectMemberships";

            KandaDbDataMapper.MapToParameters(reader, criteria);

            reader.ExecuteReader();

            return reader;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="criteria"></param>
        /// <param name="connection"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public KandaDbDataReader Select(MembershipsCriteria criteria, DbConnection connection, DbTransaction transaction)
        {
            var reader = KandaTableDataGateway._factory.CreateReader(connection, transaction);

            reader.CommandText = @"usp_SelectMembershipsView";

            KandaDbDataMapper.MapToParameters(reader, criteria);

            var result = KandaTableDataGateway._factory.CreateParameter(KandaTableDataGateway.RETURN_VALUE, DbType.Int32, sizeof(int), ParameterDirection.Output, DBNull.Value);
            reader.Parameters.Add(result);

            reader.ExecuteReader();

            return reader;
        }
        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(); }
		    }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerable<MembershipEntity> Get(MembershipsCriteria criteria, DbConnection connection, DbTransaction transaction)
        {
            var reader = default(DbDataReader);

            try
            {
                reader = MembershipsGateway.Select(criteria, connection, transaction);

                var memberships = KandaDbDataMapper.MapToEnumerable<MembershipEntity>(reader);

                return memberships;
            }
            finally
            {
                if (reader != null) { reader.Close(); }
            }
        }
 public KandaDbDataReader Count(MembershipsCriteria criteria, DbConnection connection, DbTransaction transaction)
 {
     throw new NotImplementedException(@"MembershipsViewGateway.Count()");
 }