Exemplo n.º 1
0
        private void TransformEntities()
        {
            Console.WriteLine("Transforming the different entities");

            foreach (var projectId in originalSolution.ProjectIds)
            {
                var project = solution.GetProject(projectId);

                foreach (var documentId in project.DocumentIds)
                {
                    var document = solution.GetDocument(documentId);

                    var syntaxTree = document.GetSyntaxTreeAsync().Result;
                    var root       = syntaxTree.GetRoot();

                    var cls = root.DescendantNodes().OfType <ClassDeclarationSyntax>().FirstOrDefault();

                    if (cls == null)
                    {
                        continue;
                    }

                    // If the class identifier is one of the elements inside the list of entities, we process it.
                    if (entities.Contains(cls.Identifier.Text))
                    {
                        Console.WriteLine("    -Entity: " + cls.Identifier.Text);
                        var classWithTenantIdProperty = cls.AddMembers(SyntaxBuilder.BuildTenantIdProperty());

                        var classWithAttribute = classWithTenantIdProperty.AddAttributeLists(SyntaxBuilder.BuildTenantAwareAttribute());

                        var newRoot = root.ReplaceNode(cls, classWithAttribute);

                        var newDocument       = document.WithSyntaxRoot(newRoot);
                        var formattedDocument = Formatter.FormatAsync(newDocument).Result;
                        solution = formattedDocument.Project.Solution;
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void TransformDbContextProjectAndPopulateEntities()
        {
            Console.WriteLine("Transforming project containing the DbContext");

            InsertFile(configuration.ContextProjectName, Templates.EntityFrameworkConfigurationTemplate, ClassNames.EntityFrameworkConfigurationClassName, new Dictionary <string, string>()
            {
                { "Namespace", configuration.ContextProjectName }
            });

            InsertFile(configuration.ContextProjectName, Templates.TenantCommandInterceptorTemplate, ClassNames.TenantCommandInterceptorClassName, new Dictionary <string, string>()
            {
                { "Namespace", configuration.ContextProjectName }
            });

            InsertFile(configuration.ContextProjectName, Templates.TenantCommandTreeInterceptorTemplate, ClassNames.TenantCommandTreeInterceptorClassName, new Dictionary <string, string>()
            {
                { "Namespace", configuration.ContextProjectName }
            });

            InsertFile(configuration.ContextProjectName, Templates.TenantQueryVisitorTemplate, ClassNames.TenantQueryVisitorClassName, new Dictionary <string, string>()
            {
                { "Namespace", configuration.ContextProjectName }
            });

            InsertFile(configuration.ContextProjectName, Templates.TenantAwareUtilityTemplate, ClassNames.TenantAwareUtilityClassName, new Dictionary <string, string>()
            {
                { "Namespace", configuration.ContextProjectName },
                { "Context", configuration.ContextTypeName }
            });

            var project = solution.Projects.FirstOrDefault(x => x.Name == configuration.ContextProjectName);

            if (project == null)
            {
                return;
            }

            foreach (Document document in project.Documents)
            {
                var syntaxTree = document.GetSyntaxTreeAsync().Result;
                var root       = syntaxTree.GetRoot();

                var contextClass = root.DescendantNodes().OfType <ClassDeclarationSyntax>().FirstOrDefault(_ => _.Identifier.Text == configuration.ContextTypeName);

                if (contextClass != null)
                {
                    var properties = contextClass.DescendantNodes().OfType <PropertyDeclarationSyntax>();

                    Console.WriteLine("Retrieving the different entities");

                    foreach (var p in properties)
                    {
                        var type = (GenericNameSyntax)p.Type;

                        if (type.Identifier.Text == "DbSet")
                        {
                            var entity = (IdentifierNameSyntax)type.TypeArgumentList.Arguments[0];
                            entities.Add(entity.Identifier.Text);
                            Console.WriteLine("    -Entity: " + entity.Identifier.Text);
                        }
                    }

                    Console.WriteLine("Modifying the " + configuration.ContextTypeName + " class.");

                    var modelCreatingMethod = contextClass.DescendantNodes().OfType <MethodDeclarationSyntax>().FirstOrDefault(_ => _.Identifier.Text == "OnModelCreating");
                    var enrichedMethod      = modelCreatingMethod.AddBodyStatements(SyntaxBuilder.BuildEntityFrameworkConventionBlock().ToArray());

                    var updatedClass = contextClass.ReplaceNode(modelCreatingMethod, enrichedMethod);
                    var newRoot      = root.ReplaceNode(contextClass, updatedClass);

                    var newDocument       = document.WithSyntaxRoot(newRoot);
                    var formattedDocument = Formatter.FormatAsync(newDocument).Result;
                    solution = formattedDocument.Project.Solution;
                }
            }
        }