Ejemplo n.º 1
0
        void UpdateWearItems(object[] nodes)
        {
            var progressPage = NavigationManager.OpenViewModel <ProgressWindowViewModel>(null);
            var progress     = progressPage.ViewModel.Progress;

            using (var localUow = UnitOfWorkFactory.CreateWithoutRoot("Обновление потребностей из журнала норм")) {
                var employeeRepository = AutofacScope.Resolve <EmployeeRepository>(new TypedParameter(typeof(IUnitOfWork), localUow));
                progress.Start(2, text: "Загружаем нормы");
                var norms = localUow.GetById <Norm>(nodes.GetIds()).ToArray();
                progress.Add(text: "Загружаем сотрудников");
                var employees = employeeRepository.GetEmployeesUseNorm(norms);

                progress.Start(employees.Count + 1);
                int step = 0;
                foreach (var employee in employees)
                {
                    progress.Add(text: $"Обработка {employee.ShortName}");
                    step++;
                    employee.UpdateWorkwearItems();
                    localUow.Save(employee);
                    if (step % 10 == 0)
                    {
                        localUow.Commit();
                    }
                }
                progress.Add(text: "Завершаем...");
                localUow.Commit();
            }
            NavigationManager.ForceClosePage(progressPage, CloseSource.FromParentPage);
        }
Ejemplo n.º 2
0
        public void ResolveRequired_Should_Throws_IfTypeIsNotRegistered()
        {
            var scope = new AutofacScope(new ContainerBuilder().Build());

            Assert.Throws <IoCResolutionException>(() => scope.ResolveRequired <ParameterTestClass>());
            Assert.Throws <IoCResolutionException>(() => scope.ResolveRequired(typeof(ParameterTestClass)));
        }
Ejemplo n.º 3
0
        public IDependencyContainer BeginScope(Action <ContainerConfigurator> configuration = null)
        {
            var innerScope = new AutofacContainer();

            innerScope.AutofacScope = AutofacScope.BeginLifetimeScope(builder =>
            {
                new AutofacContainerBuilder(builder).Configure(configuration);
                innerScope.RegisterSelf(builder);
            });
            return(innerScope);
        }
Ejemplo n.º 4
0
        public IDependencyContainerV2 BeginScope(LifetimeScope scope, Action <ContainerConfigurator> configuration = null)
        {
            var innerScope = new AutofacContainer();

            var autofacScope = scope.Equals(LifetimeScope.Local)
                ? new object()
                : scope;

            innerScope.AutofacScope = AutofacScope.BeginLifetimeScope(autofacScope, builder =>
            {
                new AutofacContainerBuilder(builder).Configure(configuration);
                innerScope.RegisterSelf(builder);
            });

            return(innerScope);
        }
        /// <summary>
        /// 注册到autofac
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="fromType"></param>
        /// <param name="bean"></param>
        public static void BindBuildWithScope(this ContainerBuilder builder, Type fromType, BeanAttribute bean)
        {
            AutofacScope scope     = bean.AutofacScope;
            Type         toType    = bean.As;
            string       name      = bean.Named;
            var          registrar = builder.RegisterType(fromType);

            switch (scope)
            {
            case AutofacScope.SingleInstance:
                if (toType == null)
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.Named(name, fromType).SingleInstance().PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.SingleInstance().PropertiesByAttributeAutowired();
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.As(toType).Named(name, toType).SingleInstance().PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.As(toType).SingleInstance().PropertiesByAttributeAutowired();
                    }
                }

                break;

            case AutofacScope.InstancePerDependency:
                if (toType == null)
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.Named(name, fromType).InstancePerDependency().PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.InstancePerDependency().PropertiesByAttributeAutowired();
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.As(toType).Named(name, toType).InstancePerDependency().PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.As(toType).InstancePerDependency().PropertiesByAttributeAutowired();
                    }
                }

                break;

            case AutofacScope.InstancePerOwned:
                if (toType == null)
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.Named(name, fromType).InstancePerLifetimeScope().PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.InstancePerLifetimeScope().PropertiesByAttributeAutowired();
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.As(toType).Named(name, toType).InstancePerOwned(toType).PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.As(toType).InstancePerOwned(toType).PropertiesByAttributeAutowired();
                    }
                }

                break;

            case AutofacScope.InstancePerRequest:
                if (toType == null)
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.Named(name, fromType).InstancePerRequest().PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.InstancePerRequest().PropertiesByAttributeAutowired();
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.As(toType).Named(name, toType).InstancePerRequest().PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.As(toType).InstancePerRequest().PropertiesByAttributeAutowired();
                    }
                }

                break;

            case AutofacScope.InstancePerLifetimeScope:
                if (toType == null)
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.Named(name, fromType).InstancePerLifetimeScope().PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.InstancePerLifetimeScope().PropertiesByAttributeAutowired();
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.As(toType).Named(name, toType).InstancePerLifetimeScope().PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.As(toType).InstancePerLifetimeScope().PropertiesByAttributeAutowired();
                    }
                }

                break;

            case AutofacScope.InstancePerMatchingLifetimeScope:
                if (toType == null)
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.Named(name, fromType).InstancePerMatchingLifetimeScope().PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.InstancePerMatchingLifetimeScope().PropertiesByAttributeAutowired();
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        registrar.As(toType).Named(name, toType).InstancePerMatchingLifetimeScope().PropertiesByAttributeAutowired();
                    }
                    else
                    {
                        registrar.As(toType).InstancePerMatchingLifetimeScope().PropertiesByAttributeAutowired();
                    }
                }

                break;
            }

            if (bean.InterceptorType != null)
            {
                try
                {
                    registrar.EnableInterfaceInterceptors().InterceptedBy(bean.InterceptorType);
                }
                catch (Exception)
                {
                };
            }

            if (string.IsNullOrEmpty(name))
            {
                //默认使用类的名字注册
                var newBean = bean.Clone();
                newBean.Named = fromType.Name;
                BindBuildWithScope(builder, fromType, newBean);
            }
        }
Ejemplo n.º 6
0
 public void Dispose()
 {
     AutofacScope.Dispose();
     ParentScope?.Dispose();
 }
Ejemplo n.º 7
0
 public object Resolve(Type type)
 {
     return(AutofacScope.Resolve(type));
 }
Ejemplo n.º 8
0
        void UpdateLastIssue(EmployeeProcessingJournalNode[] nodes)
        {
            var progressPage = NavigationManager.OpenViewModel <ProgressWindowViewModel>(null);
            var progress     = progressPage.ViewModel.Progress;

            loggerProcessing.Info($"Пересчет сроков носки получного для {nodes.Length} сотрудников");
            loggerProcessing.Info($"База данных: {dataBaseInfo.Name}");

            progress.Start(nodes.Length + 2, text: "Загружаем сотрудников");
            var employees = UoW.GetById <EmployeeCard>(nodes.Select(x => x.Id)).ToArray();

            progress.Add(text: $"Получаем последние выдачи");
            var employeeIssueRepository = AutofacScope.Resolve <EmployeeIssueRepository>(new TypedParameter(typeof(IUnitOfWork), UoW));
            var operations = employeeIssueRepository.GetLastIssueOperationsForEmployee(employees);
            int step       = 0;

            foreach (var employee in employees)
            {
                progress.Add(text: $"Обработка {employee.ShortName}");
                step++;
                var employeeOperations = operations.Where(x => x.Employee.IsSame(employee)).ToList();
                if (employeeOperations.Count == 0)
                {
                    Results[employee.Id] = "Нет выданного";
                    continue;
                }
                int changes = 0;
                foreach (var operation in employeeOperations)
                {
                    if (operation.ProtectionTools == null)
                    {
                        continue;
                    }
                    var oldDate = operation.ExpiryByNorm;
                    var graph   = IssueGraph.MakeIssueGraph(UoW, employee, operation.ProtectionTools);
                    operation.RecalculateDatesOfIssueOperation(graph, baseParameters, interactive);
                    if (operation.ExpiryByNorm?.Date != oldDate?.Date)
                    {
                        UoW.Save(operation);
                        loggerProcessing.Info($"Изменена дата окончания носки с {oldDate:d} на {operation.ExpiryByNorm:d} для выдачи {operation.OperationTime} [{operation.Title}]");
                        changes++;
                        var item = employee.WorkwearItems.FirstOrDefault(x => x.ProtectionTools.IsSame(operation.ProtectionTools));
                        if (item != null)
                        {
                            var lastDate = item.NextIssue;
                            item.UpdateNextIssue(UoW);
                            if (item.NextIssue?.Date != lastDate?.Date)
                            {
                                loggerProcessing.Info($"Изменена дата следующей выдачи с {lastDate:d} на {item.NextIssue:d} для потребности [{item.Title}]");
                            }
                        }
                    }
                }
                if (changes > 0)
                {
                    Results[employee.Id] = NumberToTextRus.FormatCase(changes, "изменена {0} дата", "изменено {0} даты", "изменено {0} дат");
                    UoW.Save(employee);
                }
                else
                {
                    Results[employee.Id] = "Без изменений";
                }
                if (step % 10 == 0)
                {
                    UoW.Commit();
                }
            }
            progress.Add(text: "Готово");
            UoW.Commit();
            NavigationManager.ForceClosePage(progressPage, CloseSource.FromParentPage);
            Refresh();
        }