블로그 이미지
* Microsoft MVP - Windows Development 2014 ~ 2020 http://youtube.com/FutureOfDotNet kaki104

카테고리

List All (607)
Visual Studio (7)
Blazor (2)
Windows App(Universa.. (104)
Xamarin Forms (4)
Bot Framework (19)
Azure (9)
Windows 10 (53)
WPF (7)
Facebook News & Tips (158)
Windows 8&8.1 (113)
Windows Phone 8 (42)
Silverlight (37)
HTML5 & MVC4 (16)
Portable Class Library (2)
Uncategorised Tips a.. (3)
Kinect for Windows (2)
ETC (12)
kaki104 Scrap (4)
App News (11)
Total548,370
Today2
Yesterday78


기본 사용방법 설명

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

Posted by MVP kaki104

댓글을 달아 주세요