Giter Club home page Giter Club logo

propertyresolver's Introduction

PropertyResolver

Sometimes Dependency Injection just isn't good enough.

Let's explore some current methods for Dependency Injection (by hand):

class MyClass
{
  IDependency _obj;

  public MyClass(IDependency object)
  {
    _obj = object
  }
}

MyClass requires some IDependency object. By using an interface or abstract type and passing the concrete class in to the constructor we avoid having to reference any particular instance of IDependency in MyClass. We effectively give control of the dependency to the calling object (inversion of control).

class MyClass
{
  public IDependency Obj { get; set; }
}

In this instance we can set the IDependency from outside of the class using the setter of the dependent property (setter injection). This method of dependency injection is a bit counter intuitive since it is not clear that the dependency needs to be set before the functionality is available. However, this method may be necessary if you need to have a parameterless constructor (i.e. custom XAML controls).

class MyClass
{
  public void SomeMethod(IDependency obj)
  {
    // Do something with obj
  }
}

Method injection introduces the dependency at the method level and only passes it when it is used. This method is more cumbersome; however, it is also more robust and allows the dependency to be changed between calls, multiple dependency instances, or dependencies to be shared across multiple instances of MyClass.

The Problem

Dependency injection falls short when you want to access properties and methods of similar types that do not derive from the same base class.

interface IDependency
{
  object Property { get; set; }
}

class DependencyWrapper : IDependency
{
  readonly Dependency _dependency;
  
  public object Property { get { return _dependency.getProperty(); } set { _dependency.setProperty(value); } }
  
  public DependencyWrapper(Dependency dependency)
  {
    _dependency = dependency;
  }
}

Using a wrapper class we can implement the properties necessary to conform our target class (Dependency) to the interface that our class (MyClass) is expecting (IDependency).

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.