0

So I recently found out about dependency injection and read a lot about the pros and cons and I was curious if it would be "good" using it in my app as i couldn't find an example of something like I have at the moment.

My app at the moment is 100% based on Ajax requests my typical controller looks like this:

public JsonResult Controller()
{

  getStuff from the model

  return JSON(info from controller)

}

Is DI a good choice for this situation? Or is are the benefits (if any) slim to none?

PS: I don't know if this matter much, but I only use 1 database.

Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110
jiggergargle
  • 131
  • 2
  • 2
  • 6
  • Using DI has got nothing to do with whether is an ajax call or a normal submit. –  Sep 26 '15 at 23:45
  • oh ok, then whats the real advantage of using it? – jiggergargle Sep 26 '15 at 23:56
  • Suggest you need to do some more research to understand what DI is. Refer [this question/answers](http://stackoverflow.com/questions/130794/what-is-dependency-injection) –  Sep 27 '15 at 00:19
  • This is actually a decent question and Stephen Muecke is kind of missing the point telling the OP to go off and read about DI. If I understand correctly, what jiggergargle is getting at is that it's all well and good having the Controller constructor accept a parameter that defines the service/whatever required but HOW does a standard AJAX request work with that as it's not going to know about the concrete .net server-based class that needs to be passed in to the constructor. So, for example, in TimothyClifford's example below, how does the Ajax request pass in the required database? – TheMook Nov 30 '16 at 22:45

1 Answers1

0

If you have a large application with multiple dependencies then DI is a good choice. Even if your app is relatively small and only requires the database connection to be injected, this could be a good starting point for you into DI.

For the code example you provided, assuming you're using controller injection and you require a database connection, you'll end up with something similar to:

public class MyController
{
    IDatabase database;

    public MyController(IDatabase database)
    {
        this.database = database;
    }

    public JSONResult MyControllerMethod()
    {
        var myData = database.GetData();

        return JSON(myData)
    }
}

Then you would need to configure your DI container so it knows which concrete classes to inject for which interfaces - depending on the framework you use, this will differ slightly.

Given you don't have a lot of experience with it, keep in mind there will be a bit of a learning curve while you get your head around it. I'm assuming you're using WebAPI for your AJAX calls in which case this page is an excellent resource to get you started - http://www.asp.net/web-api/overview/advanced/dependency-injection

timothyclifford
  • 6,799
  • 7
  • 57
  • 85
  • Thanks! I am using MVC5 tho, i´ll check that anyways. My Ajax calls come from Jquery. Does this change anything? – jiggergargle Sep 26 '15 at 22:55
  • From a DI perspective, your calls coming from AJAX doesn't make any difference. From your applications point of view, regular and AJAX calls are all HTTP requests so flow through the same pipelines etc. Hope this helps! – timothyclifford Sep 27 '15 at 09:05