Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Examining DI in ASP.NET Core

Can we also use simple injector with unity pattern?

Can we also use simple injector with unity pattern? Aspnet core provides interface usage. However in simple injector we have passed just repository to the container. (no interface) What is the advantage of using interface together with DI?

1 Answer

James Churchill
James Churchill
Treehouse Teacher


Thanks for your question. I started with showing registering concrete types with the Simple Injector DI container in order to keep the examples as simple as possible. All of the popular .NET DI containers, including Simple Injector, support registering concrete types or registering interface/concrete type mappings.

The main benefit of registering interfaces with the DI container is that dependent classes (like our controller in the ASP.NET Core example) only take a dependency on the interface and not on a specific concrete type. This further improves the loose coupling between classes. And it allows you to easily replace the concrete type that the container will instantiate for that interface by changing the concrete type in your container configuration that's associated with that interface.

Thanks ~James

Thank you very much for your detailed explanation. This course is very beneficial for me. I have hands on experience in these topics however I noticed that I haven't learned deeply until taking this course. Thanks.

James Churchill
James Churchill
Treehouse Teacher

You're welcome and thanks for being a student :)