예제 #1
0
        /// <summary>
        /// 根据泛型T进行依赖解析
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="regName"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public T Resolve <T>(string regName = null, params KeyValuePair <string, object>[] parameters)
            where T : class
        {
            Autofac.ILifetimeScope scope = this._lifetimeScop.GetScope(this._container);

            if (null != parameters && parameters.Length > 0)
            {
                List <Autofac.Core.Parameter> paramList = new List <Autofac.Core.Parameter>();
                foreach (var item in parameters)
                {
                    paramList.Add(new NamedParameter(item.Key, item.Value));
                }

                if (string.IsNullOrEmpty(regName))
                {
                    return(scope.Resolve <T>(paramList));
                }
                else
                {
                    return(scope.ResolveNamed <T>(regName, paramList));
                }
            }
            else
            {
                if (string.IsNullOrEmpty(regName))
                {
                    return(scope.Resolve <T>());
                }
                else
                {
                    return(scope.ResolveNamed <T>(regName));
                }
            }
        }
예제 #2
0
        public static ILifetimeScope CreateLifetimeScope(Autofac.ILifetimeScope scope)
        {
            var iocScope = new IocLifetimeScope(scope);

            CallContextUtility.SetData <ILifetimeScope>(iocScope);
            return(iocScope);
        }
예제 #3
0
        /// <summary>
        /// 解析未注册的类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public object ResolveUnregistered(Type type)
        {
            Autofac.ILifetimeScope scope = this._lifetimeScop.GetScope(this._container);
            var constructors             = type.GetConstructors();

            foreach (var constructor in constructors)
            {
                try
                {
                    var parameters         = constructor.GetParameters();
                    var parameterInstances = new List <object>();
                    foreach (var parameter in parameters)
                    {
                        var service = scope.Resolve(parameter.ParameterType);
                        if (service == null)
                        {
                            throw new Exception(string.Format("未知的参数类型{0}",
                                                              parameter.ParameterType));
                        }
                        parameterInstances.Add(service);
                    }
                    return(Activator.CreateInstance(type, parameterInstances.ToArray()));
                }
                catch
                {
                    continue;
                }
            }
            throw new Exception("未发现满足依赖解析的构造函数");
        }
예제 #4
0
        protected ViewModelFactory(Autofac.ILifetimeScope container, IFrozenContext frozenCtx, ZetboxConfig cfg, IPerfCounter perfCounter, Func <DialogCreator> dialogFactory)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (frozenCtx == null)
            {
                throw new ArgumentNullException("frozenCtx");
            }
            if (cfg == null)
            {
                throw new ArgumentNullException("cfg");
            }
            if (dialogFactory == null)
            {
                throw new ArgumentNullException("dialogFactory");
            }

            this.Container              = container;
            this.FrozenContext          = frozenCtx;
            this.Configuration          = cfg;
            this.Managers               = new Dictionary <IZetboxContext, IMultipleInstancesManager>();
            this._viewModelFactoryCache = new Dictionary <VMCacheKey, object>();
            this.PerfCounter            = perfCounter;
            this.DialogFactory          = dialogFactory;
        }
        protected override void ConfigureRequestContainer(Autofac.ILifetimeScope existingContainer)
        {
            var builder = new ContainerBuilder();

            builder.RegisterType <IdeastrikeContext>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <IdeaRepository>()
            .AsImplementedInterfaces()
            .SingleInstance();

            builder.RegisterType <ActivityRepository>()
            .AsImplementedInterfaces()
            .SingleInstance();

            builder.RegisterType <FeatureRepository>()
            .AsImplementedInterfaces()
            .SingleInstance();

            builder.RegisterType <SettingsRepository>()
            .AsImplementedInterfaces()
            .SingleInstance();

            builder.RegisterType <UserRepository>()
            .AsImplementedInterfaces()
            .SingleInstance();

            builder.RegisterType <ImageRepository>()
            .AsImplementedInterfaces()
            .SingleInstance();

            builder.Update(existingContainer.ComponentRegistry);
        }
        static void Main(string[] args)
        {
            using (Autofac.ILifetimeScope scope = Ioc.ObjectFactory.Container.BeginLifetimeScope())
            {
                var credential = scope.Resolve <ICredentialHandler>();
                consoleMessage();
                var key = Console.ReadKey();
                while (key.Key != ConsoleKey.Escape)
                {
                    switch (key.KeyChar)
                    {
                    case 'u':
                    case 'U':
                        generatePassword(credential);
                        goto default;

                    case 'v':
                    case 'V':
                        validatePassword(credential);
                        break;

                    default:
                        consoleMessage();
                        break;
                    }
                    key = Console.ReadKey();
                }
            }
        }
예제 #7
0
 public IContainer CreateContainer()
 {
     _container    = Builder.Build();
     ContainerRoot = _container;
     DatabaseInit();
     //ModulesInstaller();
     return(_container);
 }
예제 #8
0
        protected ExcelTaskManager(Autofac.ILifetimeScope lifetimeScope)
        {
            this.lifetimeScope = lifetimeScope;
            IMgLogger mgLogger;

            lifetimeScope.TryResolve <IMgLogger>(out mgLogger);
            if (mgLogger != null)
            {
                log = mgLogger.CreateLog();
            }
        }
예제 #9
0
        protected override void RequestStartup(Autofac.ILifetimeScope container, Nancy.Bootstrapper.IPipelines pipelines, NancyContext context)
        {
            TokenAuthentication.Enable(pipelines, new TokenAuthenticationConfiguration(container.Resolve<ITokenizer>()));
            pipelines.AfterRequest.AddItemToEndOfPipeline(AddCorsHeaders());

            pipelines.OnError.AddItemToEndOfPipeline((ctx, err) =>
                HandleExceptions(err, ctx)
                );

        
            base.RequestStartup(container, pipelines, context);
        }
예제 #10
0
        /// <summary>
        /// 是否可被解析出来
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="regName"></param>
        /// <returns></returns>
        public bool IsRegistered <T>(string regName = null)
        {
            Autofac.ILifetimeScope scope = this._lifetimeScop.GetScope(this._container);

            if (string.IsNullOrEmpty(regName))
            {
                return(scope.IsRegistered <T>());
            }
            else
            {
                return(scope.IsRegisteredWithName <T>(regName));
            }
        }
예제 #11
0
        /// <summary>
        /// 根据泛型T进行依赖解析
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public T[] ResolveAll <T>(params KeyValuePair <string, object>[] parameters)
        {
            Autofac.ILifetimeScope scope = this._lifetimeScop.GetScope(this._container);

            if (null != parameters && parameters.Length > 0)
            {
                List <Autofac.Core.Parameter> paramList = new List <Autofac.Core.Parameter>();
                foreach (var item in parameters)
                {
                    paramList.Add(new NamedParameter(item.Key, item.Value));
                }

                return(scope.Resolve <IEnumerable <T> >(paramList).ToArray());
            }
            else
            {
                return(scope.Resolve <IEnumerable <T> >().ToArray());
            }
        }
예제 #12
0
 public SmallCrmFixture()
 {
     Container = ServiceRegistrator.GetContainer().BeginLifetimeScope();
     DbContext = Container.Resolve <SmallCrmDbContext>();
 }
예제 #13
0
 public FileBrowser(ApplicationSettingsService applicationSettingsService, ImageLoaderService imageLoaderService, Autofac.ILifetimeScope scope)
 {
     _applicationSettingsService = applicationSettingsService;
     _applicationSettingsService.LoadSettings();
     _imageLoaderService = imageLoaderService;
     _scope = scope;
     InitializeComponent();
 }
예제 #14
0
 /// <summary>
 /// This method is called to execute the action.
 /// </summary>
 protected abstract void InvokeCore(Autofac.ILifetimeScope unitOfWork, string[] args);
예제 #15
0
 protected override void InvokeCore(Autofac.ILifetimeScope unitOfWork, string[] args)
 {
 }
예제 #16
0
 protected override void InvokeCore(Autofac.ILifetimeScope unitOfWork, string[] args)
 {
     Logging.Log.Info("Waiting for console input to shutdown");
     Console.WriteLine("Hit the anykey to exit");
     Console.ReadKey();
 }
예제 #17
0
 private IocLifetimeScope(Autofac.ILifetimeScope scope)
 {
     this.scope = scope;
 }
예제 #18
0
 public SchedulerService(Autofac.ILifetimeScope scope)
 {
     this._scope = scope;
 }
예제 #19
0
 /// <summary>
 /// 尝试解析指定的类型
 /// </summary>
 /// <param name="serviceType"></param>
 /// <param name="instance"></param>
 /// <returns></returns>
 public bool TryResolve(Type serviceType, out object instance)
 {
     Autofac.ILifetimeScope scope = this._lifetimeScop.GetScope(this._container);
     return(scope.TryResolve(serviceType, out instance));
 }
예제 #20
0
 public WpfModelFactory(Autofac.ILifetimeScope container, IUiThreadManager uiThread, IFrozenContext frozenCtx, ZetboxConfig cfg, IPerfCounter perfCounter, Func <DialogCreator> dialogFactory)
     : base(container, frozenCtx, cfg, perfCounter, dialogFactory)
 {
     this.uiThread = uiThread;
 }
예제 #21
0
 public LifetimeScopeImpl(AF.ILifetimeScope lifetimeScope)
     : base(lifetimeScope)
 {
     _lifetimeScope = lifetimeScope;
 }
예제 #22
0
 public void CreateContainer(ILifetimeScope root)
 {
     ContainerRoot = root;
     DatabaseInit();
     //ModulesInstaller();
 }