Ejemplo n.º 1
0
        private void loadAssemblies(List <Class> classes, Template template)
        {
            if (classes.Count == 0)
            {
                return;
            }

            var skipped = new List <Class>();

            foreach (var def in classes)
            {
                _tableDictionary[def.Name] = new List <dynamic>();
                string classString = template.Render(Hash.FromAnonymousObject(new
                                                                              { data = def, presentationProperty = def.PresentationProperty }));
                dynamic c = DynamicClassLoader.createDynamicInstance(classString, "GeneratedClass." + def.Name);
                if (c == null)
                {
                    skipped.Add(def);
                    continue;
                }

                Console.Out.WriteLine("Loaded class {0}", c.GetType());
            }

            loadAssemblies(skipped, template);
        }
Ejemplo n.º 2
0
        public void load()
        {
            Template.RegisterSafeType(typeof(Class),
                                      new[] { "Name", "DisplayName", "Properties", "OverviewTemplate", "PresentationProperty" });
            Template.RegisterSafeType(typeof(Property), new[] { "Name", "DisplayName", "Type" });

            string   baseDir      = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string   templatePath = Path.Combine(baseDir, "Template/ClassTemplate.liquid");
            Template template     = Template.Parse(File.ReadAllText(templatePath));

            var classes = _dbContext.Classes.Include(e => e.Properties).ToList();

            loadAssemblies(classes, template);

            templatePath = Path.Combine(baseDir, "Template/DbContextTemplate.liquid");
            template     = Template.Parse(File.ReadAllText(templatePath));
            string dbContextClassString = template.Render(Hash.FromAnonymousObject(new { data = classes }));

            _storageDbContext = DynamicClassLoader.createDynamicInstance(dbContextClassString,
                                                                         "GeneratedClass.GeneratedDbContext");

            Console.Out.WriteLine("Loaded class {0}", _storageDbContext.GetType());
            _storageDbContext.Database.EnsureCreated();

            try
            {
                foreach (var table in _storageDbContext.GetAllTables())
                {
                    foreach (var a in table.AsQueryable())
                    {
                        string type = a.GetType().ToString();
                        _tableDictionary[type.Split(".")[1]].Add(a);
                        Console.Out.WriteLine("Loaded Object {0}", a.GetType());
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Model has changed - recreating DB" + e.Message);

                _storageDbContext.DetachAllEntries();
                _storageDbContext.Database.EnsureDeleted();
                _storageDbContext = DynamicClassLoader.createDynamicInstance(dbContextClassString,
                                                                             "GeneratedClass.GeneratedDbContext");
                _storageDbContext.Database.EnsureCreated();
                loadFromBackup();
            }
        }
Ejemplo n.º 3
0
        public bool addElement(string type, Dictionary <string, string> data)
        {
            dynamic c  = DynamicClassLoader.createCachedInstance("GeneratedClass." + type);
            var     cl = getClasses().Find(e => e.Name == type);

            foreach (var kvp in data)
            {
                if (kvp.Key == "Id")
                {
                    c.Id = Guid.Parse(kvp.Value);
                    continue;
                }

                var  prop            = cl.Properties.First(e => e.Name == kvp.Key);
                Type t               = c.GetType().GetProperty(kvp.Key).PropertyType;
                bool isPrimitiveType = t.IsPrimitive || t.IsValueType || (t == typeof(string));

                if (!isPrimitiveType)
                {
                    var     id = Guid.Parse(kvp.Value);
                    dynamic d  = _tableDictionary[prop.Type].Find(p => p.Id.Equals(id));
                    if (d == null)
                    {
                        Console.Out.WriteLine(" missing id = {0} of type {1}", id, prop.Type);
                        return(false);
                    }

                    c.GetType().GetProperty(kvp.Key)
                    .SetValue(c, Convert.ChangeType(d, c.GetType().GetProperty(kvp.Key).PropertyType),
                              null);
                    Console.Out.WriteLine("kvp = {0}", id);
                }
                else
                {
                    dynamic val;
                    try
                    {
                        val = Convert.ChangeType(kvp.Value, c.GetType().GetProperty(kvp.Key).PropertyType);
                    }
                    catch (Exception ex)
                    {
                        return(false);
                    }

                    c.GetType().GetProperty(kvp.Key)
                    .SetValue(c, val,
                              null);
                    var constraint = cl.Properties.Single(p => p.Name == kvp.Key).Constraint;
                    if (constraint != null && (val < constraint.MinValue || val > constraint.MaxValue))
                    {
                        return(false);
                    }

                    Console.Out.WriteLine("kvp = {0}", kvp.Value);
                }
            }

            Console.Out.WriteLine("c = {0}", c.Id);
            dynamic dbset = _storageDbContext.GetType().GetProperty(type).GetValue(_storageDbContext, null);

            dbset.GetType().GetMethod("Add").Invoke(dbset, new[] { c });
            _storageDbContext.SaveChanges();
            _tableDictionary[type].Add(c);
            return(true);
        }