Ejemplo n.º 1
0
#pragma warning disable CA1000 // Do not declare static members on generic types; by-design, is ok.

        /// <summary>
        /// Registers (creates) the <see cref="Default"/> <see cref="DatabaseBase"/> instance; as well as registering with the <see cref="DataContextScope"/> for connection management
        /// and initiating the <see cref="OnConnectionOpen(DbConnection)"/>.
        /// </summary>
        /// <param name="create">Function to create the <see cref="Default"/> instance.</param>
        public static void Register(Func <TDefault> create)
        {
            lock (_lock)
            {
                if (_default != null)
                {
                    throw new InvalidOperationException("The Register method can only be invoked once.");
                }

                _create = create ?? throw new ArgumentNullException(nameof(create));

                DataContextScope.RegisterContext <TDefault, DbConnection>(() =>
                {
                    var conn = Default.CreateConnection(false);
                    conn.Open();

                    Default.OnConnectionOpen(conn);

                    return(conn);
                });
            }
        }