0

I've a c#.net project that uses multiple 3rd party dlls. These dlls exposes interface that are used throughout the project. My question is this: These interfaces are passed to the constructor of Controller class like this:

HomeController(IClientData clientdata, IClientRecord clientrecord)
{ }

Here clientdata and clientrecord are initialized to a default value needed to start the view(produces a list of client details). I've to use these initialized data somewhere else in my project but I don't know how to get these default initialized values.

Maxsteel
  • 1,922
  • 4
  • 30
  • 55
  • 4
    Are you sure you don`t have Dependency Injection of some kind ? – nsgocev Aug 08 '14 at 10:19
  • @nsgocev I was going to post the same question :P – Nahuel Ianni Aug 08 '14 at 10:20
  • @nsgocev I don't see any dependency injection, although please tell me how do I make sure? – Maxsteel Aug 08 '14 at 10:24
  • Also, if there indeed is dep. injection, how do I still use the default implemetations? – Maxsteel Aug 08 '14 at 10:24
  • What do you mean by "initialized to a default value"? The default value for an instance of an interface is `null`. – Enigmativity Aug 08 '14 at 10:24
  • @Enigmativity I mean to say that when I debug, I can see that the IClientdata passed to constructor is already initialized and has got values. I need to use them. – Maxsteel Aug 08 '14 at 10:25
  • @Maxsteel Well there are many frameworks which work in a different way. You can see if there is anything in registered in the App_Start. Also there is no default implementation. DI Injects concrete objects which you can use. Also if you want to use them - try creating a class and in the constructor try to do the same thing you do in your controller. – nsgocev Aug 08 '14 at 10:26
  • @nsgocev Thanks but the class where I want to use it is a unit test class and i cannot pass anything in a constructor of unit test case. – Maxsteel Aug 08 '14 at 10:26
  • @Maxsteel - there are separate questions on how to you DI Frameworks in Unit Tests. It will depend on what kind of framework are you using. The most common are Ninject,Castle,Structure map - check if you have references to something like this. – nsgocev Aug 08 '14 at 10:31
  • @nsgocev I see structure map. Should I reframe question for unit test? Suggestions? – Maxsteel Aug 08 '14 at 10:32
  • 1
    @Maxsteel refer to this topic - http://stackoverflow.com/questions/2216643/using-structuremap-with-unit-tests – nsgocev Aug 08 '14 at 10:33

1 Answers1

0

What you have seen is a very general pattern in ASP.NET MVC . Here your home controller has 2 dependencies (one class inherited from IClientData and other from IClientRecord) . These dependencies are usually injected by some DI framework .

There are some good DI frameworks available for ASP.NET MVC

  1. Unity
  2. Autofac
  3. Castle Windsor
  4. StructureMap etc...

and they usually provide two ways to setup the dependencies -

  1. through XML configuration files
  2. Fluent interface (inside code)

When Fluent interface is used, a general practice is to create a separate class file inside App_Start folder and call it from global.asax (Application_Start).

Rabi
  • 2,210
  • 17
  • 18