Home
Search

Start | Blogs | Umbraco | Get service without di

2023-04-03

Umbraco

How to get services/interfaces in Umbraco 7 without dependency injection

To get a service or interface in Umbraco 7 without Dependency Injection you can use the DependencyResolver class. This is useful in classes like ApplicationEventHandler where you can't use DI, by default anyway.

Here is an example by mapping the global settings page using Umbraco Mapper:

                        
var umbracoHelper = DependencyResolver.Current.GetService();
var umbracoMapper = DependencyResolver.Current.GetService();
var globalSettingsModel = new GlobalSettingsModel();
var globalSettings = umbracoHelper.GlobalSettings;
umbracoMapper.Map(globalSettings, globalSettingsModel);