Closed amingo123 closed 4 years ago
The main reason is to use the same connection during "request" handling. It is solved using IoC container:
builder.RegisterType<SqlConnectionFactory>()
.As<ISqlConnectionFactory>()
.WithParameter("connectionString", _databaseConnectionString)
.InstancePerLifetimeScope();
Request means HTTP request or internal command processing.
The main reason is to use the same connection during "request" handling. It is solved using IoC container:
builder.RegisterType<SqlConnectionFactory>() .As<ISqlConnectionFactory>() .WithParameter("connectionString", _databaseConnectionString) .InstancePerLifetimeScope();
Request means HTTP request or internal command processing.
Thanks for your reply. I think the same connection determined by InstancePerLifetimeScope,
the IDbConnection can be registered the same way.
builder.RegisterType
You mean this?
builder.RegisterType<SqlConnection>()
.As<IDbConnection>()
.WithParameter("connectionString", _databaseConnectionString)
.InstancePerLifetimeScope();
This has some issues:
If the SqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close.
- When the connection is closed, should be reopened if necessary
- In factory you can configure other options like timeout etc.
You mean this?
builder.RegisterType<SqlConnection>() .As<IDbConnection>() .WithParameter("connectionString", _databaseConnectionString) .InstancePerLifetimeScope();
This has some issues:
- When the connection is opened?
- You need to explicitly close connection according to https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.open?view=netframework-4.8 :
If the SqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close.
- When the connection is closed, should be reopened if necessary
- In factory you can configure other options like timeout etc.
Thanks for your explanation!
Very great DDD sample.
I have one question confuse me. why not use IDbConnection directly but use ISqlConnectionFactory to wrap a connection? Any concern about use IDbConnection ?
https://github.com/kgrzybek/sample-dotnet-core-cqrs-api/blob/master/src/SampleProject.API/Customers/DomainServices/CustomerUniquenessChecker.cs#L9
Thank you.