Code Monkey home page Code Monkey logo

frameworkeol-dependencyinjection's Introduction

Microsoft.Extensions.DependencyInjection

Microsoft Dependency Injection Framework For Framework 4.5 & 4.6


Description
CREATED BY: Latency McLaughlin
UPDATED: 4/11/2024
FRAMEWORK: [net452], [net46], [net461] ([Latest])
LANGUAGE: [C#] preview
OUTPUT TYPE: Library [API]
SUPPORTS: [Visual Studio]
GFX SUBSYS: [None]
TAGS: [API], [.NET], [C#], [Dependancy Injection]
STATUS .NET Core Desktop Build
LICENSE: License
VERSION: GitHub Release

Navigation


The code is same as .net core's Microsoft.Extensions.DependencyInjection. I just re-compiled the code to framewrok 4.5.1 and add two project to help use it.

In the solution, it includes some examples it shows how to use the DI in console application, asp.net Mvc or webform.

+ Microsoft.Extensions.DependencyInjection + Microsoft.Extensions.DependencyInjection.Abstractions + Microsoft.Extensions.DependencyInjection.ConsoleApp + Microsoft.Extensions.DependencyInjection.SystemWeb

In this solution, The both of projects Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.DependencyInjection.Abstractions thest code is same as Microsoft's DependencyInjection

The both of projects Microsoft.Extensions.DependencyInjection.ConsoleApp and Microsoft.Extensions.DependencyInjection.SystemWeb contains some usage codes it can help us to use the library in different solution (Console Application, WinForm, WebForm or Asp.net Mvc).

Console Application (or WinForm)

If you want to use the DI project in your console application or Winform, First the project need to reference Microsoft.Extensions.DependencyInjection,Microsoft.Extensions.DependencyInjection.Abstractions and Microsoft.Extensions.DependencyInjection.ConsoleApp projects. Second, you just need to add small codes in Program.Main, the code exmaple:

static void Main(string[] args)
{
    //Init the DI container
    DependencyInjectionStartup.Initialize(services =>
    {
        services.AddSingleton<IDao, Dao>();
    });

    //Get instnace by service type from DI container
    var dao = DIProviderInstance.ProviderInstance.GetRequiredService<IDao>();

    Console.WriteLine(dao.GetWriter());
    Console.ReadKey();
}

Asp.net Mvc (or Asp.net WebAPI)

If you want to use the DI project in your web application (Asp.net Mvc or Asp.net WebAPI), you need to have some Front-facing step.

  • Add nuget package Microsoft.Owin.Host.SystemWeb.
  • Add reference Microsoft.Extensions.DependencyInjection,Microsoft.Extensions.DependencyInjection.Abstractions and Microsoft.Extensions.DependencyInjection.SystemWeb
  • Add a Owin Startup class.
  • Extends the abstract class DependencyInjectionStartup (Included in Microsoft.Extensions.DependencyInjection.SystemWeb).
  • Add class Startup's construct method and add below code
base.Initialize();
  • Implement the abstract method ServiceConfiguration, you can add object to DI container in the method.
  • Add middleware in method Configuration. Example code:
public class Startup : DependencyInjectionStartup
{
    public Startup()
    {
        base.Initialize();
    }

    public void Configuration(IAppBuilder app)
    {
        app.Use<DependencyInjectionMiddleware>(base.services);
    }

    protected override void ServiceConfiguration(IServiceCollection services)
    {
        services.AddSingleton<IDao, Dao>();
    }
}

//Get object instance by service Type from DI container.
var dao = ServiceResolve.ResolveOwinFactory(HttpContext.GetOwinContext()).GetRequiredService<IDao>();

WebForm

If you want to use the DI project in your web application (Asp.net WebForm), you need to have some Front-facing step.

  • Add nuget package Microsoft.Owin.Host.SystemWeb.
  • Add reference Microsoft.Extensions.DependencyInjection,Microsoft.Extensions.DependencyInjection.Abstractions and Microsoft.Extensions.DependencyInjection.SystemWeb
  • Add a Owin Startup class.
  • Extends the abstract class DependencyInjectionStartup (Included in Microsoft.Extensions.DependencyInjection.SystemWeb).
  • Add class Startup's construct method and add below code
base.Initialize();
  • Implement the abstract method ServiceConfiguration, you can add object to DI container in the method.
  • Add middleware in method Configuration. Example code:
public class Startup : DependencyInjectionStartup
{
    public Startup()
    {
        base.Initialize();
    }

    public void Configuration(IAppBuilder app)
    {
        app.Use<DependencyInjectionMiddleware>(base.services);
    }

    protected override void ServiceConfiguration(IServiceCollection services)
    {
        services.AddSingleton<IDao, Dao>();
    }
}

//Get object instance by service Type from DI container.
var dao = ServiceResolve.ResolveHttpFactory(HttpContext.Current).GetRequiredService<IDao>();

This library can be installed using NuGet found here.

The source code for the site is licensed under the MIT license, which you can find in the MIT-LICENSE.txt file.

All graphical assets are licensed under the Creative Commons Attribution 3.0 Unported License.

frameworkeol-dependencyinjection's People

Contributors

latency avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.