1

I am studying the eShopOnContainers project on Microsoft. here is a line that injects IIntegrationEventLogServicein startup via this line:

services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
                sp => (DbConnection c) => new IntegrationEventLogService(c));

you can also see the whole class and project here on Github.

I would like to know the reason for using <Func<DbConnection, IIntegrationEventLogService>>( sp => (DbConnection c) => new IntegrationEventLogService(c)) and some declaration about syntax!

Mohammad
  • 1,197
  • 2
  • 13
  • 30
  • Does this answer your question? [AddTransient, AddScoped and AddSingleton Services Differences](https://stackoverflow.com/questions/38138100/addtransient-addscoped-and-addsingleton-services-differences) – MindSwipe Apr 19 '21 at 08:10
  • @MindSwipe No, That's not my question! I ask about func<> in transient and other sections of code! I know exactly about AddScope and AddTransient and Add Singletone! – Mohammad Apr 19 '21 at 08:58
  • I mean, then it's even easier. Check out the [`Func`](https://learn.microsoft.com/en-us/dotnet/api/system.func-2?view=net-5.0) class which is part of .NET Core – MindSwipe Apr 19 '21 at 09:12
  • @MindSwipe Dear friend, I even know about Func<>! But it's hard for me to find out that code purpose! You know! I want to know why use this complex type instead of a simple one? like AddTransient() ? – Mohammad Apr 19 '21 at 09:24
  • 1
    Well, it's adding a transient function, which when called will return a new `IntegraiontEventLogService`, it's a factory function. Using GitHub's search function to search for uses of `Func` in the code results [this](https://github.com/dotnet-architecture/eShopOnContainers/blob/5e0f55cbd92842445747a50f35c467d972ba80f9/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs#L25) showing the usage – MindSwipe Apr 19 '21 at 09:29
  • @MindSwipe Thanks, your declaration was useful. But also I need more investigation on this code! – Mohammad Apr 19 '21 at 09:33

0 Answers0