Example #1
0
        public void Save(string directoryPath, DbProject project)
        {
            if (directoryPath == null) throw new ArgumentNullException(nameof(directoryPath));
            if (project == null) throw new ArgumentNullException(nameof(project));

            // Create directory names for Objects, Adapters, Helpers, ...
            var objectsPath = Path.Combine(directoryPath, Objects);
            var adaptersPath = Path.Combine(directoryPath, Adapters);
            var helpersPath = Path.Combine(directoryPath, Helpers);
            var modulesPath = Path.Combine(directoryPath, Modules);
            var viewModelsPath = Path.Combine(directoryPath, ViewModels);

            // Create directories
            this.CreateDirectory(objectsPath);
            this.CreateDirectory(adaptersPath);
            this.CreateDirectory(helpersPath);
            this.CreateDirectory(modulesPath);
            this.CreateDirectory(viewModelsPath);

            var namespaceName = project.Schema.Name;
            var entities = project.CreateEntities();
            SaveObjects(objectsPath, project, entities, namespaceName);
            SaveAdapters(adaptersPath, project, entities, namespaceName);
            SaveHelpers(helpersPath, project, entities, namespaceName);
            SaveModules(modulesPath, project, entities, namespaceName);
            SaveViewModels(viewModelsPath, project, entities, namespaceName);
        }
Example #2
0
        private static DbProject PhoenixModel()
        {
            var outlets = DbTable.Create(@"Outlets", new[]
            {
                DbColumn.String(@"Name"),
            });
            var visits = DbTable.Create(@"Visits", new[]
            {
                DbColumn.ForeignKey(outlets),
                DbColumn.DateTime(@"Date"),
            });
            var activityTypes = DbTable.Create(@"ActivityTypes", new[]
            {
                DbColumn.String(@"Name"),
            });
            var activities = DbTable.Create(@"Activities", new[]
            {
                DbColumn.DateTime(@"Date"),
                DbColumn.ForeignKey(activityTypes),
                DbColumn.ForeignKey(visits),
            }, @"Activity");
            var brands = DbTable.Create(@"Brands", new[]
            {
                DbColumn.String(@"Name"),
            });
            var flavors = DbTable.Create(@"Flavors", new[]
            {
                DbColumn.String(@"Name"),
            });
            var articles = DbTable.Create(@"Articles", new[]
            {
                DbColumn.String(@"Name"),
                DbColumn.ForeignKey(brands),
                DbColumn.ForeignKey(flavors),
            });
            var activityNoteTypes = DbTable.Create(@"ActivityNoteTypes", new[]
            {
                DbColumn.String(@"Name"),
            });
            var activityNotes = DbTable.Create(@"ActivityNotes", new[]
            {
                DbColumn.String(@"Contents"),
                DbColumn.DateTime(@"Created_At"),
                DbColumn.ForeignKey(activityNoteTypes),
                DbColumn.ForeignKey(activities),
            });

            var schema = new DbSchema(@"Phoenix", new[]
            {
                outlets,
                visits,
                activityTypes,
                activities,
                brands,
                flavors,
                articles,
                activityNoteTypes,
                activityNotes
            });

            var project = new DbProject(schema);

            // Mark tables as Modifiable, all tables are ReadOnly by default
            project.MarkModifiable(visits);
            project.MarkModifiable(activities);
            project.MarkModifiable(activityNotes);

            // Attach Inverse tables
            project.AttachInverseTable(visits);

            // Hidden tables
            project.MarkHidden(brands);
            project.MarkHidden(flavors);
            return project;
        }
Example #3
0
        private static void GenerateProject(DbProject project, string directoryPath)
        {
            var buffer = new StringBuilder(1024 * 80);

            foreach (var entity in project.CreateEntities())
            {
                // Classes
                var entityClass = project.CreateEntityClass(entity);
                buffer.AppendLine(entityClass);
                buffer.AppendLine(EntityClass.GenerateClassViewModel(entity, !project.IsModifiable(entity.Table)));
                buffer.AppendLine(project.CreateTableViewModel(entity));

                if (!project.IsModifiable(entity.Table))
                {
                    buffer.AppendLine(project.CreateTableViewModel(entity));
                }

                // Read Only adapters
                var adapter = !project.IsModifiable(entity.Table)
                    ? project.CreateEntityAdapter(entity)
                    : project.CreateEntityAdapter(entity);
                buffer.AppendLine(adapter);
            }

            var clrProject = new ClrProject();
            clrProject.WriteAllText = File.WriteAllText;
            clrProject.CreateDirectory = path =>
            {
                if (Directory.Exists(path))
                {
                    Directory.Delete(path, true);
                }
                Directory.CreateDirectory(path);
            };
            clrProject.Save(directoryPath, project);

            var tmp = DbScript.CreateTables(project.Schema.Tables);
            Console.WriteLine(tmp);
        }
Example #4
0
        private static DbProject WordpressModel()
        {
            var users = DbTable.Create(@"Users", new[]
            {
                DbColumn.String(@"Name"),
            });

            var blogs = DbTable.Create(@"Blogs", new[]
            {
                DbColumn.String(@"Name"),
                DbColumn.String(@"Description"),
                DbColumn.ForeignKey(users)
            });

            var posts = DbTable.Create(@"Posts", new[]
            {
                DbColumn.String(@"Title"),
                DbColumn.String(@"Contents"),
                DbColumn.DateTime(@"CreationDate"),
                DbColumn.ForeignKey(blogs),
            });

            var comments = DbTable.Create(@"Comments", new[]
            {
                DbColumn.String(@"Contents"),
                DbColumn.DateTime(@"CreationDate"),
                DbColumn.ForeignKey(users),
                DbColumn.ForeignKey(posts),
            });

            var schema = new DbSchema(@"WordPress", new[]
            {
                users,
                blogs,
                posts,
                comments,
            });
            var project = new DbProject(schema);

            project.AttachInverseTable(posts);

            project.MarkModifiable(blogs);
            project.MarkModifiable(posts);
            project.MarkModifiable(comments);
            return project;
        }
Example #5
0
 private void SaveViewModels(string directoryPath, DbProject project, Entity[] entities, string namespaceName)
 {
     foreach (var entity in entities)
     {
         if (project.IsHidden(entity.Table))
         {
             continue;
         }
         SaveClassViewModel(directoryPath, project, entity, namespaceName);
         SaveTableViewModel(directoryPath, project, entity, namespaceName);
     }
 }
Example #6
0
        private void SaveTableViewModel(string directoryPath, DbProject project, Entity entity, string namespaceName)
        {
            var className = entity.Table.Name + @"ViewModel" + @".cs";
            var sourceCode = project.CreateTableViewModel(entity);

            var buffer = new StringBuilder(sourceCode.Length);
            AddUsingsForTableViewModels(namespaceName, buffer, !project.IsModifiable(entity.Table));
            AddNamespace(buffer, namespaceName, ViewModels, sourceCode);

            SaveToFile(directoryPath, className, buffer);
        }
Example #7
0
        private void SaveObjects(string directoryPath, DbProject project, Entity[] entities, string namespaceName)
        {
            foreach (var entity in entities)
            {
                var className = entity.Class.Name + @".cs";
                var sourceCode = project.CreateEntityClass(entity);

                var buffer = new StringBuilder(sourceCode.Length);
                AddUsingsForObjects(buffer, entity, project);
                AddNamespace(buffer, namespaceName, Objects, sourceCode);

                SaveToFile(directoryPath, className, buffer);
            }
        }
Example #8
0
        private void SaveModules(string directoryPath, DbProject project, Entity[] entities, string namespaceName)
        {
            foreach (var entity in entities)
            {
                if (project.IsHidden(entity.Table))
                {
                    continue;
                }

                var className = entity.Class.Name + @"Module" + @".cs";
                var sourceCode = project.CreateClassModule(entity);

                var buffer = new StringBuilder(sourceCode.Length);
                AddUsingsForModules(namespaceName, buffer, !project.IsModifiable(entity.Table));
                AddNamespace(buffer, namespaceName, Modules, sourceCode);

                SaveToFile(directoryPath, className, buffer);
            }
        }
Example #9
0
        private void SaveHelpers(string directoryPath, DbProject project, Entity[] entities, string namespaceName)
        {
            foreach (var entity in entities)
            {
                // Create helpers only fore ReadOnly tables
                if (project.IsModifiable(entity.Table))
                {
                    continue;
                }

                var className = entity.Class.Name + @"Helper" + @".cs";
                var sourceCode = project.CreateEntityHelper(entity);

                var buffer = new StringBuilder(sourceCode.Length);
                AddUsingsForHelpers(buffer, namespaceName);
                AddNamespace(buffer, namespaceName, Helpers, sourceCode);

                SaveToFile(directoryPath, className, buffer);
            }
        }
Example #10
0
        private void SaveAdapters(string directoryPath, DbProject project, Entity[] entities, string namespaceName)
        {
            foreach (var entity in entities)
            {
                var className = entity.Class.Name + @"Adapter" + @".cs";
                var sourceCode = project.CreateEntityAdapter(entity);

                var buffer = new StringBuilder(sourceCode.Length);
                AddUsingsForAdapters(buffer, entity, namespaceName, !project.IsModifiable(entity.Table));
                AddNamespace(buffer, namespaceName, Adapters, sourceCode);

                SaveToFile(directoryPath, className, buffer);
            }
        }
Example #11
0
        private void AddUsingsForObjects(StringBuilder buffer, Entity entity, DbProject project)
        {
            var hasReferenceType = false;
            foreach (var clrProperty in entity.Class.Properties)
            {
                if (clrProperty.Type.IsReference)
                {
                    hasReferenceType = true;
                    break;
                }
            }
            if (hasReferenceType)
            {
                // For ArgumentNullException class
                buffer.AppendLine(@"using System;");
            }
            if (project.IsModifiable(entity.Table))
            {
                // For IDbObject interface
                buffer.AppendLine(@"using Cchbc.Objects;");
            }
            if (entity.InverseTable != null)
            {
                // For the List<T> class
                buffer.AppendLine(@"using System.Collections.Generic;");
            }

            // Separate usings from namespace if any usings
            var hasUsings = buffer.Length > 0;
            if (hasUsings)
            {
                buffer.AppendLine();
            }
        }