protected virtual bool SerializeToStorageFile(Bucket bucket) { string filePath = this.GetFilePath(); try { var parentDirectory = Directory.GetParent(filePath); if (!parentDirectory.Exists) { Directory.CreateDirectory(parentDirectory.FullName); } using (var streamWriter = new StreamWriter(filePath, false)) { XmlSerializer serializer = this.GetBucketXmlSerializer(); serializer.Serialize(streamWriter, bucket); if (this.EnableDebuggingTraces) { using (var memStream = new MemoryStream()) { serializer.Serialize(memStream, bucket); this.TraceDebugStreamContent(memStream, "SaveOp"); } } } return true; } catch (Exception ex) { Log.Error("Failed to save settings to file", ex, this); return false; } }
protected virtual Bucket BuildBucketFromContainer(IContainer rootContainer) { if (rootContainer == null) { return null; } var bucket = new Bucket(); bucket.Name = rootContainer.Name; bucket.Settings = rootContainer.GetPresentStringSettingNames().Select(x => new StringStringPair(x, rootContainer.GetStringSetting(x))).ToList(); bucket.InnerBuckets = rootContainer.GetPresentSubContainerNames() .Select(x => this.BuildBucketFromContainer(rootContainer.GetNamedSubContainer(x))) .ToList(); return bucket; }
protected virtual IContainer BuildContainerFromBucket(Bucket rootBucket) { Dictionary<string, string> settings = rootBucket.Settings.ToDictionary( settingPair => settingPair.Key, settingPair => settingPair.Value); var subcontainers = rootBucket.InnerBuckets.Select(this.BuildContainerFromBucket).ToList(); var newContainer = this.ContainerFactory.GetPurelyNewObject() as IContainer; Assert.IsNotNull(newContainer, "Wrong container factory"); newContainer.InitializeFromCollections(rootBucket.Name, settings, subcontainers); return newContainer; }