Exemple #1
0
 public static void SetModelDerivedMappings()
 {
     using (var scope = PuckCache.ServiceProvider.CreateScope())
     {
         var apiHelper = scope.ServiceProvider.GetService <I_Api_Helper>();
         PuckCache.ModelDerivedModels = new Dictionary <string, List <Type> >();
         var modelTypes = apiHelper.Models();
         foreach (var modelType in modelTypes)
         {
             //var derivedClasses = ApiHelper.TypeChainType(modelType);
             var derivedClasses = ApiHelper.FindDerivedClasses(modelType, inclusive: true).ToList();
             PuckCache.ModelDerivedModels[modelType.Name] = derivedClasses;
         }
     }
 }
Exemple #2
0
        public static void UpdateAQNMappings()
        {
            using (var scope = PuckCache.ServiceProvider.CreateScope())
            {
                var apiHelper = scope.ServiceProvider.GetService <I_Api_Helper>();
                foreach (var t in apiHelper.AllModels(true))
                {
                    if (PuckCache.ModelNameToAQN.ContainsKey(t.Name))
                    {
                        throw new Exception($"there is more than one ViewModel with the name:{t.Name}. ViewModel names must be unique!");
                    }
                    PuckCache.ModelNameToAQN[t.Name]  = t.AssemblyQualifiedName;
                    PuckCache.ModelNameToType[t.Name] = t;
                }

                var interfaces = ApiHelper.FindDerivedClasses(typeof(puck.core.Abstract.I_BaseModel)).Where(x => x.IsInterface).ToList();

                var interfaceNameToType = new Dictionary <string, Tuple <Type, List <FlattenedObject> > >();

                foreach (var _interface in interfaces)
                {
                    var implementingTypes = ApiHelper.FindDerivedClasses(_interface).ToList();
                    implementingTypes = implementingTypes.Where(x => typeof(BaseModel).IsAssignableFrom(x)).ToList();

                    if (implementingTypes.Count == 0)
                    {
                        continue;
                    }

                    var instance = ApiHelper.CreateInstance(implementingTypes.First());
                    try
                    {
                        ObjectDumper.SetPropertyValues(instance, setNullableFields: true);
                    }
                    catch (Exception ex)
                    {
                        PuckCache.PuckLog.Log(ex);
                        continue;
                    };

                    var dmp = ObjectDumper.Write(instance, int.MaxValue);

                    interfaceNameToType.Add(_interface.Name, new Tuple <Type, List <FlattenedObject> >(_interface, dmp));
                }

                PuckCache.InterfaceNameToType = interfaceNameToType;
            }
        }
Exemple #3
0
 //update class hierarchies/typechains which may have changed since last run
 public static void UpdateTypeChains()
 {
     using (var scope = PuckCache.ServiceProvider.CreateScope())
     {
         var repo     = scope.ServiceProvider.GetService <I_Puck_Repository>();
         var excluded = new List <Type> {
             typeof(puck.core.Entities.PuckRevision)
         };
         var currentTypes  = ApiHelper.FindDerivedClasses(typeof(puck.core.Base.BaseModel), excluded: excluded, inclusive: false);
         var meta          = repo.GetPuckMeta().Where(x => x.Name == DBNames.TypeChain).ToList();
         var typesToUpdate = new List <Type>();
         foreach (var item in meta)
         {
             //check saved type is in currentTypes
             var type = currentTypes.FirstOrDefault(x => x.Name.Equals(item.Key));
             if (type != null)
             {
                 var typeChain   = ApiHelper.TypeChain(type);
                 var dbTypeChain = item.Value;
                 //check that typechain is the same
                 //if not, add to types to update
                 if (!typeChain.Equals(dbTypeChain))
                 {
                     typesToUpdate.Add(type);
                 }
             }
         }
         var toIndex = new List <BaseModel>();
         foreach (var type in typesToUpdate)
         {
             //get revisions whose typechains have changed
             var revisions = repo.GetPuckRevision().Where(x => x.Type.Equals(type.Name));
             foreach (var revision in revisions)
             {
                 //update typechain in revision and in model which may need to be published
                 revision.TypeChain = ApiHelper.TypeChain(type);
                 var model = revision.ToBaseModel();
                 model.TypeChain = ApiHelper.TypeChain(type);
                 revision.Value  = JsonConvert.SerializeObject(model);
                 if (model.Published && revision.Current)
                 {
                     toIndex.Add(model);
                 }
             }
             repo.SaveChanges();
         }
         //publish content with updated typechains
         indexer.Index(toIndex);
         //delete typechains from previous bootstrap
         meta.ForEach(x => repo.DeletePuckMeta(x));
         repo.SaveChanges();
         //save typechains from current bootstrap
         currentTypes.ToList().ForEach(x =>
         {
             var newMeta = new PuckMeta
             {
                 Name  = DBNames.TypeChain,
                 Key   = x.Name,
                 Value = ApiHelper.TypeChain(x)
             };
             repo.AddPuckMeta(newMeta);
         });
         repo.SaveChanges();
     }
 }