none
ASP.NET - Writing Clean Code in ASP.NET Core with Dependency Injection RRS feed

  • General discussion

  • Dependency Injection (DI) is a technique that allows applications to be constructed from loosely coupled modules. ASP.NET Core has built-in support for DI and uses it to provide services to applications built on it. Learn how to use DI to access ASP.NET services as well as your own application services.

    Read this article in the May issue of MSDN Magazine

    Monday, May 2, 2016 9:00 PM
    Owner

All replies

  • Thanks for the new DI article. I was hoping you, or someone could start to get into deeper scenarios. Each example I am seeing is a small permutation on Here is an object and here is how we inject it. I would be interested in seeing those objects rely on other objects and how they all link together. It isn't that I am complaining, just that I am looking for more depth to the scenarios.

    One specific thing I have been looking for is the ability to define a specific concrete implementation to be Injected from a configuration file. Dependency Injection is great, but with what I am seeing everything is still wired up with code. If I want to change from a Mock interface to a Live interface I need to recompile and deploy. This seems to lose much of the advantage of IoC. I should be able to determine how everything gets wired up from outside the code base.

    For example, I may have multiple Classes that implement an IDataSource Interface. Two of these could be RealDataSource and MockDataSource. These may have their own settings in their constructors. There should be a way to specify the specific IDataSource used in the Injection from the configuration process. Without recompiling and redeploying it should be possible to change between the two.

    I have been attempting to use Activator.CreateInstance for this, but with little luck so far. Am I missing something or is this just not possible?

    I am also struggling with trying to hand an Enumeration of objects defined by a config file into the DI process. I would be so happy to see examples of these things being done. If this will be impossible with the standard ASP.NET DI it would be great to get some advice on where to look for this.

    Please keep up the good work, but let's also see the next step.

    Tuesday, May 3, 2016 1:51 AM
  • I am encountering an error when trying to download the code sample for this article. Both are returning an error "This site can't be reached"..... ".../Code_SmithCore0516.zip might be temporarily down or it may have moved permanently to a new we address." Is this a known issue? Both C# and VB are having issues.
    Tuesday, May 3, 2016 1:12 PM
  • Same for me. No file to download.
    Wednesday, May 4, 2016 12:28 PM
  • Hi,.

          Thanks a lot for the article.i have a project initially we use masterdb connection string then after login based on user information we have to change the connection string. I have to pass connection string in dynamic. Thanks for you help advance. 


    A.Venkatesan

    Thursday, May 5, 2016 3:25 PM
  • There has been an issue with the download tool our production folks use to post code samples. We're hoping the issue will be resolved soon. I apologize for the inconvenience. 

    Thursday, May 5, 2016 4:20 PM
  • You can certainly define your dependencies in a file - in fact that's how most of the early IOC containers all did it. It's only been more recently that most developers have preferred wiring things up in code, because it tends to be more robust. For instance, code-based configuration will be updated when automatic refactoring is done, and will break at compile time in many cases where configuration-based changes would only be detected at runtime. But yes, this means you must recompile/redeploy in order to implement changes to your application's dependency graph.

    Most IOC containers have the capability to load types from a variety of locations, and to use conventions or rules rather than hardcoded settings for what type to use. For instance, StructureMap has a nice feature that will automatically match a request for an interface ISomething to an implementation class Something (that implements ISomething), without an explicit rule stating to do this. It can also automatically load all types from a given folder, so you could change out which types your system was using by adding or removing assemblies (plugins, essentially) to a folder, without the need to redeploy your full application. It wouldn't be hard to implement a UI within the application that would let you choose which implementation from those found in the plugins folder to use for a given interface, though I don't have an example of this functionality I can point you at.

    Perhaps of interest to you also is the ability to decouple projects from one another at compile time, while still letting them work together at runtime. I wrote about how to do this here:

    http://blog.falafel.com/use-types-from-project-without-referencing/

    Hope that helps.

    Steve (@ardalis)


    AspAlliance.com

    Thursday, May 5, 2016 4:34 PM
  • Sorry about that. Fixing the downloads now. It should be there shortly.
    Thursday, May 5, 2016 5:05 PM
    Owner
  • I was attempting to adopt the techniques in this article for API controllers and I found that it was necessary to change the unit test in Figure 1 to Assert.IsAssignableFrom(IEnumerable<MyType>) in order for this to work.  Assert.IsType verifies that a specified type is exactly that type and no other.  In my case, it failed on the repository because the Mock creates a generic List which is not exactly IEnumerable<MyType>.

    Might be useful for anyone else trying this.


    • Edited by Framnk2013 Friday, August 26, 2016 3:25 PM
    Friday, August 26, 2016 3:25 PM
  • The sample doesn't execute out of the box. I got 2261 errors such as "The dependency System.Numerics.Vectors >= 4.1.1-beta-23516 could not be resolved."

    It seems that since Microsoft is on the Open Source road, .NET development becomes as hard as using the Java ecosystem (Maven and JDK hells) ! :-(


    • Edited by RudyCo Wednesday, November 2, 2016 2:20 PM
    Wednesday, November 2, 2016 2:19 PM
  • Thanks for the article, I found it very useful. However, I'd like to ask you a question, imagine the following scenario:

    When a Dinner is created, you need to run some business logic, like for example, let's say create collection of Meal (populated with some data by default). Since you create the Dinner object straightforward in the controller, how 'd you handle the creation of the linked collection of Meals?

    Thanks in advance!

    Monday, February 20, 2017 2:29 PM
  • If we have more than 1 implementation of the interrace "IDinnerRepository" then how can we resolve them? Suppose the ChineseDinnerController needs ChineseDinnerRepository that intern implements "IDinnerRepository" and MaxicanDinnerController need MaxicanDinnerRepository that again inter implements "IDinnerRepository" interface. How this can be registered and resolved? I have looked on the web and have found Structure Map allowing us to do this but thats not working for me here is the link http://structuremap.github.io/registration/inline-dependencies/#sec1

    Many thanks!

    Yawar

    Wednesday, April 12, 2017 7:38 PM