Database connection life - short on demand

I have an MVC application using StructureMap for IoC.

In the application, data access is 95% through the Entity Framework; however, there are several raw ADO.NET repositories that use the instance IDbConnection

.

I am currently registering IDbConnection

with an IoC container like this ...

init.For<IDbConnection>()
    .HybridHttpOrThreadLocalScoped()
    .Use(() => this._connectionFactory.CreateConnection(config.ConnectionString));

init.Forward<IDbConnection, DbConnection>();

      

... and inject IDbConnection

into the ADO.NET source repositories where needed.

However, I am currently _connectionFactory.CreateConnection()

returning an OPEN connection with the intent to allow StructureMap to close the connection at the end of the request.

Can this be done?

Is it better to use one open connection for the lifetime of the request, or only open / close where needed and keep the connections short?

+3


source to share





All Articles