Exemplo n.º 1
0
        private void ExecuteBulkInsertAI(OrderingSettings settings, ILogger <OrderingContextSeed> logger)
        {
            const string insertBuyers     = @"BULK INSERT [Microsoft.eShopOnContainers.Services.OrderingDb].ordering.buyers  
FROM '\var\opt\bulk\orderBuyers.csv' WITH (FORMAT='CSV', FIRSTROW=2)";
            const string insertOrders     = @"BULK INSERT [Microsoft.eShopOnContainers.Services.OrderingDb].ordering.orders  
FROM '\var\opt\bulk\orders.csv' WITH (FORMAT='CSV', FIRSTROW=2)";
            const string insertOrderItems = @"BULK INSERT [Microsoft.eShopOnContainers.Services.OrderingDb].ordering.orderItems  
FROM '\var\opt\bulk\orderItems.csv' WITH (FORMAT='CSV', FIRSTROW=2)";

            using (var conn = new SqlConnection(settings.ConnectionString))
            {
                try
                {
                    conn.Open();

                    conn.Execute(insertBuyers);
                    conn.Execute(insertOrders);
                    conn.Execute(insertOrderItems);
                }
                catch (SqlException exception)
                {
                    logger.LogCritical($"FATAL ERROR: Database connections could not be opened: {exception.Message}");
                }
            }
        }
        private static void ProcessUsings(SyntaxNodeAnalysisContext context, OrderingSettings orderingSettings, SyntaxList <UsingDirectiveSyntax> usings)
        {
            var usingDirectives       = new List <UsingDirectiveSyntax>();
            var systemUsingDirectives = new List <UsingDirectiveSyntax>();

            foreach (var usingDirective in usings)
            {
                if (usingDirective.IsPrecededByPreprocessorDirective())
                {
                    CheckIncorrectlyOrderedUsingsAndReportDiagnostic(context, usingDirectives);
                    CheckIncorrectlyOrderedUsingsAndReportDiagnostic(context, systemUsingDirectives);
                    usingDirectives.Clear();
                    systemUsingDirectives.Clear();
                }

                if (IsAliasOrStaticUsingDirective(usingDirective))
                {
                    continue;
                }

                if (usingDirective.HasNamespaceAliasQualifier() ||
                    !usingDirective.IsSystemUsingDirective() ||
                    !orderingSettings.SystemUsingDirectivesFirst)
                {
                    usingDirectives.Add(usingDirective);
                }
                else
                {
                    systemUsingDirectives.Add(usingDirective);
                }
            }

            CheckIncorrectlyOrderedUsingsAndReportDiagnostic(context, usingDirectives);
            CheckIncorrectlyOrderedUsingsAndReportDiagnostic(context, systemUsingDirectives);
        }
 public GracePeriodManagerService(IOptions <OrderingSettings> settings,
                                  IEventBus eventBus,
                                  ILogger <GracePeriodManagerService> logger)
 {
     _logger   = logger ?? throw new ArgumentNullException(nameof(logger));
     _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
     _settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings));
 }
        public GracePeriodManagerService(IOptions <OrderingSettings> settings,
                                         IEndpointInstance endpoint,
                                         ILogger <GracePeriodManagerService> logger)
        {
            _endpoint = endpoint;
            _logger   = logger ?? throw new ArgumentNullException(nameof(logger));

            _settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings));
        }
Exemplo n.º 5
0
 public OrderStatusChangedToStockConfirmedDomainEventHandler(
     IOrderRepository orderRepository, ILoggerFactory logger,
     IOrderingIntegrationEventService orderingIntegrationEventService,
     IOptionsSnapshot <OrderingSettings> settings)
 {
     _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
     _logger          = logger ?? throw new ArgumentNullException(nameof(logger));
     _orderingIntegrationEventService = orderingIntegrationEventService;
     _settings = settings.Value;
 }
 public GracePeriod(IOptions <OrderingSettings> settings)
 {
     this.settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings));
 }
Exemplo n.º 7
0
        private static void CheckUsingDeclarations(SyntaxNodeAnalysisContext context, OrderingSettings orderingSettings, SyntaxList <UsingDirectiveSyntax> usingDirectives)
        {
            UsingDirectiveSyntax lastStaticUsingDirective       = null;
            UsingDirectiveSyntax lastSystemStaticUsingDirective = null;
            UsingDirectiveSyntax firstNonSystemUsing            = null;

            foreach (var usingDirective in usingDirectives)
            {
                if (usingDirective.IsPrecededByPreprocessorDirective())
                {
                    lastStaticUsingDirective       = null;
                    lastSystemStaticUsingDirective = null;
                    firstNonSystemUsing            = null;
                }

                if (usingDirective.StaticKeyword.IsKind(SyntaxKind.StaticKeyword))
                {
                    if (orderingSettings.SystemUsingDirectivesFirst && usingDirective.IsSystemUsingDirective())
                    {
                        if (firstNonSystemUsing != null)
                        {
                            context.ReportDiagnostic(Diagnostic.Create(
                                                         Descriptor,
                                                         firstNonSystemUsing.GetLocation(),
                                                         new[] { firstNonSystemUsing.Name.ToNormalizedString(), usingDirective.Name.ToNormalizedString() }));
                            return;
                        }

                        if (lastSystemStaticUsingDirective != null)
                        {
                            var firstName  = lastSystemStaticUsingDirective.Name;
                            var secondName = usingDirective.Name;

                            if (NameSyntaxHelpers.Compare(firstName, secondName) > 0)
                            {
                                context.ReportDiagnostic(Diagnostic.Create(
                                                             Descriptor,
                                                             lastSystemStaticUsingDirective.GetLocation(),
                                                             new[] { firstName.ToNormalizedString(), secondName.ToNormalizedString() }));
                                return;
                            }
                        }

                        lastSystemStaticUsingDirective = usingDirective;
                    }
                    else
                    {
                        if (lastStaticUsingDirective != null)
                        {
                            var firstName  = lastStaticUsingDirective.Name;
                            var secondName = usingDirective.Name;

                            if (NameSyntaxHelpers.Compare(firstName, secondName) > 0)
                            {
                                context.ReportDiagnostic(Diagnostic.Create(
                                                             Descriptor,
                                                             lastStaticUsingDirective.GetLocation(),
                                                             new[] { firstName.ToNormalizedString(), secondName.ToNormalizedString() }));
                                return;
                            }
                        }

                        lastStaticUsingDirective = usingDirective;
                        firstNonSystemUsing      = firstNonSystemUsing ?? usingDirective;
                    }
                }
            }
        }
        private static async Task <Document> GetTransformedDocumentAsync(Document document, SyntaxNode syntaxRoot, CancellationToken cancellationToken)
        {
            var compilationUnit = (CompilationUnitSyntax)syntaxRoot;

            var settings      = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var indentationOptions = IndentationOptions.FromDocument(document);

            var usingsHelper   = new UsingsHelper(settings, semanticModel, document, compilationUnit);
            var namespaceCount = CountNamespaces(compilationUnit.Members);

            // Only move using declarations inside the namespace when
            // - There are no global attributes
            // - There is only a single namespace declared at the top level
            // - OrderingSettings.UsingDirectivesPlacement is set to InsideNamespace
            UsingDirectivesPlacement usingDirectivesPlacement;

            OrderingSettings orderingSettings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken).OrderingRules;

            switch (orderingSettings.UsingDirectivesPlacement)
            {
            case UsingDirectivesPlacement.InsideNamespace:
                if (compilationUnit.AttributeLists.Any() ||
                    compilationUnit.Members.Count > 1 ||
                    namespaceCount > 1)
                {
                    // Override the user's setting with a more conservative one
                    usingDirectivesPlacement = UsingDirectivesPlacement.Preserve;
                }
                else if (namespaceCount == 0)
                {
                    usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
                }
                else
                {
                    usingDirectivesPlacement = UsingDirectivesPlacement.InsideNamespace;
                }

                break;

            case UsingDirectivesPlacement.OutsideNamespace:
                usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
                break;

            case UsingDirectivesPlacement.Preserve:
            default:
                usingDirectivesPlacement = UsingDirectivesPlacement.Preserve;
                break;
            }

            string usingsIndentation;

            if (usingDirectivesPlacement == UsingDirectivesPlacement.InsideNamespace)
            {
                var rootNamespace    = compilationUnit.Members.OfType <NamespaceDeclarationSyntax>().First();
                var indentationLevel = IndentationHelper.GetIndentationSteps(indentationOptions, rootNamespace);
                usingsIndentation = IndentationHelper.GenerateIndentationString(indentationOptions, indentationLevel + 1);
            }
            else
            {
                usingsIndentation = string.Empty;
            }

            // - The strategy is to strip all using directive that are not inside a conditional directive and replace them later with a sorted list at the correct spot
            // - The using directives that are inside a conditional directive are replaced (in sorted order) on the spot.
            // - Conditional directives are not moved, as correctly parsing them is too tricky
            // - No using directives will be stripped when there are multiple namespaces. In that case everything is replaced on the spot.
            List <UsingDirectiveSyntax> stripList;
            var replaceMap = new Dictionary <UsingDirectiveSyntax, UsingDirectiveSyntax>();

            // When there are multiple namespaces, do not move using statements outside of them, only sort.
            if (usingDirectivesPlacement == UsingDirectivesPlacement.Preserve)
            {
                BuildReplaceMapForNamespaces(usingsHelper, replaceMap, indentationOptions, false);
                stripList = new List <UsingDirectiveSyntax>();
            }
            else
            {
                stripList = usingsHelper.GetContainedUsings(usingsHelper.RootSpan);
            }

            BuildReplaceMapForConditionalDirectives(usingsHelper, replaceMap, indentationOptions, usingsHelper.RootSpan);

            var usingSyntaxRewriter = new UsingSyntaxRewriter(stripList, replaceMap);
            var newSyntaxRoot       = usingSyntaxRewriter.Visit(syntaxRoot);

            if (usingDirectivesPlacement == UsingDirectivesPlacement.InsideNamespace)
            {
                newSyntaxRoot = AddUsingsToNamespace(newSyntaxRoot, usingsHelper, usingsIndentation, replaceMap.Any());
            }
            else if (usingDirectivesPlacement == UsingDirectivesPlacement.OutsideNamespace)
            {
                newSyntaxRoot = AddUsingsToCompilationRoot(newSyntaxRoot, usingsHelper, usingsIndentation, replaceMap.Any());
            }

            // Final cleanup
            newSyntaxRoot = StripMultipleBlankLines(newSyntaxRoot);
            newSyntaxRoot = ReAddFileHeader(syntaxRoot, newSyntaxRoot);

            var newDocument = document.WithSyntaxRoot(newSyntaxRoot.WithoutFormatting());

            return(newDocument);
        }