Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# dependency injection - how to you inject a dependency without source?

I am trying to get started with some simple dependency injection using C# and i've run up against an issue that I can't seem to come up with an answer for.

I have a class that was written by another department for which I don't have the source in my project. I wanted to inject an object of this type though a constructor using an interface, but of course, i can't change the injected objects implementation to implement the interface to achieve polymorphism when casting the object to the interface type.

Every academic example I have ever seen of this technique has the classes uses classes which are declared in the project itself. How would I go about injecting my dependency without the source being available in the project?

I hope that makes sense, thanks.  

like image 888
Phil Harris Avatar asked Jun 17 '10 07:06

Phil Harris


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

You could create a wrapper around your target type, so for example, you could have a class they provide:

public class TheirClass
{
  public void DoSomething();
}

With which you can define an interface:

public interface ITheirClass
{
  void DoSomething();
}

And implement that interface on a wrapper class:

public class TheirClassWrapper : TheirClass, ITheirClass
{

}

Or, if the class they provided is sealed, you need to do it slightly differently:

public class TheirClassWrapper : ITheirClass
{
  private TheirClass instance = new TheirClass();

  public void DoSomething()
  {
    instance.DoSomething();
  }
}

Then you can inject that interface instead.

I know in MEF we can export concrete types and have them injected correctly, but I'm not sure about other IoC containers.

like image 56
Matthew Abbott Avatar answered Sep 23 '22 03:09

Matthew Abbott


The question is, why do you want to inject it as an interface? Is it because the class implements some properties/methods that you'd like to abstract out so it can be substituted, or are you just trying to "force" a marker interface onto it because "we always depend on interfaces, not concrete classes" ? If it's the latter then you're on the wrong path as you'd likely just cast it to the concrete immediately anyway.

Assuming the former of the two I can see two options:

  1. Use inheritance instead. This may or may not be possible depending on the class has been created, but you could inherit from it and substitute your new class for the old one.
  2. Create the interface yourself, with the methods/properties you require and wrap the external concrete class in a class that implements the interface.

For option 2, if you can't inherit, as an example, assuming you only care about one method in the class (we'll call it Method1()) you create a matching interface for it:

public interface IMyNewInterface
{
    void Method1();
}

Then create an implementation of it that takes the concrete class as a dependency (injected by your container as normal) that just calls the concrete class:

public class MyNewClass : IMyNewInterface
{
    private MyConcreteClass _MyConcreteClass;

    public void MyNewClass(MyConcreteClass concreteClass)
    {
        _MyConcreteClass = concreteClass;
    }

    public void Method1()
    {
        _MyConcreteClass.Method1();
    }
}
like image 28
Steven Robbins Avatar answered Sep 27 '22 03:09

Steven Robbins