Singleton Management
Prism์ IContainerRegistry๋ฅผ ํตํด Singleton ์ธ์คํด์ค ๊ฐ์ฒด๋ฅผ ์ง์ ๊ด๋ฆฌํ ์ ์์ต๋๋ค. IContainerRegistry๋ ๊ธฐ๋ณธ์ ์ผ๋ก PrismApplication ํด๋์ค๋ก ๋ถํฐ ๊ฐ์ ๊ตฌํ๋๊ธฐ ๋๋ฌธ์ ์ด๋ ค์ ์์ด ์ฌ์ฉํ ์ ์์ต๋๋ค. (abstract)
override ํ์
์ธ RegisterTypes ๋ฉ์๋๋ฅผ ํตํด IContainerRegister๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
internal class App : PrismApplication
{
...
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// ์ฌ๊ธฐ!!
}
}
ํ๋! ๊ธฐ์กด Application์ ์ ์งํ๋ฉด์ PrismBootstrapper๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ๋ ์๊ฒ ์ฃ . ์ด ๊ฒฝ์ฐ PrismBootstrapper ์์๋ ๋ํ RegisterTypes ๋ฉ์๋๋ฅผ overrideํ ์ ์์ผ๋ฏ๋ก ๋์ผํ๋ค ์๊ฐํ๋ฉด ๋ฉ๋๋ค. ํฌ๊ฒ ๋ค๋ฅผ ๊ฒ์ด ์๊ธฐ์, ์ด ๋ถ๋ถ์ ๋์ค์ ์ข ๋ ๋ค๋ค๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
IContainerRegistry
์ด์ Singleton ์ธ์คํด์ค๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ํด ํด๋น ๊ฐ์ฒด๋ฅผ ๋ฑ๋กํด์ผ ํฉ๋๋ค. ์ด ๋ ์ฌ์ฉ๋๋ ๋ฉ์๋๊ฐ ๋ฐ๋ก RegisterSingleton ์
๋๋ค. ๊ทธ๋ผ RegisterSingleton ๋ฉ์๋๋ฅผ ์ด๋ป๊ฒ ์ฌ์ฉํ ์ ์๋์ง ํ๋ฒ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
๋ฐ๋ก์์ฑํ๊ธฐ
Application ์์ฑ ๊ณผ์ ์์ ๋ฐ๋ก ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ๊ฒฝ์ฐ์
๋๋ค. ์ค์ new() ์์ฑ์ด ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ Singleton์ ๋ฑ๋กํ๋ ๊ณผ์ ์์ ๊ตณ์ด ์์ฑํ ํ์๊ฐ ์๊ฑฐ๋ ํ์ง ๋ง์์ผ ํ๋ ๊ฒฝ์ฐ๋ฅผ ์ ํ๋จํด ๋ณด๋ ๊ฒ์ด ์ข์ต๋๋ค.
containerRegistry.RegisterSingleton(new AvatarView());
์๋ง๋ ๋ณ์๊ฐ ํ์ํ ์์ฑ์ ์ด๊ฑฐ๋ ๋ฌด์ธ๊ฐ ์
ํ
์ด ํ์ํ๊ฑฐ๋, ์ฌ๋ฌ ์์ ์์ ์ด์ ๊ฐ ์์ ์ ์๊ฒ ์ฃ .
๊ทธ๋ฆฌ๊ณ ์ธ์คํด์ค๋ฅผ ๋ฌธ์์ด๋ก ์ ๊ทผํ ๋ ์ํ๋ ์ด๋ฆ์ผ๋ก ์ฐพ์ ์ ์๋๋ก ๋ถ์ฌํ๋ ๊ฒ๋ ๊ฐ๋ฅํฉ๋๋ค. (overriding)
containerRegistry.RegisterSingleton(new BlogView(), "BlogPage");
containerRegistry.RegisterSingleton<AvatarView>(new AvatarView(), "BlogPage");
๋๋ฒ์งธ ์ค์ <T>
ํ์
๋จ์ํ๊ฐ ๊ฐ๋ฅํ์ฃ !
์ฌ์ค ๊ธฐ๋ณธ์ ์ผ๋ก ์ด๋ฆ์ ์์ฑํ์ง ์๋๋ค๋ฉด ๊ฐ์ฒด ์ด๋ฆ๊ณผ ๋์ผํ AvatarView ๋ฌธ์์ด์ด ๋ถ์ฌ๋์ง๋ง, ์ด๋ ๊ฒ ์ธ์คํด์ค๋ช
๊ณผ ๋ค๋ฅด๊ฒ ๋ฑ๋กํ ์๋ ์๊ฒ ์ต๋๋ค. ์๋ง๋ ๋ณดํต ์ ๋ ๋จ์ด๋ฅผ ๋ถ์ด๊ฑฐ๋ ํ ๋ ์ฌ์ฉ๋๊ฒ ์ฃ .
ํ์
๋ฑ๋กํ๊ธฐ
๋ฐ๋ก ์์ฑํ๋ ๋ฐฉ๋ฒ๊ณผ๋ ๋ฌ๋ฆฌ Singleton ์ธ์คํด์ค๋ฅผ ๋ฑ๋กํ๋ ๊ณผ์ ์์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ํ์๊ฐ ์๊ฑฐ๋ ViewModel ์์กด์ฑ ์ฃผ์
, Resolve, Region ๋ฑ์์ ํธ์ถ์ด ๋ ๋ ์์ฑ๋์ด์ผ ํ ๊ฒฝ์ฐ์๋ ๋ฐ๋ก ์ด ๋ฐฉ๋ฒ์ ํด๋นํ๊ฒ ๋ฉ๋๋ค. ๋ฌผ๋ก ์์ ์ ๋ ๋ค์ํ ๊ฒฝ์ฐ๊ฐ ์๊ฒ ์ต๋๋ค.
container.Registry.RegisterSingleton(typeof(BlogView));
์์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ๊ฒ๊ณผ๋ ๋ฌ๋ฆฌ <T>
ํ์
์ ๋๊ฒจ์ค์ผ๋ก์จ ๋จผ ํ๋ โฆ BlogView๋ฅผ ์ฐพ๋ ์ํฉ์ด ์๊ธธ ๋ Singleton ๊ด๋ฆฌ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๊ฒ์
๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ด๋ฒ์๋ ๋ง์ฐฌ๊ฐ์ง๋ก name์ ์ํ๋ ๋ฌธ์์ด๋ก ๋ถ์ฌํ ์ ์์ต๋๋ค.
container.Registry.RegisterSingleton(typeof(BlogView), "BlogViewPage");
์ด๋ ๊ฒ ํ๋ฉด Type์ ํตํด Singleton ๊ฐ์ฒด๋ฅผ ๋ฑ๋ก๋ง ํด์ ์ด๋์ ๊ฐ ์ฌ์ฉ๋๊ธฐ๋ฅผ ๊ธฐ๋ค๋ฆฌ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
Singleton ์ฌ์ฉ
๊ทธ๋ผ ์ด์ ViewModel์ด๋ Resolve<T>
๋ฅผ ํตํด ๋ฑ๋ก๋ Singleton ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์๋ณผ๊น์?
ViewModel์์ IoC Container ์ฃผ์
์ ํตํ ์ฌ์ฉ๋ฐฉ๋ฒ์
๋๋ค.
public class JamesViewModel(BlogView blogView)
{
}
Resolve๋ฅผ ํตํ ์ฌ์ฉ ๋ฐฉ๋ฒ๋ ํฌ๊ฒ ๋ค๋ฅด์ง ์์ต๋๋ค.
BlogView blogView = _container.Resolve<BlogView>("BlogViewPage");
BlogView galleryView = _container.Resolve<GalleryView>();
BlogView aboutView = _container.Resolve<AboutView>();
Region์ ์ถ๊ฐํ๊ฑฐ๋ ๋ชฉ์ ์ ๋ง๊ฒ ๋ค์ํ๊ฒ ํ์ฉํ๊ฒ ์ต๋๋ค. (Singleton ๊ด๋ฆฌ๊ฐ ํ์ํ View์ธ ๊ฒฝ์ฐ ์ฌ๋ฏธ์๊ฒ ์์ฉํ ์ ์์ฃ .)
Interface ํ์ฉ
์ฌ์ค Singleton์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ๋ฑ๋ก์ ์ฌ์ฉ๋ ๊ฐ์ฒด ํ์
์ ์๊ณ ์์ด์ผ๋ง ํ๋ ๊ตฌ์กฐ์ ์ธ ๋ถํธํจ์ด ์๊น๋๋ค. ํ๋ก์ ํธ ๊ตฌ์กฐ์, ๋ณ ์๊ด์๋ ๊ฒฝ์ฐ๋ ์๊ฒ ์ง๋ง ๋ณต์กํ๊ฒ ์ฌ๋ฌ ํ๋ก์ ํธ๋ฅผ ์ฐธ์กฐํด์ผ ํ๋ ๊ฒฝ์ฐ์๋ ์ด ํ์ฉ์ด ํ์ํฉ๋๋ค.
๊ธฐ์กด Singleton ๋ฐฉ์๊ณผ ๊ฑฐ์ ๋์ผ ํ๋ฉด์ ๋ํ ์ธํฐํ์ด์ค(๋๋ ํด๋์ค)๋ฅผ ์ ์ํ ์ ์์ต๋๋ค.
container.Registry.RegisterSingleton<IViewable, BlogView>("Blog");
container.Registry.RegisterSingleton<IViewable, GalleryView>("Gallery");
container.Registry.RegisterSingleton<IViewable, AboutView>("About");
์ด๋ ๊ฒ ์ธํฐํ์ด์ค ๋๋ ์์ ์์ํด๋์ค๋ฅผ ๋ํ ํ์
์ผ๋ก ์ ์ํ๋ค๋ฉด ์๋์ฒ๋ผ ์ด๋ ํ ์ฐธ์กฐ ์์ด๋ ๋ํ ํ์
์ ํตํด ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
_container.Resolve<IViewable>("Blog");
_container.Resolve<IViewable>("Gallery");
_container.Resolve<IViewable>("About");
์ด๋ฅผ ํตํด ํ๋ก์ ํธ๊ฐ ์ฐธ์กฐ ๊ด๊ณ์ ์๊ด ์์ด๋ Singleton์ ํธํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ฝ์ด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค.