This commit is contained in:
christoerasmus 2022-06-23 11:33:04 +02:00 коммит произвёл GitHub
Родитель 2f16da5796
Коммит 73e800ab88
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 13 добавлений и 13 удалений

Просмотреть файл

@ -4,13 +4,13 @@ uid: Article.Unity.Using
# Using Unity in Applications
This topic describes how to develop applications using Unity, and how to create and build instances of objects. It assumes that you familiar with dependency injection and separation of concerns concepts.
This topic describes how to develop applications using Unity and how to create and build instances of objects. It assumes that you are familiar with dependency injection and separation of concern concepts.
## The Container
Unity exposes very compact API to operate the container. The most operations related to registration, resolution, and lifetime management is exposed through one interface - **[IUnityContainer](xref:Unity.IUnityContainer)**.
Unity exposes a very compact API to operate on the container. Most operations related to registration, resolution and lifetime management is exposed through one interface - **[IUnityContainer](xref:Unity.IUnityContainer)**.
To start using Unity you need to create an instance of the container and get reference to [IUnityContainer](xref:Unity.IUnityContainer) interface:
To start using Unity you need to create an instance of the container and get a reference to the [IUnityContainer](xref:Unity.IUnityContainer) interface:
```cs
IUnityContainer container = new UnityContainer();
@ -18,7 +18,7 @@ IUnityContainer container = new UnityContainer();
## Creating instances
Once container is created you could start using it immediately:
Once the container is created you can use it immediately:
```cs
IUnityContainer container = new UnityContainer();
@ -28,7 +28,7 @@ var value = container.Resolve<object>();
value = new object();
```
It will create any type with accessible constructor. Consider following example:
It will create any type with an accessible constructor. Consider following example:
```cs
// Simple class Foo
@ -50,9 +50,9 @@ var value = container.Resolve<Foo>();
// value created with constructor 'Foo(object obj)'
```
`Foo` is a simple class with three public constructors. When `Resolve<Foo>()` is called, Unity will evaluate available constructors and select one with longest list of parameters it can satisfy with dependencies. It will create all required dependencies and pass them to selected constructor during initialization.
`Foo` is a simple class with three public constructors. When `Resolve<Foo>()` is called, Unity will evaluate the available constructors and select one with the longest list of parameters it can satisfy with dependencies. It will create all required dependencies and pass them to the selected constructor during initialization.
In this particular case Unity will select second constructor with parameter `obj` of type [Object](xref:System.Object). Although constructor `Foo(string id, object obj)` is longer, it has parameter of type [String](xref:System.String) which is a primitive type. Unity can not create primitive types by itself. If you want to make these available for dependency injection you would need to register them with the container. For Unity to select third constructor `Foo(string id, object obj)` you need to register string instance with container:
In this particular case Unity will select the second constructor with parameter `obj` of type [Object](xref:System.Object). Although constructor `Foo(string id, object obj)` is longer, it has a parameter of type [String](xref:System.String) which is a primitive type. Unity cannot create primitive types by itself. If you want to make these available for dependency injection you would need to register them with the container. For Unity to select the third constructor `Foo(string id, object obj)` you need to register a string instance with container:
```cs
// Register string instance
@ -95,9 +95,9 @@ public class Foo
}
```
In this example we have `IService` interface defining an API and class `Component` implementing that API. Type `Foo` is a consumer of the service and should be injected by container with an instance of the service during initialization.
In this example we have `IService` interface defining an API and class `Component` implementing that API. Type `Foo` is a consumer of the service and should be injected by the container with an instance of the service during initialization.
If you just call `container.Resolve<IService>()` it will throw an exception complaining that it can not create an interface of type `IService`. You need to register a [Type Mapping](xref:Tutorial.Mapping) to instructs Unity how to create service of type `IService`:
If you just call `container.Resolve<IService>()` it will throw an exception complaining that it cannot create an interface of type `IService`. You need to register a [Type Mapping](xref:Tutorial.Mapping) to instruct Unity how to create a service of type `IService`:
```cs
// Register mapping between IService and Component
@ -109,13 +109,13 @@ var value = container.Resolve<Foo>();
// value created with constructor 'Foo(IService service)'
```
During the resolution, when Unity will try to satisfy dependencies, it will look for a registration for each dependency and will find this mapping. It will create `Component` and passes it to constructor of `Foo` as `IService`.
During resolution, Unity will try to satisfy dependencies, it will look for a registration for each dependency and find this mapping. It will create `Component` and pass it to the constructor of `Foo` as `IService`.
For more information see [Type Mapping](xref:Tutorial.Mapping)
## Lifetime
By default Unity creates a new instance every time type is requested. Instances it created are not tracked or managed by the container.
By default Unity creates a new instance every time a type is requested. Instances it create are not tracked or managed by the container.
```cs
// Register mapping between IService and Component
@ -128,7 +128,7 @@ var value2 = container.Resolve<IService>();
// value1 and value2 are not the same
```
To enable lifetime management, type needs to be registered with one of the compatible [lifetime managers](xref:Unity.Lifetime). Depending on registration type Unity provides three helpers:
To enable lifetime management, a type needs to be registered with one of the compatible [lifetime managers](xref:Unity.Lifetime). Depending on registration type Unity provides three helpers:
* [TypeLifetime](xref:Unity.TypeLifetime)
* [InstanceLifetime](xref:Unity.InstanceLifetime)
@ -147,4 +147,4 @@ var value2 = container.Resolve<IService>();
// value1 and value2 are the same instance of Component
```
For more information see [Lifetime Management](xref:Tutorial.Lifetime)
For more information see [Lifetime Management](xref:Tutorial.Lifetime)