DI for Functions–idea – part 1

Looking at ASP.NET Core,there is a wonderful feature that  gives you thinking :  you can put in any action for a controller FromServices argument and the framework will complete from,well,services: :

public ActionResult Test([FromServices] MyFunction

What if  you can do the same with any function from any class ?

It will be good,but … how  ?  ASP.NET Core instantiate himself the functions,but I cannot do this. 

I can generate with Roslyn a function that takes not DI arguments . For example,from

public bool TestMyFunc1([FromServices] TestDI1 t1,[FromServices] TestDI2 t2,int x,int y)

Roslyn can generate this

public bool TestMyFunc1(int  x,int  y)

And call the previous function – but HOW we can retrieve the arguments ?

As I see,there are 2 options:

1.  Generate a constructor that have as a parameter the ServiceProvider and find the services from ServiceProvider

2. Generate a constructor that have the DI arguments and assign them as fields .

Now go to work!


Posted

in

, , ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *