Firstly, I would set up the controller so that it does not have to create the object on which it relies to do its work (see Inversion of Control).
Therefore, the controller may look like this:
public class MyController: Controller
{
private readonly IMyService myService;
public MyController(IMyService myService)
{
this.myService = myService;
}
...
}
Notice that the service is passed into the controller via the controllers constructor and that the controller replies on the service interface and not the actual service type. The controller does not need to care about where the service has come from or how it was implemented, just that it conforms to the specified interface.
So now, you can use the service to obtain your information:
public class MyController: Controller
{
private readonly IMyService myService;
public MyController(IMyService myService)
{
this.myService = myService;
}
public ViewResult Index()
{
var instruments = this.myService.GetInstruments();
return View(instruments);
}
}
Notice here that I have changed your service implementation to retrieve all the instruments as you specified in your description that you would like to retrieve all the instruments.
Also, notice that I have just passed the returned type from the service directly to the view for simplicity. However, I would usually recommend using a view model but I will leave this as an exercise to the reader.
As for the service itself, you might have:
public class MyService : IMyService
{
private readonly IDataProvider dataProvider;
public MyService(IDataProvider dataProvider)
{
this.dataProvider = dataProvider;
}
public IEnumerable<Instrument> GetInstruments()
{
return dataProvider.GetInstruments();
}
}
I wasn't sure what the IServiceProvider
was so I have renamed this to IDataProvider
. You can you whatever you need here to obtain the data in the service. For example, you could instead pass in a IDbContext
(if using entity framework) into the constructor instead and use that to obtain the data.
I must also point out that if your application is very simple with very simple business use case scenarios then you may not even need this service layer here and pass in the data provider directly into the constructor. Again, I will leave this as an exercise to the reader.
Finally, to pass the implementation of IMyService
into the contructor and to pass the implementation of IDataProvider
into MyService
you could use a dependency injection framework.
When MVC instantiates a controller it will look to the dependency injection framework to find out how to create the required dependencies. In this example, when a call is made to the Index()
action, MVC will attempt to create an instance of MyService
. It will have the dependency injection framework create an instance that implements IMyService
. The dependency injection framework will look to its setting and determine that it needs to create an instance of MyService
but this implementation itself requires an instance of IDataProvider
. So the dependency injection framework will again look to its settings and determine that it needs to create an instance of DataProvider
. It will pass the instance of DataProvider
to MyService
and will then pass the instance of MyService
to MyController
, MVC will then eventually call the Index()
action method and the action method will then have access to the service.
One example of a common dependency injection framework is Ninject. You may set up rules to tell the framework that for a given interface, this is the instance type that is required. For example:
this.Bind<IMyService>().To<MyService>();
this.Bind<IDataProvider>().To<DataProvider();
And finally, (assuming you are using razor views) the declaration at the top of the view may look something like:
@model IEnumerable<Instrument>
// You are now able to use @Model. in the view to provide you the information you require.
I hope this helps, but this is only a very brief introduction for your situation. I would therefore recommend reading around inversion of control, dependency injection and Ninject as a potential dependency injection framework although there are various others.