Coder Social home page Coder Social logo

dependencyinjection's Introduction

Dependency Injection (simple and fast)

Example1

var value = new object();
var injector = new Injector();
injector.ToValue(value);
Assert.True( value == injector.Resolve<object>() );

Example2

var random1 = new Random(0);
var injector = new Injector();
injector.ToValue<int>(() => random1.Next());

var random1Value1 = injector.Resolve<int>();
var random1Value2 = injector.Resolve<int>();
var random1Value3 = injector.Resolve<int>();

var random2 = new Random(0);
var random2Value1 = random2.Next();
var random2Value2 = random2.Next();
var random2Value3 = random2.Next();

Assert.Equal(random1Value1, random2Value1);
Assert.Equal(random1Value2, random2Value2);
Assert.Equal(random1Value3, random2Value3);

Example3

public interface ITestValue {  }

public class TestValue : ITestValue
{
  [Inject]
  public int Value;
}

public interface ITestInterface {  }

public class TestImplementation : ITestInterface
{
  public ITestValue Value;
}

public class TestClass
{
  [Inject]
  private ITestInterface _testPivateField;

  [Inject]
  public ITestInterface TestProperty { get; set; }

  [Inject]
  public TestValue TestValue;

  public ITestInterface TestPivateField => _testPivateField;
}

var injector1 = new Injector();
injector1.ToValue<int>(10);
injector1.ToFactory<ITestValue, TestValue>();
injector1.ToSingleton<TestValue>();

Assert.Equal(10, injector1.Resolve<int>());
Assert.NotEqual(injector1.Resolve<ITestValue>(), injector1.Resolve<TestValue>());
Assert.Equal(injector1.Resolve<TestValue>(), injector1.Resolve<TestValue>());

var injector2 = new Injector(injector1);
injector2.ToFactory<TestClass>();
injector2.ToFactory<ITestInterface, TestImplementation>();

var testClass1 = new TestClass();
injector2.Inject(testClass1);

Assert.Equal(10, injector2.Resolve<int>());

Assert.NotNull(testClass1.TestValue);
Assert.NotNull(testClass1.TestPivateField);
Assert.NotNull(testClass1.TestProperty);
Assert.NotEqual(testClass1.TestPivateField, testClass1.TestProperty);

var testClass2 = injector2.Resolve<TestClass>();

Assert.NotNull(testClass2.TestValue);
Assert.NotNull(testClass2.TestPivateField);
Assert.NotNull(testClass2.TestProperty);
Assert.NotEqual(testClass2.TestPivateField, testClass2.TestProperty);

Assert.Equal(testClass1.TestValue, testClass2.TestValue);

Assert.Equal(10, testClass1.TestValue.Value);
Assert.Equal(10, testClass2.TestValue.Value);

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.