Exemplo n.º 1
0
        /// <summary>
        /// 该类型是否已注册
        /// </summary>
        /// <param name="type">对象类型</param>
        /// <param name="name">服务名称</param>
        /// <returns></returns>
        public bool IsRegistered(Type type, string name = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return(Container.IsRegistered(type));
            }

            return(Container.IsRegisteredWithName(name, type));
        }
Exemplo n.º 2
0
        private void RegisterServices()
        {
            var containerBuilder = new Autofac.ContainerBuilder();

            containerBuilder.RegisterType <SqliteDataStore <Entities.Transaction> >().As <IDataStore <Entities.Transaction> >();
            containerBuilder.Register <AutoMapper.IMapper>(cc => _automapperCfg.CreateMapper());
            _container = containerBuilder.Build();
            DependencyResolver.ResolveUsing((t, ro) => _container.IsRegistered(t) ? _container.Resolve(t) : null);
            System.Threading.Tasks.Task.Run(() => DependencyService.Resolve <IDataStore <Entities.Transaction> >().CreateTablesAsync());
        }
Exemplo n.º 3
0
 protected override object GetInstance(Type serviceType, string key)
 {
     if (string.IsNullOrWhiteSpace(key))
     {
         if (_container.IsRegistered(serviceType))
         {
             return(_container.Resolve(serviceType));
         }
     }
     else
     {
         var type = Type.GetType(key);
         if (_container.IsRegistered(type))
         {
             return(_container.Resolve(type));
         }
     }
     throw new Exception(string.Format("Could not locate any instances for type {0}.", serviceType));
 }
Exemplo n.º 4
0
        public IServiceRegister Register <TService>(Func <IServiceProvider, TService> serviceCreator) where TService : class
        {
            if (serviceCreator == null)
            {
                throw new ArgumentNullException("serviceCreator");
            }

            var builder = initialBuilder ?? new ContainerBuilder();

            builder
            .Register(c => serviceCreator(this))
            .SingleInstance();

            if (container != null && !container.IsRegistered <TService>())
            {
                builder.Update(container);
            }

            return(this);
        }
Exemplo n.º 5
0
 protected override object GetInstance(Type serviceType, string key)
 {
     if (string.IsNullOrWhiteSpace(key))
     {
         if (container.IsRegistered(serviceType))
         {
             return(container.Resolve(serviceType));
         }
     }
     else if (container.IsRegisteredWithKey(key, serviceType))
     {
         return(container.ResolveKeyed(key, serviceType));
     }
     throw new Exception($"Could not locate any instances of contract {key ?? serviceType.Name}.");
 }
 /// <summary>
 /// Initializes the composer with its required dependencies from the provided container
 /// </summary>
 /// <param name="container"></param>
 /// <param name="systemId">The integer identifier of this system</param>
 protected Composer(IContainer container, int systemId)
 {
     _systemId   = systemId;
     _instanceId = Guid.NewGuid();
     try
     {
         _dependencyResolver = container;
         _actorManager       = container.Resolve <IActorManager>();
         _statisticsProvider = container.Resolve <IProcessorStatisticsProvider>();
         _connectionFactory  = container.Resolve <IConnectionProvider>();
         // There may not be any external dependencies for this system
         _externalDependencies = container.IsRegistered <IEnumerable <IExternalDependency> >() ? container.Resolve <IEnumerable <IExternalDependency> >() : new List <IExternalDependency>();
     }
     catch (Exception ex)
     {
         EventLog.WriteEntry(GetType().Name, "Fatal exception encountered during start-up: \n" + ex, EventLogEntryType.Error);
     }
 }
Exemplo n.º 7
0
        protected override object GetInstance(Type service, string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                if (_container.IsRegistered(service))
                {
                    return(_container.Resolve(service));
                }
            }
            else
            {
                if (_container.IsRegisteredWithKey(key, service))
                {
                    return(_container.ResolveKeyed(key, service));
                }
            }

            var msgFormat = "Could not locate any instances of contract {0}.";
            var msg       = string.Format(msgFormat, key ?? service.Name);

            throw new Exception(msg);
        }
Exemplo n.º 8
0
 protected override object GetInstance(Type service, string key)
 {
     if (service == null)
     {
         throw new ArgumentNullException(nameof(service));
     }
     if (string.IsNullOrWhiteSpace(key))
     {
         if (_container.IsRegistered(service))
         {
             return(_container.Resolve(service));
         }
     }
     else
     {
         if (_container.IsRegisteredWithName(key, service))
         {
             return(_container.ResolveNamed(key, service));
         }
     }
     throw new Exception($"Could not locate an instances of '{key ?? service.Name}'.");
 }
Exemplo n.º 9
0
 public static bool IsRegistered <TService>()
 {
     ThrowIfNotInitialized();
     return(_container.IsRegistered <TService>());
 }
        /// <inheritdoc />
        /// <summary>
        /// Resolve an instance of the specified type
        /// </summary>
        /// <param name="type">The type to resolve.</param>
        /// <returns>The instance or null when not found</returns>
        public object TryResolve(Type type)
        {
            _Log.DebugFormat("Try to resolve instance of type {0}", type);

            return(_InnerContainer.IsRegistered(type) ? _InnerContainer.Resolve(type) : null);
        }
 /// <summary>
 /// Resolve an instance of the specified type
 /// </summary>
 /// <param name="type">The type to resolve.</param>
 /// <returns>The instance or null when not found</returns>
 public object TryResolve(Type type)
 {
     return(_innerContainer.IsRegistered(type) ? _innerContainer.Resolve(type) : null);
 }
Exemplo n.º 12
0
 public override bool IsRegistered(Type serviceType, string serviceName = null)
 {
     return(string.IsNullOrEmpty(serviceName)
         ? _container.IsRegistered(serviceType)
         : _container.IsRegisteredWithName(serviceName, serviceType));
 }