/// <summary>
        /// Construct a new instance of the <see cref="DapperGroupRepository{TGroup}"/>
        /// </summary>
        /// <param name="connection">The connection to the database. If the connection is not open, an attempt is made to open it.</param>
        /// <param name="utilities">An instance of the <see cref="Utilities"/> class. Defaults to null, in which case a new instance is created.</param>
        /// <param name="schema">The schema used for the tables. Default is "dbo".</param>
        /// <param name="groupTable">The name of the table that represents Groups. Default is "Groups".</param>
        /// <param name="tableNameMap">A dictionary mapping any custom types used to their corresponding table names.</param>
        /// <param name="keySelectorMap">A dictionary mapping any custom types used to a <see cref="PropertyInfo"/> object that can be used to retrieve the primary key.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="connection"/> is null, or <paramref name="groupTable"/> is null or whitespace.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If <paramref name="connection"/> fails to open.
        /// </exception>
        public DapperGroupRepository(IDbConnection connection, Utilities utilities = null, string schema = "dbo", string groupTable = "Groups",
                                     Dictionary <Type, string> tableNameMap        = null, Dictionary <Type, PropertyInfo> keySelectorMap = null)
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (string.IsNullOrWhiteSpace(groupTable))
            {
                throw new ArgumentNullException(nameof(groupTable));
            }

            if (connection.State != ConnectionState.Open)
            {
                if (connection.State == ConnectionState.Broken)
                {
                    connection.Close();
                }
                connection.Open();
            }

            if (connection.State != ConnectionState.Open)
            {
                throw new ArgumentException("Cannot open the connection", nameof(connection));
            }

            Connection = connection;
            _utilities = utilities ?? new Utilities();

            Schema     = _utilities.EscapeTableName(schema);
            GroupTable = _utilities.EscapeTableName(groupTable);

            // NOTE(tim): Ignore results, we just want to populate the cache.
            _utilities.GetTypeProperties <TGroup>();
            _utilities.GetChildCollectionProperties <TGroup>();
            _utilities.GetTypeProperties <RelationalGroupChild>();

            TableNameMap = tableNameMap ?? new Dictionary <Type, string>();
            foreach (var kv in TableNameMap.ToList())
            {
                if (string.IsNullOrWhiteSpace(kv.Value))
                {
                    throw new Exception($"The table name specified for {kv.Key.Name} is invalid.");
                }
                TableNameMap[kv.Key] = _utilities.EscapeTableName(kv.Value);
            }

            if (!TableNameMap.ContainsKey(typeof(RelationalGroupChild)))
            {
                TableNameMap[typeof(RelationalGroupChild)] = "GroupChilds";
            }

            KeySelectorMap = keySelectorMap ?? new Dictionary <Type, PropertyInfo>();

            if (!KeySelectorMap.ContainsKey(typeof(RelationalGroupChild)))
            {
                KeySelectorMap[typeof(RelationalGroupChild)] = typeof(RelationalGroupChild).GetProperty("Key");
            }
        }
예제 #2
0
        /// <summary>
        /// Construct a new instance of the <see cref="DapperUserAccountRepository{TAccount}"/>
        /// </summary>
        /// <param name="connection">The connection to the database. If the connection is not open, an attempt is made to open it.</param>
        /// <param name="utilities">An instance of the <see cref="Utilities"/> class. Defaults to null, in which case a new instance is created.</param>
        /// <param name="schema">The schema used for the tables. Default is "dbo".</param>
        /// <param name="userAccountTable">The name of the table that represents User Accounts. Default is "UserAccounts".</param>
        /// <param name="tableNameMap">A dictionary mapping any custom types used to their corresponding table names.</param>
        /// <param name="keySelectorMap">A dictionary mapping any custom types used to a <see cref="PropertyInfo"/> object that can be used to retrieve the primary key.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="connection"/> is null, or any table name parameter is null or whitespace.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If <paramref name="connection"/> fails to open.
        /// </exception>
        public DapperUserAccountRepository(IDbConnection connection, Utilities utilities = null, string schema = "dbo", string userAccountTable = "UserAccounts",
                                           Dictionary <Type, string> tableNameMap        = null, Dictionary <Type, PropertyInfo> keySelectorMap = null)
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (string.IsNullOrWhiteSpace(userAccountTable))
            {
                throw new ArgumentNullException(nameof(userAccountTable));
            }

            if (connection.State != ConnectionState.Open)
            {
                if (connection.State == ConnectionState.Broken)
                {
                    connection.Close();
                }
                connection.Open();
            }

            if (connection.State != ConnectionState.Open)
            {
                throw new ArgumentException("Cannot open the connection", nameof(connection));
            }

            Connection = connection;
            _utilities = utilities ?? new Utilities();

            Schema           = _utilities.EscapeTableName(schema);
            UserAccountTable = _utilities.EscapeTableName(userAccountTable);

            // NOTE(tim): Ignore results, we just want to populate the cache.
            _utilities.GetTypeProperties <TAccount>();
            _utilities.GetChildCollectionProperties <TAccount>();
            _utilities.GetTypeProperties <RelationalUserCertificate>();
            _utilities.GetTypeProperties <RelationalUserClaim>();
            _utilities.GetTypeProperties <RelationalLinkedAccount>();
            _utilities.GetTypeProperties <RelationalLinkedAccountClaim>();
            _utilities.GetTypeProperties <RelationalTwoFactorAuthToken>();
            _utilities.GetTypeProperties <RelationalPasswordResetSecret>();

            TableNameMap = tableNameMap ?? new Dictionary <Type, string>();
            foreach (var kv in TableNameMap.ToList())
            {
                if (string.IsNullOrWhiteSpace(kv.Value))
                {
                    throw new Exception($"The table name specified for {kv.Key.Name} is invalid.");
                }
                TableNameMap[kv.Key] = _utilities.EscapeTableName(kv.Value);
            }

            if (!TableNameMap.ContainsKey(typeof(RelationalUserCertificate)))
            {
                TableNameMap[typeof(RelationalUserCertificate)] = "UserCertificates";
            }
            if (!TableNameMap.ContainsKey(typeof(RelationalUserClaim)))
            {
                TableNameMap[typeof(RelationalUserClaim)] = "UserClaims";
            }
            if (!TableNameMap.ContainsKey(typeof(RelationalLinkedAccount)))
            {
                TableNameMap[typeof(RelationalLinkedAccount)] = "LinkedAccounts";
            }
            if (!TableNameMap.ContainsKey(typeof(RelationalLinkedAccountClaim)))
            {
                TableNameMap[typeof(RelationalLinkedAccountClaim)] = "LinkedAccountClaims";
            }
            if (!TableNameMap.ContainsKey(typeof(RelationalTwoFactorAuthToken)))
            {
                TableNameMap[typeof(RelationalTwoFactorAuthToken)] = "TwoFactorAuthTokens";
            }
            if (!TableNameMap.ContainsKey(typeof(RelationalPasswordResetSecret)))
            {
                TableNameMap[typeof(RelationalPasswordResetSecret)] = "PasswordResetSecrets";
            }

            KeySelectorMap = keySelectorMap ?? new Dictionary <Type, PropertyInfo>();

            if (!KeySelectorMap.ContainsKey(typeof(RelationalUserCertificate)))
            {
                KeySelectorMap[typeof(RelationalUserCertificate)] = typeof(RelationalUserCertificate).GetProperty("Key");
            }
            if (!KeySelectorMap.ContainsKey(typeof(RelationalUserClaim)))
            {
                KeySelectorMap[typeof(RelationalUserClaim)] = typeof(RelationalUserClaim).GetProperty("Key");
            }
            if (!KeySelectorMap.ContainsKey(typeof(RelationalLinkedAccount)))
            {
                KeySelectorMap[typeof(RelationalLinkedAccount)] = typeof(RelationalLinkedAccount).GetProperty("Key");
            }
            if (!KeySelectorMap.ContainsKey(typeof(RelationalLinkedAccountClaim)))
            {
                KeySelectorMap[typeof(RelationalLinkedAccountClaim)] = typeof(RelationalLinkedAccountClaim).GetProperty("Key");
            }
            if (!KeySelectorMap.ContainsKey(typeof(RelationalTwoFactorAuthToken)))
            {
                KeySelectorMap[typeof(RelationalTwoFactorAuthToken)] = typeof(RelationalTwoFactorAuthToken).GetProperty("Key");
            }
            if (!KeySelectorMap.ContainsKey(typeof(RelationalPasswordResetSecret)))
            {
                KeySelectorMap[typeof(RelationalPasswordResetSecret)] = typeof(RelationalPasswordResetSecret).GetProperty("Key");
            }
        }