Table of Contents

Dependency 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

Prefer Kit do GetService()

Important

It's recommended to use Kit instead of GetService() directly for better maintainability and readability.

The Kit object has almost all the services you'll ever need.

So the example which will be shown below, would usually be written like this:

@inherits Custom.Hybrid.RazorTyped
@Kit.Page.AddOpenGraph("video", "https://2sxc.org/videos/intro.mp4");

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:

@inherits Custom.Hybrid.Razor12
@GetService<ToSic.Sxc.Services.IPageService>().AddOpenGraph("video", "https://2sxc.org/videos/intro.mp4");

Discover More

GetService<T>() Command new in v11.11
GetService
ToSic.Sxc.Services
Dependency Injection in Dnn Skins and Modules
Dependency Injection in Oqtane

FAQ

  1. How Can I Use Dependency Injection in WebAPIs?
    Just like with with Razor, 2sxc 11.11 added the same GetService to all WebAPIs.

History of Dependency Injection in 2sxc

  1. 2sxc introduced Dependency Injection 1.1 in v9 (May 2017)
  2. Integrated with Oqtane DI in v12 (May 2021)
  3. GetService<T> pattern introduced for Razor/C# in v12 (May 2021)
  4. Integrated with Oqtane DI in v12 (May. 2021)
  5. Upgraded to Dependency Injection 2.1 in v13 (Dec. 2021)
  6. Integrated with Dnn DI in v13 (Dec. 2021)
  7. Created ServiceKits for v14 (June 2022)
  8. Added ServiceKitLight for custom DataSources in 2sxc 15
  9. Added ServiceKit16 for 2sxc 16