Depedency Injection in 2sxc and EAV
Dependency Injection is a way to structure applications and to get Services or Helpers in your code.
👉 We suggest you read the introduction to Dependency Injection
How Can I Use Dependency Injection in Razor?
Previously this was reserved for internal use. Starting in 2sxc v11.11 all Razor classes have a command called GetService. This is how your code would get a service:
using ToSic.Sxc.Services; var page = GetService<IPageService>(); page.AddOpenGraph("video", "https://2sxc.org/videos/intro.mp4");
How Can I Use Dependency Injection in WebAPIs?
Just like with with Razor, 2sxc 11.11 added the same GetService to all WebAPIs.
How Can I Use Dependency Injection in Dnn Modules and Skins
How Can I Use Dependency Injection in Oqtane
Services My Code May Need
You can get just about anything from 2sxc. The most common services you'll request as of 2sxc 12 are in the ToSic.Sxc.Services namespace.
- 2sxc introduced Dependency Injection 1.1 in v9 (May 2017)
- Integrated with Oqtane DI in v12 (May 2021)
GetService<T>pattern introduced for Razor/C# in v12 (May 2021)
- Upgraded to Dependency Injection 2.1 in v13 (Dec. 2021)
- Integrated with Dnn DI in v13 (Dec. 2021)
- Created ServiceKits for v14 (June 2022)