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.
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.
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.
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.
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 ...
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.
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:
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With