Using DatabaseFactory.CreateDatabase() or EnterpriseLibraryContainer.Current.GetInstance<Database>()

Topics: Data Access Application Block
Apr 3, 2012 at 2:00 PM

I am using Enterprise library data access block 5.0

In my application currently I am usingfollowing code

	  Database objDb;
            try
            {
                objDb = DatabaseFactory.CreateDatabase();
                //objDb = EnterpriseLibraryContainer.Current.GetInstance();
                using (DbCommand objCmd = objDb.GetStoredProcCommand("uspGetEntryPlanningSelectedRecordContainers"))
                {
                    objCmd.CommandTimeout = 900;
                    objDb.AddInParameter(objCmd, "@JobID", DbType.String, objEntry.MAJobID);                    
                    using (IDataReader objDataReader = objDb.ExecuteReader(objCmd))
                    {
                        if (objDataReader != null)
                        {
                            obj = ChangeToBusinessObject.FillCollection(objDataReader);
                        }
                    }
                    objCmd.Dispose();
                }
            }
            catch (Exception ex)
            {
                LoggingHandler.Log(ex);
                throw;
            }
            finally
            { 
				objDb = null;			
            }

i.e i am using DatabaseFactory.CreateDatabase() to create an instance and putting it null in finally block
Please let me know am i doing correctly or not

OR should I use EnterpriseLibraryContainer.Current.GetInstance
();
and remove line objDb = null from finally block

 

 




 

 



 

 

Apr 3, 2012 at 3:28 PM
Edited Apr 3, 2012 at 3:30 PM

DatabaseFactory.CreateDatabase() and EnterpriseLibraryContainer.Current.GetInstance() are functionally equivalent so you can use both methods.  I.e. CreateDatabase() actually calls GetInstance<Database>().  (The only difference being that CreateDatabase potentially logs a message in the case of an exception.)

But as described in Creating and Referencing Enterprise Library Objects: "legacy static facades and factories that were the default approach in versions of Enterprise Library prior to version 5.0 are still available, and continue to be supported for the purpose of backwards compatibility. However, new code should use either the service locator approach or the techniques for accessing the container directly, as described in previous sections of this topic".  So, the preferred method in Enterprise Library 5 is to use the service locator or dependency injection approach.

> remove line objDb = null from finally block

In general, you should not set local variables to null -- there is rarely a need since .NET will manage that for you.  See Should I assign null to my local variables?

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Apr 3, 2012 at 7:44 PM

Thanks randylevy.

Please let me know what is mean't by

The only difference being that CreateDatabase potentially logs a message in the case of an exception

What error i might not get with new approach.also Suppose i am not using enterprise library logging block.

Apr 4, 2012 at 2:22 AM
kamii47 wrote:

Thanks randylevy.

Please let me know what is mean't by

The only difference being that CreateDatabase potentially logs a message in the case of an exception

What error i might not get with new approach.also Suppose i am not using enterprise library logging block.

They are functionally equivalent so you will receive the same errors from either method.  The logging is conditional and does not depend on the Logging Application Block.  It is based on the Instrumentation Settings; if the settings section does not exist or it is configured and Event Log Enabled is set to false then nothing will attempt to be written to the Event Log.  In the event of an exception attempting to log, the exception is swallowed and the original exception is (still) thrown.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com