Dreamine.MVVM.Core 1.0.13
중요: 자동 등록된 타입은 기본적으로 Singleton으로 등록됩니다. 따라서 자동 발견된 ViewModel, Model, Event, Manager는 애플리케이션이 별도 수명으로 명시 등록하지 않는 한 반복 해석 시 동일 인스턴스를 유지합니다.
로딩중...
검색중...
일치하는것 없음
DMContainer.cs
이 파일의 문서화 페이지로 가기
1using System;
2using System.Reflection;
4using Dreamine.MVVM.Interfaces.DependencyInjection;
5
6namespace Dreamine.MVVM.Core
7{
16 public static partial class DMContainer
17 {
26 private static readonly object SyncRoot = new();
35 private static IServiceContainer Container = CreateDefaultContainer();
36
37 // Self-registers IServiceResolver so that ViewManager(IServiceResolver) and other
38 // constructor-injected types can be resolved by DreamineContainer without an
39 // explicit registration call from application startup code.
57 {
58 var c = new DreamineContainer();
59 c.RegisterSingleton<IServiceResolver>(c);
60 return c;
61 }
62
79 internal static IServiceRegistry GetRegistry()
80 {
81 lock (SyncRoot) { return Container; }
82 }
83
100 public static IServiceResolver GetResolver()
101 {
102 lock (SyncRoot) { return Container; }
103 }
104
105
122 public static void SetContainer(IServiceContainer container)
123 {
124 ArgumentNullException.ThrowIfNull(container);
125
126 lock (SyncRoot)
127 {
128 Container = container;
129 }
130 }
131
140 public static void Reset()
141 {
142 IServiceContainer previous;
143
144 lock (SyncRoot)
145 {
146 previous = Container;
148 }
149
150 (previous as IDisposable)?.Dispose();
151 }
152
169 public static void Register<TImplementation>()
170 where TImplementation : class
171 {
172 lock (SyncRoot)
173 {
174 Container.Register<TImplementation>();
175 }
176 }
177
203 where TService : class
204 where TImplementation : class, TService
205 {
206 lock (SyncRoot)
207 {
208 Container.Register<TService, TImplementation>();
209 }
210 }
211
236 public static void Register<TService>(Func<TService> factory)
237 where TService : class
238 {
239 lock (SyncRoot)
240 {
241 Container.Register(factory);
242 }
243 }
244
269 public static void RegisterSingleton<TService>(TService instance)
270 where TService : class
271 {
272 lock (SyncRoot)
273 {
274 Container.RegisterSingleton(instance);
275 }
276 }
277
295 where TImplementation : class
296 {
297 lock (SyncRoot)
298 {
299 Container.RegisterSingleton<TImplementation>();
300 }
301 }
302
328 where TService : class
329 where TImplementation : class, TService
330 {
331 lock (SyncRoot)
332 {
333 Container.RegisterSingleton<TService, TImplementation>();
334 }
335 }
336
361 public static TService Resolve<TService>()
362 where TService : class
363 {
364 lock (SyncRoot)
365 {
366 return Container.Resolve<TService>();
367 }
368 }
369
394 public static object Resolve(Type type)
395 {
396 ArgumentNullException.ThrowIfNull(type);
397
398 lock (SyncRoot)
399 {
400 return Container.Resolve(type);
401 }
402 }
403
436 public static bool TryResolve<TService>(out TService? result)
437 where TService : class
438 {
439 try
440 {
441 lock (SyncRoot)
442 {
443 result = Container.Resolve<TService>();
444 return true;
445 }
446 }
447 catch (InvalidOperationException)
448 {
449 result = null;
450 return false;
451 }
452 }
453
478 [System.Obsolete("Use DreamineAutoRegistrar.RegisterAll(rootAssembly, DMContainer) instead.")]
479 public static void AutoRegisterAll(Assembly rootAssembly)
480 {
482 }
483
508 public static bool IsRegistered<TService>()
509 {
510 lock (SyncRoot)
511 {
512 return Container.IsRegistered(typeof(TService));
513 }
514 }
515
540 public static bool IsRegistered(Type serviceType)
541 {
542 ArgumentNullException.ThrowIfNull(serviceType);
543
544 lock (SyncRoot)
545 {
546 return Container.IsRegistered(serviceType);
547 }
548 }
549 }
550}
static DreamineContainer CreateDefaultContainer()
static bool IsRegistered(Type serviceType)
static readonly object SyncRoot
static void AutoRegisterAll(Assembly rootAssembly)
static IServiceContainer Container
static void RegisterSingleton< TService, TImplementation >()
static void RegisterSingleton< TService >(TService instance)
static bool IsRegistered< TService >()
static void RegisterSingleton< TImplementation >()
static void Register< TImplementation >()
static void SetContainer(IServiceContainer container)
static TService Resolve< TService >()
static void Register< TService >(Func< TService > factory)
static IServiceResolver GetResolver()
static void Register< TService, TImplementation >()
static object Resolve(Type type)
static bool TryResolve< TService >(out TService? result)
static void RegisterAll(Assembly rootAssembly, IServiceRegistry registry)