diff --git a/Src/AspNet.Identity.Oracle/OracleDatabase.cs b/Src/AspNet.Identity.Oracle/OracleDatabase.cs index 6d7559e..07781bb 100644 --- a/Src/AspNet.Identity.Oracle/OracleDatabase.cs +++ b/Src/AspNet.Identity.Oracle/OracleDatabase.cs @@ -1,218 +1,218 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Configuration; -using System.Data; -using System.Threading; -using Oracle.DataAccess.Client; - -namespace AspNet.Identity.Oracle -{ - /// - /// Class that encapsulates a Oracle Database connections - /// and CRUD operations - /// - public class OracleDatabase : IDisposable - { - private OracleConnection connection; - - /// - /// Default constructor which uses the "DefaultConnection" connectionString - /// - public OracleDatabase() - : this("DefaultConnection") - { - } - - /// - /// Constructor which takes the connection string name - /// - /// - public OracleDatabase(string connectionStringName) - { - var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString; - connection = new OracleConnection(connectionString); - } - - /// - /// Executes a non-query Oracle statement - /// - /// The Oracle query to execute - /// Optional parameters to pass to the query - /// The count of records affected by the Oracle statement - public int Execute(string commandText, IEnumerable parameters) - { - int result; - - if (string.IsNullOrEmpty(commandText)) - { - throw new ArgumentException("Command text cannot be null or empty."); - } - - try - { - ensureConnectionOpen(); - var command = createCommand(commandText, parameters); - result = command.ExecuteNonQuery(); - } - finally - { - connection.Close(); - } - - return result; - } - - /// - /// Executes a Oracle query that returns a single scalar value as the result. - /// - /// The Oracle query to execute - /// Optional parameters to pass to the query - /// - public object QueryValue(string commandText, IEnumerable parameters) - { +using System; +using System.Collections; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Threading; +using Oracle.DataAccess.Client; + +namespace AspNet.Identity.Oracle +{ + /// + /// Class that encapsulates a Oracle Database connections + /// and CRUD operations + /// + public class OracleDatabase : IDisposable + { + private OracleConnection _connection; + + /// + /// Default constructor which uses the "DefaultConnection" connectionString + /// + public OracleDatabase() + : this("DefaultConnection") + { + } + + /// + /// Constructor which takes the connection string name + /// + /// + public OracleDatabase(string connectionStringName) + { + var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString; + _connection = new OracleConnection(connectionString); + } + + /// + /// Executes a non-query Oracle statement + /// + /// The Oracle query to execute + /// Optional parameters to pass to the query + /// The count of records affected by the Oracle statement + public int Execute(string commandText, IEnumerable parameters) + { + int result; + + if (string.IsNullOrEmpty(commandText)) + { + throw new ArgumentException("Command text cannot be null or empty."); + } + + try + { + EnsureConnectionOpen(); + var command = CreateCommand(commandText, parameters); + result = command.ExecuteNonQuery(); + } + finally + { + _connection.Close(); + } + + return result; + } + + /// + /// Executes a Oracle query that returns a single scalar value as the result. + /// + /// The Oracle query to execute + /// Optional parameters to pass to the query + /// + public object QueryValue(string commandText, IEnumerable parameters) + { object result; - if (string.IsNullOrEmpty(commandText)) - { - throw new ArgumentException("Command text cannot be null or empty."); - } - - try - { - ensureConnectionOpen(); - var command = createCommand(commandText, parameters); - result = command.ExecuteScalar(); - } - finally - { - ensureConnectionClosed(); - } - - return result; - } - - /// - /// Executes a SQL query that returns a list of rows as the result. - /// - /// The Oracle query to execute - /// Parameters to pass to the Oracle query - /// A list of a Dictionary of Key, values pairs representing the - /// ColumnName and corresponding value - public List> Query(string commandText, IEnumerable parameters) - { + if (string.IsNullOrEmpty(commandText)) + { + throw new ArgumentException("Command text cannot be null or empty."); + } + + try + { + EnsureConnectionOpen(); + var command = CreateCommand(commandText, parameters); + result = command.ExecuteScalar(); + } + finally + { + EnsureConnectionClosed(); + } + + return result; + } + + /// + /// Executes a SQL query that returns a list of rows as the result. + /// + /// The Oracle query to execute + /// Parameters to pass to the Oracle query + /// A list of a Dictionary of Key, values pairs representing the + /// ColumnName and corresponding value + public List> Query(string commandText, IEnumerable parameters) + { List> rows; - if (string.IsNullOrEmpty(commandText)) - { - throw new ArgumentException("Command text cannot be null or empty."); - } - - try - { - ensureConnectionOpen(); - var command = createCommand(commandText, parameters); - using (var reader = command.ExecuteReader()) - { - rows = new List>(); - while (reader.Read()) - { - var row = new Dictionary(); - for (var i = 0; i < reader.FieldCount; i++) - { - var columnName = reader.GetName(i); - var columnValue = reader.IsDBNull(i) ? null : reader.GetValue(i).ToString(); - row.Add(columnName, columnValue); - } - rows.Add(row); - } - } - } - finally - { - ensureConnectionClosed(); - } - - return rows; - } - - /// - /// Opens a connection if not open - /// - private void ensureConnectionOpen() - { - var retries = 3; - if (connection.State == ConnectionState.Open) - { - return; - } - while (retries >= 0 && connection.State != ConnectionState.Open) - { - connection.Open(); - retries--; - Thread.Sleep(30); - } - } - - /// - /// Closes a connection if open - /// - private void ensureConnectionClosed() - { - if (connection.State == ConnectionState.Open) - { - connection.Close(); - } - } - - /// - /// Creates a OracleCommand with the given parameters - /// - /// The Oracle query to execute - /// Parameters to pass to the Oracle query - /// - private OracleCommand createCommand(string commandText, IEnumerable parameters) - { - var command = connection.CreateCommand(); - command.BindByName = true; - command.CommandText = commandText; - addParameters(command, parameters); - - return command; - } - - /// - /// Adds the parameters to a Oracle command - /// - /// The Oracle query to execute - /// Parameters to pass to the Oracle query - private static void addParameters(OracleCommand command, IEnumerable parameters) - { - if (parameters == null) return; - - foreach (var parameter in parameters) - { - command.Parameters.Add(parameter); - } - } - - /// - /// Helper method to return query a string value - /// - /// The Oracle query to execute - /// Parameters to pass to the Oracle query - /// The string value resulting from the query - public string GetStrValue(string commandText, IEnumerable parameters) - { - var value = QueryValue(commandText, parameters) as string; - return value; - } - - public void Dispose() - { - if (connection == null) return; - - connection.Dispose(); - connection = null; - } - } -} + if (string.IsNullOrEmpty(commandText)) + { + throw new ArgumentException("Command text cannot be null or empty."); + } + + try + { + EnsureConnectionOpen(); + var command = CreateCommand(commandText, parameters); + using (var reader = command.ExecuteReader()) + { + rows = new List>(); + while (reader.Read()) + { + var row = new Dictionary(); + for (var i = 0; i < reader.FieldCount; i++) + { + var columnName = reader.GetName(i); + var columnValue = reader.IsDBNull(i) ? null : reader.GetValue(i).ToString(); + row.Add(columnName, columnValue); + } + rows.Add(row); + } + } + } + finally + { + EnsureConnectionClosed(); + } + + return rows; + } + + /// + /// Opens a connection if not open + /// + private void EnsureConnectionOpen() + { + var retries = 3; + if (_connection.State == ConnectionState.Open) + { + return; + } + while (retries >= 0 && _connection.State != ConnectionState.Open) + { + _connection.Open(); + retries--; + Thread.Sleep(30); + } + } + + /// + /// Closes a connection if open + /// + private void EnsureConnectionClosed() + { + if (_connection.State == ConnectionState.Open) + { + _connection.Close(); + } + } + + /// + /// Creates a OracleCommand with the given parameters + /// + /// The Oracle query to execute + /// Parameters to pass to the Oracle query + /// + private OracleCommand CreateCommand(string commandText, IEnumerable parameters) + { + var command = _connection.CreateCommand(); + command.BindByName = true; + command.CommandText = commandText; + AddParameters(command, parameters); + + return command; + } + + /// + /// Adds the parameters to a Oracle command + /// + /// The Oracle query to execute + /// Parameters to pass to the Oracle query + private static void AddParameters(OracleCommand command, IEnumerable parameters) + { + if (parameters == null) return; + + foreach (var parameter in parameters) + { + command.Parameters.Add(parameter); + } + } + + /// + /// Helper method to return query a string value + /// + /// The Oracle query to execute + /// Parameters to pass to the Oracle query + /// The string value resulting from the query + public string GetStrValue(string commandText, IEnumerable parameters) + { + var value = QueryValue(commandText, parameters) as string; + return value; + } + + public void Dispose() + { + if (_connection == null) return; + + _connection.Dispose(); + _connection = null; + } + } +} diff --git a/Src/SampleWebSite/App_Start/IdentityConfig.cs b/Src/SampleWebSite/App_Start/IdentityConfig.cs index f2d5ef2..f006f4a 100644 --- a/Src/SampleWebSite/App_Start/IdentityConfig.cs +++ b/Src/SampleWebSite/App_Start/IdentityConfig.cs @@ -158,30 +158,29 @@ public Task SendAsync(IdentityMessage message) //This is useful if you do not want to tear down the database each time you run the application. - public class ApplicationDbInitializer : IDisposable + public sealed class ApplicationDbInitializer : IDisposable { - private static readonly object thisLock = new object(); - private static ApplicationDbInitializer applicationDbInitializer; + private static readonly object ThisLock = new object(); + private static volatile ApplicationDbInitializer _applicationDbInitializer; - private bool isInitialized; - private ApplicationDbInitializer() + private bool _isInitialized; + + private ApplicationDbInitializer(IOwinContext context) { + Seed(context); } public static ApplicationDbInitializer Create(IdentityFactoryOptions options, IOwinContext context) { - if (applicationDbInitializer != null) - return applicationDbInitializer; + if (_applicationDbInitializer != null) + return _applicationDbInitializer; - lock (thisLock) + lock (ThisLock) { - if (applicationDbInitializer == null) - { - applicationDbInitializer = new ApplicationDbInitializer(); - applicationDbInitializer.Seed(context); - } + if (_applicationDbInitializer != null) return _applicationDbInitializer; + _applicationDbInitializer = new ApplicationDbInitializer(context); } - return applicationDbInitializer; + return _applicationDbInitializer; } /// @@ -190,19 +189,18 @@ public static ApplicationDbInitializer Create(IdentityFactoryOptions private void Seed(IOwinContext context) { - if (isInitialized) return; + if (_isInitialized) return; InitializeDb(context); InitializeIdentity(context); - isInitialized = true; + _isInitialized = true; } // Verify Db or Tables and Create it, If you need. - private void InitializeDb(IOwinContext context) + private static void InitializeDb(IOwinContext context) { // var oracleDatabase = context.Get() as OracleDatabase; - // e.g. Run the DDL if Table is not. } @@ -237,7 +235,7 @@ private static void InitializeIdentity(IOwinContext context) } } - bool disposed = false; + bool _disposed; public void Dispose() { @@ -245,9 +243,9 @@ public void Dispose() GC.SuppressFinalize(this); } - protected virtual void Dispose(bool disposing) + private void Dispose(bool disposing) { - if (disposed) return; + if (_disposed) return; if (disposing) { @@ -255,7 +253,7 @@ protected virtual void Dispose(bool disposing) } // Free any unmanaged objects here. - disposed = true; + _disposed = true; } } diff --git a/Src/SampleWebSite/Models/IdentityModels.cs b/Src/SampleWebSite/Models/IdentityModels.cs index d8b5dd5..e2541c7 100644 --- a/Src/SampleWebSite/Models/IdentityModels.cs +++ b/Src/SampleWebSite/Models/IdentityModels.cs @@ -1,6 +1,5 @@ using AspNet.Identity.Oracle; using Microsoft.AspNet.Identity; -//using Microsoft.AspNet.Identity.EntityFramework; using System.Data.Entity; using System.Security.Claims; using System.Threading.Tasks; @@ -28,12 +27,6 @@ public ApplicationDbContext(string connectionStringName) { } - //static ApplicationDbContext() { - // // Set the database intializer which is run once during application start - // // This seeds the database with admin user credentials and admin role - // Database.SetInitializer(new ApplicationDbInitializer()); - //} - public static ApplicationDbContext Create() { return new ApplicationDbContext(); }