1

When is the constructor of controller class is called and how? I'm asking this because in an application that I am maintaining, constructor of controller is passed with Interfaces of dlls and they appear to be automatically initialized by some deafult method in Dll. Contoller looks like this:

private _clientdetails
public CleintController(IClientDetails clientdetails)
{
    _clientdetails = clientdetails
}
//here various members of clientdetails used via _clientdetails

This only appears to work when IClientdetails clientdetails is passed as a param to constructor otherwise I get error: Type passed as var. If I can see/know how constructor of controller is called, I can know how to pass this initialized interface to my other methods.

Maxsteel
  • 1,922
  • 4
  • 30
  • 55
  • How about `new ClientController(yourParamHere)`? – Andrei V Aug 06 '14 at 10:29
  • @AndreiV didn't get your question? – Maxsteel Aug 06 '14 at 10:30
  • Did you try instantiating a controller, i.e. just create a `new` object? – Andrei V Aug 06 '14 at 10:31
  • @AndreiV Yes I did. And passing `IClientDetails clientdetails` gives error as I mentioned in question. – Maxsteel Aug 06 '14 at 10:32
  • You just need to pass an object which implements `IClientDetails`: `new ClientController(clientdetails)`. – Andrei V Aug 06 '14 at 10:34
  • @AndreiV Sorry but how? If I declare `IClientDetails clientdetails` and pass it to any function, it will be null and not initialized like it should be. – Maxsteel Aug 06 '14 at 10:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58767/discussion-between-maxsteel-and-andrei-v). – Maxsteel Aug 06 '14 at 10:39
  • It's very likely that your code is being instantiated by Dependency Injection at some point, potentially by some DI tech like Ninject, StructureMap or any of the other many possibilities. Otherwise you need to pass in an object that conforms to the IClientDetail interface. – Code Uniquely Aug 06 '14 at 10:39
  • @CodeUniquely Okay, so if I have to use the same initialized IClientDetail object, what do I do? – Maxsteel Aug 06 '14 at 10:40
  • The constructor is called every time one if its methods is called - i.e. get or post. It looks like this app is using dependency injection (ninject or similar) to inject a concrete implementation of `IClientDetails` –  Aug 06 '14 at 10:40
  • I presume that `IClientDetails` is an interface. You need to have a class which implements this interface, create an object of that class and then pass that object to the controller. For example: `public class MyClientDetails : IClientDetails { /*add client details*/ }`. Now for the object: `MyClientDetails myClientDetails = new MyClientDetails (); /*set the details you need*/ new ClientController(myClientDetails );`. – Andrei V Aug 06 '14 at 10:40

3 Answers3

1

If I understood your question, You may pass an instanciation of a class detail which implements IClientDetails

public class detail : IClientDetails 
{
//detail class implementation
//interface method implementation
}

Usage

CleintController(new detail());

or

detail d = new detail(){ proper1 = value,....};
CleintController(d);

Editing

if you want to use a default implementation of the interface just add a default constructor in the controller , like this :

public CleintController()
{
    _clientdetails = new DefaultClass();
}

with DefaultClass is the default class that implements IClientDetails

Editing 2 *

To get all class that implements the interface as mentionned here : Getting all types that implement an interface

You can do this :

var type = typeof(IClientDetails);
var types = AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(s => s.GetTypes())
    .Where(p => type.IsAssignableFrom(p));

then you can easily identify which class (that implements IClientDetails) you need .

Community
  • 1
  • 1
Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
  • I understand that this interface needs to be implemented somewhere. What I'm saying is, this interface is implemented in a class in the dll that I'm using and the default values are initialized already. How do I use this default in my application. – Maxsteel Aug 06 '14 at 10:45
  • 1
    Thanks but what if I don't know what class implements it. I am using a dll imported in my project. How do I know which class implements it. I can only see the interfaces and not actual implementation. – Maxsteel Aug 06 '14 at 10:52
1

what you are asking is called dependency injection.

a simple example is here

basically you say your application where ever you saw this interface implement this class also every one of them has lots of options.

Just check your application start method and you will see some injections.

Onur Topal
  • 3,042
  • 1
  • 24
  • 41
0

Bit late but for whoever who needs it: ASP.NET MVC allows you to specify which objects to automatically supply to the caller. Take a look at Startup.cs in your main root and you should see some code that looks like this:

public void ConfigureServices(IServiceCollection services)
{
    // Add application services.
    services.AddTransient<IDateTime, SystemDateTime>();
}

This bit of code will supply the caller of IDateTime with a new instance of SystemDateTime.

More info here: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/dependency-injection?view=aspnetcore-2.2