SlideShare a Scribd company logo
Dependency Injection in Episerver and .Net
Dependency Injection
in Episerver
Dependency Injection in Episerver and .Net
Dependency Injection
in .Net
Dependency Injection in Episerver and .Net
wałdis iljuczonok (aka technical fellow)
tech guy at getadigital.com
(Microsoft .Net + Episerver) ◦ MVP
@tech_fellow
Why?
Testability
Dependency Injection in Episerver and .Net
@tech_fellow
What is dependency?
public class Class1
{
public Class1()
{
}
public Answer Method1(string directory)
{
// ....
return ...;
}
}
@tech_fellow
«DI is a set of principles and patterns that enable loose
coupling* »
* - http://blog.ploeh.dk/2010/04/07/DependencyInjectionisLooseCoupling/
@tech_fellow
s.o.l.i.d.
@tech_fellow
s.o.l.i.d.
Patterns
public class Class1
{
private readonly ISomeService _service;
public Class1(ISomeService service)
{
_service = service;
}
public IAnotherService AnotherService { private get; set; }
public Answer Method1(IThirdService thridService)
{
return thridService.CouldBe() ? Answer.Yes() : Answer.No();
}
}
Constructor Injection
Property Injection
Method Injection
public static class AmbientContext
{
public static ISomeService TheService { get; } = new SomeService();
}
Ambient Context
Dependency Injection in Episerver and .Net
@tech_fellow
Composition Root
Layered
Onion
hexagonal
pizza
@tech_fellow
Main(string[] args)
Global.asax & IControllerFactory
Startup.cs & RouterMiddleware
..rely on framework..
@tech_fellow
IConfigurableModule
IInitializableModule*
Anti-Patterns
@tech_fellow
Control Freak
public class Class1
{
private readonly ISomeService _service;
public Class1(ISomeService service)
{
_service = service;
}
public IAnotherService AnotherService { private get; set; }
public Answer Method1(IThirdService thridService)
{
return thridService.CouldBe() ? Answer.Yes() : Answer.No();
}
}
public class Class1
{
private readonly ISomeService _service;
public Class1()
{
_service = new SomeService();
}
public IAnotherService AnotherService { private get; set; }
public Answer Method1()
{
var thridService = new ThirdService();
return thridService.CouldBe() ? Answer.Yes() : Answer.No();
}
}
@tech_fellow
Control Freak
Bastard Injections
public class Class1
{
private readonly ISomeService _service;
public Class1()
{
_service = new SomeService();
}
public IAnotherService AnotherService { private get; set; }
public Answer Method1()
{
var thridService = new ThirdService();
return thridService.CouldBe() ? Answer.Yes() : Answer.No();
}
}
public class Class1
{
private readonly ISomeService _service;
public Class1(ISomeService service)
{
_service = service;
}
public Class1()
{
_service = new SomeService();
}
public IAnotherService AnotherService { private get; set; }
public Answer Method1()
{
var thridService = new ThirdService();
return thridService.CouldBe() ? Answer.Yes() : Answer.No();
}
}
@tech_fellow
Control Freak
Bastard Injections
Constrained Construction
Dependency Injection in Episerver and .Net
@tech_fellow
Control Freak
Bastard Injections
Constrained Construction
Service Locator
Dependency Injection in Episerver and .Net
Dependency Injection in Episerver and .Net
[Fact]
public void TestMethod1()
{
var kpi = new Kpi();
kpi.Evaluate(this, new EventArgs());
...
}
@tech_fellow
Dependency Injection in Episerver and .Net
@tech_fellow
T Injected<T>
T ServiceAccessor<out T>
Dependency Injection in Episerver and .Net
Don’t ask, we will call you!
Hollywood Principle
DI in Episerver
@tech_fellow
Scheduled Jobs
@tech_fellow
Configurable Modules
&
Initializable Modules
@tech_fellow
Episerver Forms Actors
Dependency Injection in Episerver and .Net
@tech_fellow
Razor Page*
@tech_fellow
Extension Methods
@tech_fellow
Episerver Developer Tools AddOn
DI Friendly Libraries
@tech_fellow
Libraries are designed to be «consumed»
@tech_fellow
Conforming Container
(anti-pattern)
@tech_fellow
FeatureSwitch
*.Ninject
*.StructureMap
*.Unity
*.<your-container>
*.Ninject
*.StructureMap
*.Unity
*.<your-container>
@tech_fellow
AbstractFactory
public delegate object SingleInstanceFactory(Type target);
public class TypeAbstractFactory
{
private readonly SingleInstanceFactory _factory;
public TypeAbstractFactory(SingleInstanceFactory factory)
{
_factory = factory;
}
private void CreateTargetType()
{
var s = (ISomeService)_factory(typeof(ISomeService));
}
}
context.Container.Configure(
c => c.For<SingleInstanceFactory>()
.Use<SingleInstanceFactory>(ctx => ctx.GetInstance));
public delegate object SingleInstanceFactory(Type target);
public class TypeAbstractFactory
{
private readonly SingleInstanceFactory _factory;
public TypeAbstractFactory(SingleInstanceFactory factory)
{
_factory = factory;
}
private void CreateTargetType()
{
var s = (ISomeService)_factory(typeof(ISomeService));
}
}
@tech_fellow
Constructor Over-Injection
DI Friendly Framework
@tech_fellow
[ServiceConfiguration( … )]
Dependency Injection in Episerver and .Net
@tech_fellow
RRR
Register-(Resolve-Release)
public delegate object SingleInstanceFactory(Type target);
public class TypeAbstractFactory
{
private readonly SingleInstanceFactory _factory;
public TypeAbstractFactory(SingleInstanceFactory factory)
{
_factory = factory;
}
private void CreateTargetType()
{
var s = (ISomeService)_factory(typeof(ISomeService));
}
}
@tech_fellow
Remember to Release
Extra Bonus with DI
@tech_fellow
Aspects
(Interceptors & Decorators)
russian doll model
https://cynthianewberrymartin.files.wordpress.com/2009/05/blog-2-13.jpg
@tech_fellow
Validation
Pre/post processing
Authorization
Logging
Auditing
Event dispatching
Notifications
Unit of work/transactions
...
@tech_fellow
Create loose-coupling code
Demand dependencies from consumer
Leverage DI container features
Look for anti-patterns
@tech_fellow
Be a BOY SCOUT!
Dependency Injection in Episerver and .Net

More Related Content

PPTX
SQL Saturday 28 - .NET Fundamentals
PPTX
Top 20 java programming interview questions for sdet
PDF
DBMask: Fine-Grained Access Control on Encrypted Relational Databases
PDF
Mutation Testing at BzhJUG
PPT
Integrating Erlang and Java
PPT
Java™ (OOP) - Chapter 6: "Arrays"
PDF
IOC in unity
PPTX
1. Mini seminar intro
SQL Saturday 28 - .NET Fundamentals
Top 20 java programming interview questions for sdet
DBMask: Fine-Grained Access Control on Encrypted Relational Databases
Mutation Testing at BzhJUG
Integrating Erlang and Java
Java™ (OOP) - Chapter 6: "Arrays"
IOC in unity
1. Mini seminar intro

What's hot (20)

PPTX
Testers guide to unit testing
PDF
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
PDF
Static program analysis tools
PPT
The Erlang Programming Language
PDF
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
PPTX
Rock Your Code with Code Contracts
PDF
Poster on Automated Refactoring of Legacy Java Software to Default Methods
PPTX
Back-2-Basics: .NET Coding Standards For The Real World (2011)
PPTX
TDD Training
PDF
Exploring lambdas and invokedynamic for embedded systems
PPTX
Pj01 4-operators and control flow
DOCX
Simulado java se 7 programmer
ODP
Microservices Pain: Testing
PPTX
Test driven development
PPTX
PHPUnit - Unit testing
PDF
Pyconie 2012
PDF
Clean Code 2
PDF
Integration of Cincom Smalltalk Systems
PPTX
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
 
PDF
Mutate and Test your Tests with STAMP, Caroline Landry, Inria, Paris Open Sou...
Testers guide to unit testing
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Static program analysis tools
The Erlang Programming Language
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Rock Your Code with Code Contracts
Poster on Automated Refactoring of Legacy Java Software to Default Methods
Back-2-Basics: .NET Coding Standards For The Real World (2011)
TDD Training
Exploring lambdas and invokedynamic for embedded systems
Pj01 4-operators and control flow
Simulado java se 7 programmer
Microservices Pain: Testing
Test driven development
PHPUnit - Unit testing
Pyconie 2012
Clean Code 2
Integration of Cincom Smalltalk Systems
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
 
Mutate and Test your Tests with STAMP, Caroline Landry, Inria, Paris Open Sou...
Ad

Similar to Dependency Injection in Episerver and .Net (20)

PDF
Dependency Injection
PPTX
Oleksandr Valetskyy - DI vs. IoC
PPTX
Dependency injection with unity 2.0 Dmytro Mindra Lohika
PPTX
Introduction to Dependency Injection
PPTX
Dependency Injection або Don’t call me, I’ll call you
PPTX
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
PPTX
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
PPTX
Clean Code Part II - Dependency Injection at SoCal Code Camp
PPTX
I gotta dependency on dependency injection
PPTX
Polaris presentation ioc - code conference
PPTX
Design for testability as a way to good coding (SOLID and IoC)
PPTX
Dependency Injection
PPTX
How to implement dependency injection in c#
PPTX
Clean Code II - Dependency Injection
PDF
Slaying Sacred Cows: Deconstructing Dependency Injection
PPTX
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
PDF
Dependency Injection with Unity Container
PPTX
Poco Es Mucho: WCF, EF, and Class Design
PPTX
Dependency Inversion Principle
PPTX
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Dependency Injection
Oleksandr Valetskyy - DI vs. IoC
Dependency injection with unity 2.0 Dmytro Mindra Lohika
Introduction to Dependency Injection
Dependency Injection або Don’t call me, I’ll call you
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Clean Code Part II - Dependency Injection at SoCal Code Camp
I gotta dependency on dependency injection
Polaris presentation ioc - code conference
Design for testability as a way to good coding (SOLID and IoC)
Dependency Injection
How to implement dependency injection in c#
Clean Code II - Dependency Injection
Slaying Sacred Cows: Deconstructing Dependency Injection
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Dependency Injection with Unity Container
Poco Es Mucho: WCF, EF, and Class Design
Dependency Inversion Principle
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Ad

More from Valdis Iljuconoks (11)

PPTX
Loving data with F#
PPTX
Lap around Visual Studio 2013
PPTX
Client side development with knockout.js
PPTX
Asp.Net MVC overview
PPTX
CSharp 5 Async
PPTX
Lean Development with TFServices
PPTX
Introduction to FSharp
PPTX
Parallel development in VS 2012
PPTX
Windows phone development
PPTX
Knockoutjs UG meeting presentation
PPTX
Parallel Development in VS10
Loving data with F#
Lap around Visual Studio 2013
Client side development with knockout.js
Asp.Net MVC overview
CSharp 5 Async
Lean Development with TFServices
Introduction to FSharp
Parallel development in VS 2012
Windows phone development
Knockoutjs UG meeting presentation
Parallel Development in VS10

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
A Presentation on Artificial Intelligence
PPTX
Spectroscopy.pptx food analysis technology
PDF
Approach and Philosophy of On baking technology
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Big Data Technologies - Introduction.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MIND Revenue Release Quarter 2 2025 Press Release
Network Security Unit 5.pdf for BCA BBA.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Programs and apps: productivity, graphics, security and other tools
A Presentation on Artificial Intelligence
Spectroscopy.pptx food analysis technology
Approach and Philosophy of On baking technology
A comparative analysis of optical character recognition models for extracting...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
NewMind AI Weekly Chronicles - August'25-Week II
“AI and Expert System Decision Support & Business Intelligence Systems”
Digital-Transformation-Roadmap-for-Companies.pptx
Spectral efficient network and resource selection model in 5G networks
Unlocking AI with Model Context Protocol (MCP)
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Per capita expenditure prediction using model stacking based on satellite ima...

Dependency Injection in Episerver and .Net

Editor's Notes

  • #14: DI fuels SOLID
  • #15: DI fuels SOLID Dependency Inversion Principle (the D in SOLID) is the guiding principle behind DI.
  • #20: Compose whole graph and get DI container out of way in layers below. Read bottom-up = to understand dependency graph and required dependencies for other services.
  • #28: Cannot replace dependencies after application has been compiled (in unit tests). Drags unnecessary dependencies.
  • #31: Main problem – it uses FOREIGN DEFAULT
  • #34: We lose flexibility. Fx does not know what exactly component will need for composition.
  • #36: The essence here – is not how component is constructed, but how it’s used.
  • #49: EPiServer.Forms.Core.PostSubmissionActor.ActorsExecutingService
  • #64: Refactor to Facasde Services, makes it easy to see and react on violation of SRP.
  • #65: Frameworks are designed with Dependency Inversion in mind. You implement interface, register it (in composition root) and framework calls you. Framework is the consumer of the client code. It CREATES instances of the client code!
  • #66: It hides registration details that would be explicitly visible in composition root (no room for 2 in container) How to unregister (clear out) previous registrations for an object? It’s hard to know lifetime of the dependency (wrong assumptions could be made)
  • #72: Respects Open/Closed Principle, Liskov comes into play