Showing posts with label Service Injection. Show all posts
Showing posts with label Service Injection. Show all posts

Thursday, August 13, 2020

Angular Provider Scope

In the previous post, we saw on how to make a service available globally by declaring the @Injectable providedIn: 'root'. We can also declare module-level services using ProvidedIn, in this post we shall see on how to declare module-level service using @Injectable

To limit the scope of a service to a module we need to specify the module in the providedIn instead of ‘root’. In the below code the HellpService is made available only to the HelloModule, the service is not available to any other Modules in the Application.

Angular Service Scope

In Angular, Services are a great way to share information among Components that don't know each other. Based on how we register a service we can control the scope/visibility of the service across the Application.

Services can be registered with 3 levels of scope
1. Global Scope
2. Module Scope
3. Component Scope

Wednesday, May 22, 2019

Injecting Service into Asp.Net Core View

In the previous posts we saw on how register a service Dependency Injection in the startup and inject the service to the MVC controller or Action method. In some cases we might need to inject a service directly into the View. A typical example is populating dropdown lists in the view.

If a view has dropdowns we cannot use the original Entity model since it will not contain the option values for the dropdown lists, in these cases we will create a custom View Model with will contain the properties from the entity model and additional lists to populate the dropdowns in the view. By injecting services to the view directly we can avoid this by injecting services directly to the view to populate the dropdown option values and continue using the entity model to display the model properties.

To inject a service directly to a view we need to use the @Inject directive. The syntax for the @Inject directive is as follows.

Injecting Service into Asp.Net Core Action Methods

In the previous post we saw on how to inject a service to the MVC controller. This might not be needed always, there are cases where we will need the service only for one action method in the controller. In this post we shall see on how to inject a service to one of the action methods in a controller.

The service and the service registration in the startup remains the same, instead of injecting the service in the controller’s constructor. When injecting the service in the action method we need to use the [FromServices] attribute.  We will inject the service in the action method as follows.

Injecting Service into Asp.Net Core Controllers

In the previous example we created an interface IUserService and a service class UserService. We also registered the service in the Asp.Net Core startup. In this post we will see on how to inject this service in the constructor of MVC controller and use it to populate the view. To start with let us create a new MVC controller and name it UserServiceController.

In the constructor of the Controller class we will pass a reference to the IUserService. Since we registered this interface with the service class in the startup, the Asp.Net runtime will inject an instance of the UserService class to the controller. Following is the code for the MVC controller.