コード例 #1
0
        /// <summary>
        /// Find all user claims by user id and the given claim.
        /// </summary>
        /// <param name="userId">Target user id.</param>
        /// <param name="claim">Target claim.</param>
        /// <returns>Returns a list of user claims if found; otherwise, returns empty list.</returns>
        public ICollection <TUserClaim> FindAllByUserId(TKey userId, Claim claim)
        {
            PropertyConfiguration userIdPropCfg     = Configuration.Property(p => p.UserId);
            PropertyConfiguration claimTypePropCfg  = Configuration.Property(p => p.ClaimType);
            PropertyConfiguration claimValuePropCfg = Configuration.Property(p => p.ClaimValue);
            DbCommand             command           = StorageContext.CreateCommand();

            command.CommandText = String.Format(
                @"SELECT * FROM {0} WHERE {1} = @{4} AND {2} = @{5} AND {3} = @{6};",
                QueryBuilder.GetQuotedIdentifier(Configuration.TableName),
                // Configured field names
                QueryBuilder.GetQuotedIdentifier(userIdPropCfg.ColumnName),
                QueryBuilder.GetQuotedIdentifier(claimTypePropCfg.ColumnName),
                QueryBuilder.GetQuotedIdentifier(claimValuePropCfg.ColumnName),
                // Parameter names
                userIdPropCfg.PropertyName,
                claimTypePropCfg.PropertyName,
                claimValuePropCfg.PropertyName);

            DbCommandContext cmdContext = new DbCommandContext(command);

            cmdContext.Parameters[userIdPropCfg.PropertyName].Value     = userId;
            cmdContext.Parameters[claimTypePropCfg.PropertyName].Value  = claim.Type;
            cmdContext.Parameters[claimValuePropCfg.PropertyName].Value = claim.Value;

            DbDataReader             reader = null;
            ICollection <TUserClaim> list   = null;

            StorageContext.Open();

            try
            {
                reader = cmdContext.ExecuteReader();
                list   = EntityBuilder.BuildAll(reader);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                cmdContext.Dispose();
                StorageContext.Close();
            }

            return(list);
        }