Skip to content

Add Npgsql.DependencyInjection#4521

Merged
roji merged 3 commits intonpgsql:mainfrom
roji:DataSource/DependencyInjection
Jul 3, 2022
Merged

Add Npgsql.DependencyInjection#4521
roji merged 3 commits intonpgsql:mainfrom
roji:DataSource/DependencyInjection

Conversation

@roji
Copy link
Member

@roji roji commented Jun 20, 2022

Closes #4503

/cc @ajcvickers

@roji roji marked this pull request as ready for review June 20, 2022 11:28
@roji roji requested a review from vonzshik as a code owner June 20, 2022 11:28
@roji roji marked this pull request as draft June 20, 2022 12:35
@roji
Copy link
Member Author

roji commented Jun 20, 2022

TODO: This needs to automatically pick up logging from the (application) service provider, do this after #4493.

@roji roji force-pushed the DataSource/DependencyInjection branch from d5646f6 to 1bc340f Compare June 20, 2022 13:47
@roji
Copy link
Member Author

roji commented Jun 20, 2022

Done, this is now based off of #4493.

@roji roji marked this pull request as ready for review June 20, 2022 13:53
@roji roji requested a review from Brar as a code owner June 20, 2022 13:53
@roji roji force-pushed the DataSource/DependencyInjection branch from 1bc340f to faaa013 Compare June 21, 2022 19:31
Copy link
Member

@NinoFloris NinoFloris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback but it's neat to see it all come together :)

},
dataSourceLifetime));

serviceCollection.Add(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should always prefer the ServiceDescriptor constructor taking implementationType over implementationFactory. Using factories breaks constructor graph optimizations in DI containers (MS.DI has these for instance) due to factories being opaque methods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not know that... But what are you concretely suggesting here, given that NpgsqlConnection needs to be resolved from the NpgsqlDataSource?

This registers a scoped `NpgsqlConnection` which can get injected into your controllers:

```csharp
public class MyController : Controller
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who still uses controllers /jk

We may want to swap one of these samples out for a minimal api one instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'm a bit behind the times when it comes to ASP.NET. Will do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NinoFloris check out the new samples - looks great. The automatic logging integration is kinda magical!

@roji roji requested a review from NinoFloris June 21, 2022 22:43
@roji roji force-pushed the DataSource/DependencyInjection branch from 62f49e5 to 777bb49 Compare June 21, 2022 22:51
@roji roji merged commit bd6ead8 into npgsql:main Jul 3, 2022
@roji roji deleted the DataSource/DependencyInjection branch July 3, 2022 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Npgsql.DependencyInjection package for DbDataSource DI integration

3 participants