DevTeam / Pure.DI

Pure DI for .NET
MIT License
410 stars 21 forks source link

Asp.Net Example #29

Closed jwyza-pi closed 1 year ago

jwyza-pi commented 1 year ago

Firstly, been watching this repo to see what 2.0 was going to bring and excited to see the possibilities! Awesome work.

Second, I didn't see it in the list in the readme, but do you have an example for how this might be used in an ASP.Net 6 project. This seems like a big chunk of what would need to happen to make it AoT compatible (plus the stuff that's coming in .Net 8).

NikolayPianikov commented 1 year ago

@jwyza-pi thanks for your interest in this repo! :)

I tried not to add dependencies on any frameworks in v2 or change the concept in any way. But I tried to add enough extension points to make v2 as flexible as possible. To do this, you can change the behavior of the code generator using "Setup hints".

There are many uses for this code generator in ASP.NET. For example, you can use it to build individual compositions of objects. You can then register the corresponding "composition roots" in your Service Collection yourself. In this example, this is done automatically for all roots.

Here is example how to create a Service Provider.

All external dependencies (e.g. logger) can be added to the composition as arguments.

You can use OnCannotResolve hint

If you have a small example of a "template" ASP.NET application that is both simple and shows the essence of ASP.NET, please add it as a zip archive here and describe what you want to get.

I will add this example to this section.

NikolayPianikov commented 1 year ago

I added sample Web API using Asp Net Core 7. I will add a parts description for this example later. Maybe it will help in some questions. In any case, please send your example. If we can't do anything with it right away, we will try to figure out how to do it well :)

jwyza-pi commented 1 year ago

Thanks, it might be a day or 2, but I'll get you an example scenario.

NikolayPianikov commented 1 year ago

@jwyza-pi regarding AOT - this is an ambitious task in my opinion. Now ServiceCollection for web app contains about 300 registrations, some of which are realized by internal types. To create a composition all objects of these types must be able to be created in the composition class. And I have a feeling that you can try to make a fork of the ASP.NET project and type by type carefully build the composition directly in ASP.NET modules.

jwyza-pi commented 1 year ago

No doubt it'll be a fair bit of effort, but mostly been watching the ASP.Net 8.0 preview where they are expanding to (almost) full AoT support: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/native-aot?view=aspnetcore-8.0&preserve-view=true.

That said, my primary thing was wanting to be able to use the PureDI even in non-aot scenarios. After looking at your example, that probably is going to be sufficient to do that (since the real question was how to tie it into the asp.net DI chain so that it would use PureDI for resolving all the things that it can resolve).

For now, I think your samples and responses are sufficient for me. I don't know when I'm going to be able to try and use this in a real world scenario (ya know how it goes in development), but definitely am going to keep an eye on this and hopefully get to play with it in the coming months.