CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.11k stars 174 forks source link

ICarterModule as Singleton services? #288

Closed KevinValmo closed 2 years ago

KevinValmo commented 2 years ago

Hello.

This could be a discussion or a feature that could be added or not...

I was thinking about "old" Controllers and ControllerBases on the Mvc namespace. As the "old" Controllers are by default transient service (not always), don't you think to preserve this behaviur by adding to Services the ICarterModules as transient? Isn't it better to mark modules as transient lifetime services? Or it could be better maybe to let devs configure the lifetime of those ICarterModules service within an implementation of a ICarterModuleFactory or somethig else.

https://github.com/CarterCommunity/Carter/blob/5dc8a5e4d03ec91221d16b9faf41f7d1c9727f89/src/Carter/CarterExtensions.cs#L76-L79

jchannon commented 2 years ago

Due to the nature of minimal APIs all dependencies in a route delegate are resolved at runtime therefore modules cannot take any dependencies in their constructor as they will not be resolved when a route is hit. Carter modules could essentially be static classes with the routes defined in them. You could potentially inject singletons into a module but I don’t think I’d advise it just so it doesn’t get confusing

On Mon, 22 Nov 2021 at 23:15, Kevin Valmorbida @.***> wrote:

Hello.

This could be a discussion or a feature that could be added or not...

I was thinking about "old" Controllers and ControllerBases on the Mvc namespace. As the "old" Controllers are by default transient service (not always), don't you think to preserve this behaviur by adding to Services the ICarterModules as transient? Isn't it better to mark modules as transient lifetime services? Or it could be better maybe to let devs configure the lifetime of those ICarterModules service within an implementation of a ICarterModuleFactory or somethig else.

https://github.com/CarterCommunity/Carter/blob/5dc8a5e4d03ec91221d16b9faf41f7d1c9727f89/src/Carter/CarterExtensions.cs#L76-L79

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/288, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJR3K4ZCV6SFWW6HKLLUNLFHJANCNFSM5ISFUVBQ .