Beispiel #1
0
        public object CreateObject(
            IScopedStateDescriptor <TKey> descriptor,
            Func <object> serviceFactory)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            if (serviceFactory == null)
            {
                throw new ArgumentNullException(nameof(serviceFactory));
            }

            if (!_instances.TryGetValue(descriptor.Key, out object instance))
            {
                lock (_sync)
                {
                    if (!_instances.TryGetValue(descriptor.Key, out instance))
                    {
                        instance   = serviceFactory();
                        _instances = _instances
                                     .SetItem(descriptor.Key, instance);
                    }
                }
            }

            return(instance);
        }
Beispiel #2
0
        public Func <object> CreateFactory(
            IServiceProvider services,
            IScopedStateDescriptor <TKey> descriptor)
        {
            if (descriptor.Factory != null)
            {
                return(() => descriptor.Factory(services));
            }

            var serviceFactory = new ServiceFactory();

            serviceFactory.Services = services;
            return(() => serviceFactory.CreateInstance(descriptor.Type));
        }
Beispiel #3
0
 public bool TryGetDescriptor(
     TKey key,
     out IScopedStateDescriptor <TKey> descriptor)
 {
     return(_descriptors.TryGetValue(key, out descriptor));
 }