Click here to Skip to main content
15,910,886 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello friends,
I am using the concept of inversion control with the patten of dependency injection.
In that i am using Constructor Injection.I had done upto this level but now the problem is creating an instance of the class.When i create an instance of the class its asking for the arguments and says "Constructor cannot have 0 arguments".I am using an interface as an argument in the constructor injection.How can i create an instance of the class and if i need any kind of IoC container so please tell me its way of implementing in the project.
Posted
Comments
Sergey Alexandrovich Kryukov 17-Dec-13 4:32am    
Constructor of class can have 0 arguments! What have you tried so far?
—SA
Sumit Bhargav 17-Dec-13 4:44am    
looks like you didn't understand the post carefully.I have written there that i am using "Constructor Injection".
Sergey Alexandrovich Kryukov 17-Dec-13 4:50am    
Thank you for your note.

I inspected your post as carefully as I can, but failed to find a single line of source code.
This is your who needs to read my post careful. I was perfectly aware that you tried something and wrote some code. I only wanted to ask what did you try.

Here is what we would expect from you: SSCCE.
Thank you for understanding.

—SA
Er. Tushar Srivastava 17-Dec-13 10:35am    
You should either post a code snippet or update your question and make your explanation in a clear understandable points (Short and Concise) ... It will be more readable.. Hope that you understand :)

1 solution

So you created a class with public constructor taking your injected object as parameter - if you didn't also provide a default constructor with no arguments (which most times you shouldn't if you are using DI) you can't instanciate your class with out an instance of the injected object type - thats the sense of DI. Are you shure you understand the concept?. If you really need to construct an instance without the injected object you could provide a default constructor and and do DI in this case with a property, but write your class in a way that it can handle null-references on the injected object/Interface.

Example: if you want to inject a logger and provide a way to not inject it (by calling a default constructor) you have to check for null reference on the logger-object every time you want to use it inside the class...

Although SA is correct in general (would help if you show some code) it's obviouse what you do wrong (nothing - you just didn't use DI as you should)

So here is an example what I mean:

C#
namespace DependencyInjection
{
    class Program
    {
        static void Main(string[] args)
        {
            var a = new CompilerWillWriteDefaultConstructor();
            // wont compile - no default constructor
            // var b = new ClassWithOwnConstructorCompilerWontWriteDefaultConstructor();
            var interfaceimplementation = new ImplementsInterface();
            // Inject interface to class per constructor injection
            var b = new ClassWithOwnConstructorCompilerWontWriteDefaultConstructor(interfaceimplementation);
        }
    }

    // some interface you want to inject
    interface  InterfaceToInject { };
    // a concrete class implementing this interface
    class ImplementsInterface : InterfaceToInject { }

    // the class you want to inject the interface dependency to
    class ClassWithOwnConstructorCompilerWontWriteDefaultConstructor
    {
        public ClassWithOwnConstructorCompilerWontWriteDefaultConstructor(InterfaceToInject obj)
        {
        }
    }

    // some other class - compiler will write constructor
    class CompilerWillWriteDefaultConstructor{}
}
 
Share this answer
 
v3
Comments
Sumit Bhargav 17-Dec-13 12:22pm    
I have a class name classA and i injected my interface IClassA in the constructor of class classA.Now whenever i am instantiating classA in classB its asking for the arguments of constructor of classA.how can i handle this?
Or if i am not on the right track can you please post an example here for contructor injection?
johannesnestler 17-Dec-13 16:12pm    
hi sumit bhargav
So your Problem is just to understand constructor logic? your problem isn't realted to DI... I improved the solution please see the example code.
Philippe Mori 17-Dec-13 19:14pm    
Then the constructor of your class B has to call constructor of classA with the proper injected interface.
Sumit Bhargav 18-Dec-13 0:57am    
Thanks Johannesnestler,It really worked.
My next problem is registering the classes and then resolving them using unity container.I know the syntax to register and resolve them i wanna know the pattern of doing it.Can you please help me out?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900