Пример #1
0
        public object Resolve(Type type)
        {
            if (IsRegistered(type))
            {
                var dependencyModel = _registeredTypes.Single(p => p.From == type);

                var lifetimeManager = DobbyInstanceService.GetInstance(dependencyModel.LifetimeManager) as ILifetimeManager;

                if (lifetimeManager.GetKey() == dependencyModel.LifetimeManagerKey)
                {
                    lifetimeManager.Dispose();
                    return(dependencyModel.ResolvedType);
                }
                else
                {
                    _registeredTypes.Remove(dependencyModel);

                    dependencyModel.ResolvedType       = DobbyInstanceService.GetInstance(dependencyModel.To);
                    dependencyModel.LifetimeManagerKey = lifetimeManager.GetKey();

                    lifetimeManager.Dispose();

                    _registeredTypes.Add(dependencyModel);

                    return(dependencyModel.ResolvedType);
                }
            }
            else
            {
                throw new Exception($"{type} is not registered to DobbyContainer.");
            }
        }
Пример #2
0
        public void Register(Type i, Type c, ILifetimeManager lifetimeManager = null)
        {
            if (lifetimeManager == null)
            {
                lifetimeManager = new PerResolveLifetimeManager();
            }

            var dependencyModel = new DependencyModel
            {
                From               = i,
                To                 = c,
                LifetimeManager    = lifetimeManager.GetType(),
                LifetimeManagerKey = lifetimeManager.GetKey(),
                ResolvedType       = DobbyInstanceService.GetInstance(c)
            };

            _registeredTypes.Add(dependencyModel);
            lifetimeManager.Dispose();
        }
Пример #3
0
 public object ResolveNotRegisteredType(Type type)
 {
     return(DobbyInstanceService.GetInstance(type));
 }