This means that configuration information can be stored in any. NET configuration file, which is typically your App. The following example is a simple XML configuration file. A Unity configuration section consists of a set of type lookup modifiers aliases, namespaces, and assemblies , and one or more elements.
A element has a set of elements that provide the configuration for the container's types. Other elements can also be used in the container, see The Unity Configuration Schema for a full description of the schema. The XML namespace specified with the xmlns attribute in the example is not required at run time. Adding a Unity configuration section to a configuration file neither create an actual container nor configures it.
You must create a Unity container instance, read the configuration file, and load the configuration file information into the container. Note: Alternatively, after you create the container, you can configure it programmatically at run time with registrations, type mappings, and any extensions. For more information about run-time configuration, see Run-Time Configuration. To load the configuration file information into a container, use the LoadConfiguration extension method on IUnityContainer.
This interprets the default configuration file for your application, seeks the Unity configuration section, and configures the container. The LoadConfiguration extension method has several overloads. Unity offers a convention-based approach to configuring your container that can be applied in most cases. To take advantage of this you must use the default Unity configuration section name of unity and specify an un-named container in your App. The following example uses this approach.
The configuration for a named container can be loaded from the default configuration section by providing the name of the container to the LoadConfiguration method as shown in the following example:. If your configuration is in a different section either with a different name or from a different file entirely , you must first load the section object through ConfigurationManager and then pass the section to the LoadConfiguration method, as shown in the following example:.
You can also load multiple configurations into the same container. Now, we need to resolve the class we need to use. Here, we are resolving our CourierBusinessClass.
We do not need to create a New Instance of the class nor we need to pass any parameter in the constructor while declaration, Unity Framework does it for us automatically. Now based on our requirement, whether we need to use Usps or Fedex repository , we need to pass string while resolving.
Unity Framework will try to match the string with the name in the Config file and if the match is found, it will create the instance of that type. For example, here we are passing UspsCourier on the first line.
Here we go, it got a match. Unity will realize that it uses a parameterized constructor with the type of ICourierRepository.
But we have dependency name declared which will help Unity to point out to appropriate type. Here dependency name is UspsCourierRepository. Finally, when GetLatestPrices method is executed, it will point out to method in UspsCourierRepository and return results accordingly.
As I previously explained the business problem, we needed multiple repositories which can be switched as and when needed. That can be done by just changing the name of the file in Web. So I created a new Unity Config file and mapped all the types to old repositories. Below is the screenshot of config file with old repositories. I was really amazed when I understood the concept of Dependency Injection using Unity and how it can simplify the code.
The best part is that the documentation for Unity is so simple that it helped me understand it and come up with this concept. Even we are able to achieve Inversion of Control IoC using Unity making sure that Business layer does not have to do anything with 'Newing' up the repository instances. Copy Code. Abstractions assembly and override its Initialize method as follows to register DataClass type with IData. Extension;namespace to this class to access UnityContainerExtension class. The complete class is as follows.
Extension; using Unity. We created a three-layered basic architecture with presentation, business, and data access layer. We introduced Unity to resolve dependency between presentation and business layer. Thereafter we used constructor injection to inject dependencies via interfaces. We resolved dependency of dependency using Unity Extensions so that our architecture was not breached. We achieved an inversion of control via dependency injection. Every class has a single responsibility and is open to extension and not modification.
We achieved Abstraction with the help of interfaces. Conclusion In this tutorial, we tried to learn dependency injection with the help of a simple example. The example is very basic, but the concept could be applied in MVC, Web API, or any enterprise-level application to resolve dependencies and achieve inversion of control with dependency injection.
We also resolved the dependency of dependencies with unity extensions. There could be more methods to resolve dependencies like property injection or a service locator.
We used constructor injection in our application. One can make use of other containers instead of Unity. I used Unity as I am more comfortable with it. I hope you enjoyed the article. View All. Akhil Mittal Updated date Jun 25, Inversion of control IOC and Dependency Injection DI work hand in hand and make our application more loosely coupled and easy to expand. Time to introduce Unity in the application. The output and logic remain the same, we just moved a few pieces from Program.
So, our Business class looks something like the following,. It is pretty straightforward. This is also easy to understand.
This DependencyInjector class is a generic class that takes care of resolving types and registering types. This class just calls the generic Register method of DependencyInjector class and requests to resolve dependency of BusinessClass, so that when needed, the instance of Business class would be supplied.
Simple, crisp, and clear. Yes, you are right. Unity provides this flexibility to resolve dependency of dependencies without structure violation via UnityContainerExtensions as shown below:. This class is part of Unity.
Now, inherit the class from UnityContainerExtension class present in Unity. Do not forget to add using Unity. The job is done; just run the application and we will see our desired output as shown below:. In this way, we resolved dependency of dependency; that is dependency DataClass of dependency BusinessClass to the presentation layer.
In this tutorial, we tried to learn dependency injection with the help of a simple example. Next Recommended Reading. Net Core 6. Create A. Understanding Thread Starvation in.
NET Core Applications. Exploring Numeric Streams In Java. What Is The Metaverse. Microservice Circuit Breaker Pattern.
0コメント