protected override bool TryStore(object input, ISerializerContext context, CompositeType type, CompositeVersion typeVersion, ICompositeStorage storage) { if (base.TryStore(input, context, type, typeVersion, storage)) { if (input is AggregateRootException e) { ICompositeStorage inner = storage.Add(Name.InnerStorage); inner.Add(Name.AggregateRootKey, e.Key); inner.Add(Name.CommandKey, e.CommandKey); inner.Add(Name.SourceCommandKey, e.SourceCommandKey); } return(true); } return(false); }
/// <summary> /// Tries to store the <paramref name="input"/> to the <paramref name="context"/>. The <paramref name="input"/> is described by the <paramref name="type"/> /// and in the <paramref name="typeVersion"/>. /// </summary> /// <param name="input">The object to store.</param> /// <param name="context">The serialization context.</param> /// <param name="type">The <paramref name="input"/> composite type descriptor.</param> /// <param name="typeVersion">The version of the <paramref name="input"/>.</param> /// <param name="storage">The composite storage to store values to.</param> /// <returns><c>true</c> if the <paramref name="input"/> was serialized to the <paramref name="context"/>; <c>false</c> otherwise.</returns> protected virtual bool TryStore(object input, ISerializerContext context, CompositeType type, CompositeVersion typeVersion, ICompositeStorage storage) { storage.Add(Name.TypeName, type.Name); storage.Add(Name.Version, type.VersionProperty.Getter(input)); ICompositeStorage valueStorage = storage.Add(Name.Payload); foreach (CompositeProperty property in typeVersion.Properties) { object propertyValue = property.Getter(input); if (!TryStoreValue(valueStorage, property.Name, propertyValue)) { throw new NotSupportedValueException(type.Type, property.Name, property.Type, propertyValue); } } return(true); }
public bool TrySerialize(object input, ISerializerContext context) { CompositeType type; if (!provider.TryGet(context.InputType, out type)) { return(false); } int version = (int)type.VersionProperty.Getter(input); CompositeVersion typeVersion = GetCompositeVersion(type, version, context.InputType); ICompositeStorage storage = storageFactory.Create(); bool result = TryStore(input, context, type, typeVersion, storage); storage.Store(context.Output); return(result); }
private AdditionalApplicationModel LoadItem(ICompositeStorage storage) { string name = storage.Get <string>("Name"); string path = storage.Get <string>("Path"); string arguments = storage.Get <string>("Arguments", null); Key hotKey = storage.Get("HotKey", Key.None); IReadOnlyList <AdditionalApplicationModel> commands = LoadCollection(storage); if (commands == null) { return(new AdditionalApplicationModel(name, path, arguments, hotKey)); } else { return(new AdditionalApplicationModel(name, path, arguments, hotKey, commands)); } }
public bool TrySerialize(object input, ISerializerContext context) { Ensure.NotNull(input, "input"); Ensure.NotNull(context, "context"); ICompositeModel model = input as ICompositeModel; if (model == null) { return(false); } ICompositeStorage storage = storageFactory.Create(); model.Save(storage); storage.Store(context.Output); return(true); }
public async Task <bool> TryDeserializeAsync(Stream input, IDeserializerContext context) { Ensure.NotNull(input, "input"); Ensure.NotNull(context, "context"); ICompositeModel model = modelFactory.Invoke(context.OutputType) as ICompositeModel; if (model == null) { return(false); } ICompositeStorage storage = storageFactory.Create(); await storage.LoadAsync(input).ConfigureAwait(false); model.Load(storage); context.Output = model; return(true); }
public Task <bool> TrySerializeAsync(object input, ISerializerContext context) { CompositeType type; if (!provider.TryGet(context.InputType, out type)) { return(Task.FromResult(false)); } int version = (int)type.VersionProperty.Getter(input); CompositeVersion typeVersion = GetCompositeVersion(type, version, context.InputType); ICompositeStorage storage = storageFactory.Create(); bool result = TryStore(input, context, type, typeVersion, storage); Task task = storage.StoreAsync(context.Output); task.ConfigureAwait(false); return(Task <bool> .Factory.ContinueWhenAll(new Task[] { task }, t => result)); }
public async Task <bool> TrySerializeAsync(object input, ISerializerContext context) { Ensure.NotNull(input, "input"); Ensure.NotNull(context, "context"); ICompositeModel model = input as ICompositeModel; if (model == null) { return(false); } ICompositeStorage storage = storageFactory.Create(); model.Save(storage); await storage.StoreAsync(context.Output).ConfigureAwait(false); return(true); }
public bool TryDeserialize(Stream input, IDeserializerContext context) { Ensure.NotNull(input, "input"); Ensure.NotNull(context, "context"); ICompositeModel model = modelFactory.Invoke(context.OutputType) as ICompositeModel; if (model == null) { return(false); } ICompositeStorage storage = storageFactory.Create(); storage.Load(input); model.Load(storage); context.Output = model; return(true); }
protected override bool TryStore(object input, ISerializerContext context, CompositeType type, CompositeVersion typeVersion, ICompositeStorage storage) { if (input is IList list) { storage.Add("Count", list.Count); for (int i = 0; i < list.Count; i++) { object item = list[i]; ICompositeStorage innerStorage = storage.Add(i.ToString()); if (!base.TryStore(item, context, type, typeVersion, innerStorage)) { return(false); } } return(true); } return(base.TryStore(input, context, type, typeVersion, storage)); }
protected override bool TryStore(object input, ISerializerContext context, CompositeType type, CompositeVersion typeVersion, ICompositeStorage storage) { if (base.TryStore(input, context, type, typeVersion, storage)) { TryStore(GetOrAddPayloadStorage(storage), input); IReadOnlyKeyValueCollection metadata; if (context.TryGetEnvelopeMetadata(out metadata)) { ICompositeStorage metadataStorage = storage.Add(MetadataKey); foreach (string key in metadata.Keys) { metadataStorage.Add(key, metadata.Get <object>(key)); } } return(true); } return(false); }
public void Load(ICompositeStorage storage) { int configurationVersion = storage.Get("ConfigurationVersion", 1); FileSearchPattern = storage.Get <string>("FileSearchPattern"); IsAutoSelectApplicationVersion = storage.Get <bool>("IsAutoSelectApplicationVersion"); IsDismissedWhenLostFocus = storage.Get <bool>("IsDismissedWhenLostFocus"); IsDisplayedPathTrimmedToLastFolderName = storage.Get <bool>("IsDisplayedPathTrimmedToLastFolderName"); IsFileNameRemovedFromDisplayedPath = storage.Get <bool>("IsFileNameRemovedFromDisplayedPath"); IsFileSearchPatternSaved = storage.Get <bool>("IsFileSearchPatternSaved"); IsHiddentOnStartup = storage.Get <bool>("IsHiddentOnStartup"); IsLastUsedApplicationSavedAsPrefered = storage.Get <bool>("IsLastUsedApplicationSavedAsPrefered"); IsProjectCountEnabled = storage.Get <bool>("IsProjectCountEnabled"); IsStatisticsCounted = storage.Get <bool>("IsStatisticsCounted"); IsTrayIcon = storage.Get <bool>("IsTrayIcon"); PinnedFiles = storage.Get <IReadOnlyList <string> >("PinnedFiles"); PositionTop = storage.Get <int>("PositionTop"); PositionLeft = storage.Get <int>("PositionLeft"); PositionMode = storage.Get <PositionMode>("PositionMode"); PreferedApplicationPath = storage.Get <string>("PreferedApplicationPath"); RunKey = storage.Get <string>("RunKey"); SourceDirectoryPath = storage.Get <string>("SourceDirectoryPath"); ThemeMode = storage.Get <ThemeMode>("ThemeMode"); AdditionalApplications = new AdditionalApplicationCollection(); if (storage.TryGet("AdditionalApplications", out var additionalApplications)) { AdditionalApplications.Load(additionalApplications); } HiddenMainApplications = storage.Get <IReadOnlyList <string> >("HiddenMainApplications"); FileSearchCount = storage.Get <int>("FileSearchCount"); FileSearchMode = storage.Get <FileSearchMode>("FileSearchMode"); AutoSelectApplicationMinimalVersion = storage.Get <Version>("AutoSelectApplicationMinimalVersion"); LogLevel = storage.Get("LogLevel", LogLevel.Error); }
protected override bool TryStore(object input, ISerializerContext context, CompositeType type, CompositeVersion typeVersion, ICompositeStorage storage) { if (!base.TryStore(input, context, type, typeVersion, storage)) { return(false); } ICompositeStorage payloadStorage; GuidedObject guided = input as GuidedObject; if (guided != null && storage.TryGet(Name.Payload, out payloadStorage)) { payloadStorage.Add("Guid", guided.Guid); } return(true); }
protected override bool TryLoad(Stream input, IDeserializerContext context, CompositeType type, ICompositeStorage storage) { if (!base.TryLoad(input, context, type, storage)) { return(false); } ICompositeStorage payloadStorage; GuidedObject guided = context.Output as GuidedObject; if (guided != null && storage.TryGet(Name.Payload, out payloadStorage)) { string guid; if (payloadStorage.TryGet("Guid", out guid)) { guided.Guid = guid; } } return(true); }
protected override bool TryLoad(Stream input, IDeserializerContext context, CompositeType type, ICompositeStorage storage) { if (base.TryLoad(input, context, type, storage)) { if (storage.TryGet("AggregateRootException", out ICompositeStorage inner)) { AggregateRootExceptionDecorator decorator = new AggregateRootExceptionDecorator((AggregateRootException)context.Output); IKey key = inner.Get <IKey>(Name.AggregateRootKey); if (key != null) { decorator.SetKey(key); } key = inner.Get <IKey>(Name.CommandKey); if (key != null) { decorator.SetCommandKey(key); } key = inner.Get <IKey>(Name.SourceCommandKey); if (key != null) { decorator.SetSourceCommandKey(key); } } return(true); } return(false); }
/// <summary> /// Tries to load a object from the <paramref name="input"/> described by the <paramref name="type"/>. /// </summary> /// <param name="input">The serialized value to deserialize.</param> /// <param name="context">The deserialization context.</param> /// <param name="type">The composite type descriptor to load.</param> /// <param name="storage">The composite storage with loaded values.</param> /// <returns><c>true</c> if object was deserialized; <c>false</c> otherwise.</returns> protected virtual bool TryLoad(Stream input, IDeserializerContext context, CompositeType type, ICompositeStorage storage) { int version; if (!storage.TryGet(Name.Version, out version)) { throw new MissingVersionValueException(); } CompositeVersion typeVersion = GetCompositeVersion(type, version, context.OutputType); ICompositeStorage valueStorage; if (!storage.TryGet(Name.Payload, out valueStorage)) { throw new MissingPayloadValueException(); } List <object> values = new List <object>(); foreach (CompositeProperty property in typeVersion.Properties) { object value; if (!TryLoadValue(valueStorage, property.Name, property.Type, out value)) { throw new NotSupportedValueException(type.Type, property.Name, property.Type); } values.Add(value); } context.Output = typeVersion.Constructor.Factory(values.ToArray()); if (type.VersionProperty.Setter != null) { type.VersionProperty.Setter(context.Output, version); } return(true); }
/// <summary> /// Tries to store hte <paramref name="value"/> to the <paramref name="storage"/> with the <paramref name="key"/>. /// </summary> /// <param name="storage">The composite storage to store the <paramref name="value"/> to.</param> /// <param name="key">The key to associate the <paramref name="value"/> to.</param> /// <param name="value">The value to store.</param> /// <returns><c>true</c> if the <paramref name="value"/> was stored to the <paramref name="storage"/>; <c>false</c> otherwise.</returns> protected virtual bool TryStoreValue(ICompositeStorage storage, string key, object value) { storage.Add(key, value); return(true); }
public void Save(ICompositeStorage storage) { SaveCollection(storage, items); }
protected override bool TryLoad(Stream input, IDeserializerContext context, CompositeType type, ICompositeStorage storage) { if (base.TryLoad(input, context, type, storage)) { TryLoad(GetOrAddPayloadStorage(storage), context.Output); IKeyValueCollection metadata; ICompositeStorage metadataStorage; if (context.TryGetEnvelopeMetadata(out metadata) && storage.TryGet(MetadataKey, out metadataStorage)) { foreach (string key in metadataStorage.Keys) { metadata.Add(key, metadataStorage.Get <object>(key)); } } return(true); } return(false); }
protected abstract void TryStore(ICompositeStorage payloadStorage, object input);
protected abstract void TryLoad(ICompositeStorage payloadStorage, object output);
protected override bool TryLoad(Stream input, IDeserializerContext context, CompositeType type, ICompositeStorage storage) { if (storage.TryGet("Count", out int count)) { log.Debug($"Count '{count}'."); Type listType = typeof(List <>).MakeGenericType(type.Type); IList list = (IList)Activator.CreateInstance(listType); log.Debug($"List: '{list != null}'."); for (int i = 0; i < count; i++) { if (storage.TryGet(i.ToString(), out ICompositeStorage innerStorage) && base.TryLoad(input, context, type, innerStorage)) { log.Debug($"Add item '{context.Output != null}'."); list.Add(context.Output); } else { log.Error($"Item failed at index '{i}'."); return(false); } } context.Output = list; return(true); } log.Debug($"Count not found."); return(base.TryLoad(input, context, type, storage)); }
public void Base() { Converts.Repository .AddJsonEnumSearchHandler() .AddJsonPrimitivesSearchHandler() .AddJsonObjectSearchHandler(); ICompositeStorage storage = new JsonCompositeStorage(); storage.Add("Name", "Test.UserModel"); storage.Add("Version", 1); ICompositeStorage payloadStorage = storage.Add("Payload"); payloadStorage.Add("FirstName", "John"); payloadStorage.Add("LastName", "Doe"); payloadStorage.Add("Direction", ListSortDirection.Descending); payloadStorage.Add("IDs", new int[] { 1, 2, 3 }); payloadStorage.Add("Keys", new List <int>() { 4, 5, 6 }); using (MemoryStream stream = new MemoryStream()) { storage.StoreAsync(stream).Wait(); stream.Seek(0, SeekOrigin.Begin); string json = null; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true)) json = reader.ReadToEnd(); stream.Seek(0, SeekOrigin.Begin); storage.LoadAsync(stream).Wait(); } string value; Assert.AreEqual(true, storage.TryGet("Name", out value)); Assert.AreEqual("Test.UserModel", value); int version; Assert.AreEqual(true, storage.TryGet("Version", out version)); Assert.AreEqual(1, version); Assert.AreEqual(true, storage.TryGet("Payload", out payloadStorage)); Assert.AreEqual(true, payloadStorage.TryGet("FirstName", out value)); Assert.AreEqual("John", value); Assert.AreEqual(true, payloadStorage.TryGet("LastName", out value)); Assert.AreEqual("Doe", value); ListSortDirection direction; Assert.AreEqual(true, payloadStorage.TryGet("Direction", out direction)); Assert.AreEqual(ListSortDirection.Descending, direction); int[] ids; Assert.AreEqual(true, payloadStorage.TryGet("IDs", out ids)); Assert.IsNotNull(ids); Assert.AreEqual(1, ids[0]); Assert.AreEqual(2, ids[1]); Assert.AreEqual(3, ids[2]); List <int> keys; Assert.AreEqual(true, payloadStorage.TryGet("Keys", out keys)); Assert.IsNotNull(ids); Assert.AreEqual(4, keys[0]); Assert.AreEqual(5, keys[1]); Assert.AreEqual(6, keys[2]); }