Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8747809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:25:07+00:00 2026-06-13T12:25:07+00:00

We all write code with some patterns even when we dont realise it. I

  • 0

We all write code with some patterns even when we dont realise it. I am trying to really understand some of the S.O.L.I.D principles and how you apply these principles in the real world.

I am struggling with “D“.

I sometimes confuse Dependency Inversion with Dependency Injection. Does it mean that as long as you keep things depending on abstraction (IE:interfaces) you are done.

Does anybody have even a small C# example that explains it?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:25:08+00:00Added an answer on June 13, 2026 at 12:25 pm

    Have a look at Mark Seeman’s blog or, even better, buy his book. It covers so much more than just DI. I appreciate you probably just want a simple sample to get going with. However, it’s a subject that many who claim to understand don’t and therefore worth learning well.

    That said, here’s a very simple example. The terminology, as I understand it, is
    Inversion of Control and Dependency Injection. Inversion of Control refers to the fact that you give control of a class’s dependencies to some other class, opposed to the class controlling the dependency itself, usually via the new keyword. This control is exerted via Dependency Injection where a class is given, or injected, with its dependencies. This can be done via an IoC framework or in code (known as Pure DI). Injection can be performed in the class’s constructor, via a property or as a method’s parameter. Dependencies can be any type, they don’t have to be abstract.

    Here’s a class that lists Tour de France winners who haven’t doped:

    class CleanRiders
    {
        List<Rider> GetCleanRiders()
        {
            var riderRepository = new MsSqlRiderRepository();
    
            return riderRepository.GetRiders.Where(x => x.Doping == false);
        }
    }
    

    This class is dependent on the MsSqlRiderRepository. The class takes control of the creation of the instance. The problem is that this dependency is inflexible. It’s hard to change it to a OracleRiderRepository or a TestRiderRepository.

    IoC and DI solve this for us:

    class CleanRiders
    {
        private IRiderRepository _repository;
    
        public CleanRiders(IRiderRepository repository)
        {
            _repository = repository;
        }
    
        List<Rider> GetCleanRiders()
        {
            return _repository.GetRiders.Where(x => x.Doping == false);
        }
    }
    

    Now the class is only depending on an Interface. Control of the dependency has been given up to the class’s creator and must be injected via its constructor:

    void Main()
    {
        var c = new CleanRiders(new MsSqlRepository());
    
        var riders = c.GetRiders();
    }
    

    Arguably, a more flexible, testable and SOLID approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some code to catch all Errors and ErrorEvents however i
I need to write some code that would loop though all rows of a
I write my own code to index some data files myself. It was all
I understand these are necessary...of course to write proper code, but is there a
I wrote a simple plugin which sets some css code using wp_options. It all
How could I write code to get all the action names from a controller
I write the following code all the time to handle when the enter key
I'm trying to restructure some specific GUI code in a Java project, let's call
Some code needs to be refactored in approx 10 classes, of these classes there
I have a quad core machine and would like to write some code to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.