티스토리 뷰

반응형


기본 사용방법 설명

Constructor Injection – 생성자 주입

- Resolve시에 기본으로 사용하는 방식
- private readonly T _instance; 를 이용해서 내부에서 사용
- Multiple Parameters
  public Driver(ICar car, ICarKey key)
  private readonly ICar _car;
- Multiple Constructors
  [InjectionConstructor] public Driver(ICar car)
  public Driver(string name)
- Primitive Type Parameter
  public Driver(ICar car, string driverName)

 

https://youtu.be/UHrrIDBubdg

 

UnityContainer Detail & Examples - Prism으로 시작하는 UWP app part15 - 2

 

Property Injection

- Attribute를 이용해서 Injection
- public T Instance { get; set;} 을 이용
- Dependency Attribute
  [Dependency] public ICar Car { get; set; }
- Named Mapping
  [Dependency("MyCar")] protected ICar Car { get; set; }
- Run-time Configuration
  public ICar MyCar { get; set; }
  unityContainer.RegisterType<IDriver, UWPDriverRuntime>(new InjectionProperty("MyCar", new BMW()));


Method Injection

- Attribute를 이용해서 Injection
- private readonly T _instance; 를 이용해서 내부에서 사용
- 생성자를 이용할 수 없는 경우 사용
- InjectionMethod Attribute
  [InjectionMethod] public void UseCar(ICar car) { _car = car; }
- Run-time Configuration
  public void MyCar(ICar car) { _car = car; }
  unityContainer.RegisterType<IDriver, UWPDriverRuntimeMethod>(new InjectionMethod("MyCar", new Audi()));


Overrides

- 등록된 Type이외의 Type을 Injection 시키는 방법
- ResolverOverride를 이용해서 등록된 Type을 재정의 할 수 있음
- ParameterOverride
  unityContainer.Resolve(
  new ParameterOverride("car", new BMW()));
  Override Multiple Parameters
  new ParameterOverrides {  { "car1", new Audi() },
                         { "carKey1", new AudiKey() }, 
                         { "car2", new BMW() },                 
                         { "carKey2", new BMWKey() }}
- PropertyOverride
  unityContainer.Resolve(new PropertyOverride("Car", new BMW()));
- DependencyOverride
  unityContainer.Resolve(new DependencyOverride(typeof(ICar), new BMW()));


Lifetime Manager

- UnityContainer에 등록된 객체에 대한 수명을 관리하는 방법을 지정할 수 있음
1. TransientLifetimeManager
   기본 수명 관리 방법, Resolve호출시 새로운 객체를 생성해서 반환
2. ContainerControlledLifetimeManager
   처음 Resolve시 싱글톤 객체를 생성하고, 이후 Resolve할 때마다 반환 
3. HierarchicalLifetimeManager
   2 항목과 동일, 추가로 하위 컨테이너가 자체 단일 객체를 만들어 반환 – 상위 컨테이너와 하위 컨테이너가 서로 다른 객체 관리
4. PerResolveLifetimeManager
   1 항목과 동일, 재귀호출시 생성된 객체는 재사용함
5. PerThreadLifetimeManager
   스래드당 단일 객체를 생성
6. ExternallyControlledLifetimeManager
   생성된 객체에 대한 약한 참조만 유지. 사용자 정의 수명관리자를 만들어서 사용할 수 있음

 

https://youtu.be/fQunFpAvTsg

소스

https://github.com/kaki104/PrismSample2019

반응형

'UWP & Windows App > Beginner' 카테고리의 다른 글

LINQ part2  (0) 2020.01.31
LINQ part1  (0) 2020.01.29
UnityContainer - Prism으로 시작하는 UWP app part 14  (0) 2020.01.06
Cookie 관리, Prism으로 시작하는 UWP app Part13  (0) 2019.06.10
WebView - Async, EventAggregator  (1) 2019.05.29
댓글