示例#1
0
        private IService GetService <TContract>()
        {
            TypeVerifier.ThrowExceptionIfNotRegistered(_services, typeof(TContract));

            var obj = _services[typeof(TContract)];

            switch (obj.LifeTime)
            {
            case LifeTime.Transient:
            {
                var contract       = obj.Contract;
                var implementation = obj.Implementation;
                var instance       = Resolve(typeof(TContract));
                var lifeTime       = obj.LifeTime;
                return(new Service(contract, implementation, instance, lifeTime));
            }

            case LifeTime.Singleton when !obj.InstanceInitialized:
                obj.Instance = Resolve(typeof(TContract));
                return(obj);

            default:
                return(obj);
            }
        }
示例#2
0
        public void RegisterSingletonInstance <TContract, TImplementation>(TImplementation instance)
        {
            TypeVerifier.ThrowExceptionIfAlreadyRegistered(_services, typeof(TContract));
            TypeVerifier.ThrowExceptionIfNotSubtype(typeof(TContract), typeof(TImplementation));

            _services[typeof(TContract)] =
                new Service(typeof(TContract), typeof(TImplementation), instance, LifeTime.Singleton);
        }
示例#3
0
        public void RegisterTransient <TContract, TImplementation>()
        {
            TypeVerifier.ThrowExceptionIfAlreadyRegistered(_services, typeof(TContract));
            TypeVerifier.ThrowExceptionIfNotSubtype(typeof(TContract), typeof(TImplementation));

            _services[typeof(TContract)]
                = new Service(typeof(TContract), typeof(TImplementation), LifeTime.Transient);
        }