1

请求完成后,与我的 DatabaseFactory 相关联的请求范围会释放我的数据库连接吗?

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();
        }
}
4

1 回答 1

2

如果您使用的是Ninject.Web.MVC扩展,那么一旦垃圾收集器 (GC) 收集了请求对象(从这里开始) ,这些对象就保证被释放

于 2012-07-12T03:57:11.183 回答