1

Will request scope tied to my DatabaseFactory release my database connection after the request is finished?

kernel.Bind<IDatabaseFactory>().To<DatabaseFactory<MySqlConnection>>().InRequestScope().WithConstructorArgument("connectionString", Config.Data.MySQLConnection);


public class DatabaseFactory<T> : Disposable, IDatabaseFactory where T : IDbConnection, new()
    {
        private readonly string _connectionString;
        private  IDbConnection _dataConnection;

        public DatabaseFactory(string connectionString)
        {
            _connectionString = connectionString;
        }

        #region IDatabaseFactory Members

        public IDbConnection Get()
        {
            return _dataConnection ?? (_dataConnection = new T { ConnectionString = _connectionString });
        }

        #endregion

        protected override void DisposeCore()
        {
            if (_dataConnection != null)
                _dataConnection.Dispose();
        }
}
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341

1 Answers1

2

If you are using the Ninject.Web.MVC extension then those objects are guaranteed to be disposed once the request object is collected by the Garbage Collector (GC) (from here)

Community
  • 1
  • 1
VJAI
  • 32,167
  • 23
  • 102
  • 164
  • It seems from that post by using InRequestScope, it disposes the DBFactory at the end of the request. Am I wrong in noticing that in the post you sent? – Mike Flynn Jul 12 '12 at 16:00