コード例 #1
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.DeleteMeta(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.AddMeta(newMeta);
         });
         repo.SaveChanges();
     }
 }