📜 ⬆️ ⬇️

Preparing ASP.NET5, release number 3 - introducing dependencies in a new way

We continue our column on the topic ASP.NET5 with a publication from Viktor Kotsyuban ( Gbdrm ) - Technical Leader from SoftServe. In this article, Victor will share with you the details of the new built-in dependency injection functionality in ASP.NET5. Previous articles from the column can always be read on the link #aspnetcolumn - Vladimir Yunev
Dependency injection is one of the most popular and used forms of control inversion, an important principle of OOP, which allows reducing coupling (coupling) - interdependence, interconnection of modules.

In many modern frameworks, dependency injection is already a part of the framework itself. This is a big plus, as developers have a built-in ability to create components, resolve dependencies and provide them to other components upon request, and this happens inside the software platform itself. In addition, dependency injection is one of the basic principles of working with the platform, ignorance of which can lead to inefficient use of the framework for writing govnokod .

ASP.NET 5 has built-in dependency injection capability. What it allows and what stands out among other such approaches will be discussed below. And also try to use it in practice.

ASP.NET 5 is in beta version and everything described here may change before the final release. For example, not so long ago there were serious changes in the possibilities of introducing into properties. But the basic concept and main directions of this innovation are already clear.
')

Basics and Registration Services


Most of the dependencies that are needed for views, controllers, etc. implemented as services, because service is the minimum unit of which the DI container manages. The key features of the minimalist ASP.NET 5 container are rendered into the IServiceProvider interface. The only method for this interface is object GetService (Type serviceType).

There are 4 types of service initialization (4 types of scope) that are supported by the container:
  1. Instance - returns a specific object for the creation of which you are responsible.
  2. Transient - every time a new object is returned.
  3. Singleton - always returns the same object.
  4. Scoped is equivalent to siglton, but in a given area (for example, in the query area).

Services are registered at the start of the application, in the Startup class, the ConfigureServices method (IServiceCollection services), and the ServiceCollection also has methods for adding a service with the corresponding scope (Fig. 1).


Fig.1. - Class diagram

An example of using the extension-methods of the ServiceCollection collection:

public virtual void ConfigureServices(IServiceCollection services) { var settings = new Settings(); services.AddInstance(settings); services.AddScoped<IProductService, ProductService>(); services.AddSingleton<TestService>(); } 

After registration, services are available in all classes that were called through DI, as well as those available in the views.

We introduce services to the controller


With the introduction of controllers, everything is very simple - it happens through the controller constructor.

 public class HomeController : Controller { private readonly IApplicationEnvironment _appEnvironment; private readonly IProductService _productService; public HomeController(IProductService productService, IApplicationEnvironment appEnvironment) { _appEnvironment = appEnvironment; _productService = productService; } // ... } 

And nothing else needs to be done. Registered services are available as constructor parameters.
If we use EntityFramework in our application, then one of the first dependencies that we will think about to use DI is DbContext. This will allow us to write less tied to DbContext code, it is easier to test such functionality, etc.

Example:
github.com/gbdrm/aspnet5/blob/master/src/aspnet5/Controllers/HomeController.cs#L12-L17

Introduction to the submission


There is also an interesting possibility of using registered services in views (* .cshtml).

 @model SomeViewModel @inject SomeContext SomeContext <h1>@SomeContext.PageTitle</h1> <!-- HTML --> 

The @inject directive tells the view engine that we want to use the SomeContext service. And in the view itself you can already use this service, in this case the PageTitle property.

In addition to using services in views, another interesting innovation is typed settings. In addition to registering services, it is possible to “register” a part of the configuration, before that bringing this configuration to a specific type and this can be done in just a few lines of code.
An example of implementing a typed configuration in a view:
github.com/gbdrm/aspnet5/commit/88dc1e708f89edfd30a55ae90265cde9074ae312

Implementing properties and where did [Activate] go


Previously, using the [Activate] attribute, you could pull out the corresponding service for the property. But the developers decided to abandon it. The main reasons for this were problems with compatibility with other libraries for dependency injection, as well as debugging complexity. For some time there were discussions about this, many details can be found here: github.com/aspnet/Mvc/issues/2151 , github.com/aspnet/Announcements/issues/28

But still, the ability to inject dependencies through properties has remained - using the [FromServices] attribute.

Let's create a test class, add it to the container and try to inject it into the controller property.
github.com/gbdrm/aspnet5/commit/3d09ef0c5b6d079884337a6e044262659a4f6250

Conclusion


Introducing dependencies at the framework level is an excellent mechanism for unifying work with dependencies. The implementation in ASP.NET 5 is transparent and straightforward, and the configuration is very easy. Despite the fact that while the implementation is in beta, most of the functionality is already stable and ready for full development. All container sources are available on GitHub - github.com/aspnet/DependencyInjection . Work is still going on, you can get acquainted not only with the functionality, but also to see how to create part of the platform. If you are interested in learning more about dependency injection in asp.net 5, there is a great article (in English): www.emadashi.com/2015/06/dependency-injection-in-asp-net-5-one-step- deeper .

aspnetcolumngithub Tip! And also, if you are interested in the ASP.NET5 topic, you can see examples, try to add something yourself or write a request to implement some functionality in the project with examples: github.com/gbdrm/aspnet5

Latest news


As you already know, Visual Studio 2015 is released with ASP.NET5 Beta5. Details about what exactly is included in the release of Visual Studio can be found in this blog .

Released ASP.NET5 Beta6 update with many changes, improvements, and bug fixes. Details of the update can be found in this blog . You can download the update from this link .

Plans have been published for the release of platform releases in the coming months before the release of the final version of ASP.NET5. According to them, we are waiting for versions Beta7 and Beta8, after which in November we will receive the first version, ready for production (RC1), the final version will be released in the first quarter of 2016. Details of each version can be found on the link .

Published reports of the DevCon 2015 conference, including on web development and the topic of ASP.NET .

useful links


The latest ASP.NET5 documentation is located at http://docs.asp.net/en/latest/ .

We invite you to connect to the live broadcasts of the periodic show ASP.NET 5 Community Standup , where developers from the Microsoft team share the latest news about the platform. Entries are available at this link .

Check out the new article on the development of ASP.NET applications in Visual Studio Code by Eric Reitan, which details the interesting aspects of working with web projects in VS Code.

Learn the basics of ASP.NET5 with the new free course of the Microsoft Virtual Academy.

To authors


Friends, if you are interested in supporting the column with your own material, please write to me at vyunev@microsoft.com to discuss all the details. We are looking for authors who can interestingly tell about ASP.NET and other topics.

about the author


Kotsyuban Victor
Technical Leader in SoftServe
Gbdrm

.NET Developer with over 8 years of experience. Specialist in the field of Enterprise web projects. The last 4 years he has held the position of technical leader.

Source: https://habr.com/ru/post/264467/


All Articles