Deveel.Data.Services.ServiceContainer.Register C# (CSharp) Метод

Register() публичный Метод

public Register ( ServiceRegistration registration ) : void
registration ServiceRegistration
Результат void
        public void Register(ServiceRegistration registration)
        {
            if (registration == null)
                throw new ArgumentNullException("registration");

            if (container == null)
                throw new InvalidOperationException("The container was not initialized.");

            try {
                lock (this) {
                    var serviceType = registration.ServiceType;
                    var service = registration.Instance;
                    var serviceName = registration.ServiceKey;
                    var implementationType = registration.ImplementationType;

                    var reuse = Reuse.Singleton;
                    if (!String.IsNullOrEmpty(ScopeName))
                        reuse = Reuse.InCurrentNamedScope(ScopeName);

                    if (!String.IsNullOrEmpty(registration.Scope))
                        reuse = Reuse.InCurrentNamedScope(registration.Scope);

                    if (service == null) {
                        container.Register(serviceType, implementationType, serviceKey: serviceName, reuse: reuse);
                    } else {
                        container.RegisterInstance(serviceType, service, serviceKey: serviceName, reuse: reuse);
                    }
                }
            } catch (Exception ex) {
                throw new Exception("Error when registering service.", ex);
            }
        }

Usage Example

Пример #1
0
        public void RegisterAndResolveFromChild()
        {
            var parent = new ServiceContainer();
            parent.Register<TestService1>();

            var child = parent.OpenScope("child");

            var childService = child.Resolve<ITestService>();
            var parentService = parent.Resolve<ITestService>();

            Assert.IsNotNull(childService);
            Assert.IsInstanceOf<TestService1>(childService);

            Assert.IsNotNull(parentService);
            Assert.IsInstanceOf<TestService1>(parentService);

            Assert.AreEqual(parentService, childService);
        }
All Usage Examples Of Deveel.Data.Services.ServiceContainer::Register