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

  • Home
  • SEARCH
  • 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 170473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:48:52+00:00 2026-05-11T12:48:52+00:00

Can someone explain dependency injection with a basic .NET example and provide a few

  • 0

Can someone explain dependency injection with a basic .NET example and provide a few links to .NET resources to extend on the subject?

This is not a duplicate of What is dependency injection? because I am asking about specific .NET examples and resources.

  • 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. 2026-05-11T12:48:53+00:00Added an answer on May 11, 2026 at 12:48 pm

    Here’s a common example. You need to log in your application. But, at design time, you’re not sure if the client wants to log to a database, files, or the event log.

    So, you want to use DI to defer that choice to one that can be configured by the client.

    This is some pseudocode (roughly based on Unity):

    You create a logging interface:

    public interface ILog {   void Log(string text); } 

    then use this interface in your classes

    public class SomeClass {   [Dependency]   public ILog Log {get;set;} } 

    inject those dependencies at runtime

    public class SomeClassFactory {   public SomeClass Create()   {     var result = new SomeClass();     DependencyInjector.Inject(result);     return result;   } } 

    and the instance is configured in app.config:

    <?xml version='1.0' encoding='utf-8'?> <configuration>   <configSections>     <section name ='unity'              type='Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,               Microsoft.Practices.Unity.Configuration'/>   </configSections>   <unity>     <typeAliases>       <typeAlias alias='singleton'                  type='Microsoft.Practices.Unity.ContainerControlledLifetimeManager,Microsoft.Practices.Unity' />     </typeAliases>     <containers>       <container>         <types>           <type type='MyAssembly.ILog,MyAssembly'                 mapTo='MyImplementations.SqlLog, MyImplementations'>             <lifetime type='singleton'/>           </type>         </types>       </container>     </containers>   </unity> </configuration> 

    Now if you want to change the type of logger, you just go into the configuration and specify another type.

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

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Piping to another process (although this won't accomplish what you… May 11, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer As you have it, it means that the thread is… May 11, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer Have a look at the Except standard query operator in… May 11, 2026 at 6:20 pm

Related Questions

Duplicate: What is aspect-oriented programming? Every time I here a podcast or read a
I was wondering what could be the size of an object of an empty
Can someone explain the mechanics of a jump table and why is would be
Can someone explain to me the advantages of using an IOC container over simply

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.