diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2019-01-05 22:35:03 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2019-01-05 22:35:03 +0200 |
| commit | a7137c1f053ba48c3ce22bee483fe7f716cc90cb (patch) | |
| tree | 743a606cb586a2b59585ea205e10551587846f06 /Software/Visual_Studio/Tango.BL/ObservableEntity.cs | |
| parent | 97ae338e0413613ee5a0d135f8226ee42ba4aa06 (diff) | |
| download | Tango-a7137c1f053ba48c3ce22bee483fe7f716cc90cb.tar.gz Tango-a7137c1f053ba48c3ce22bee483fe7f716cc90cb.zip | |
Working on entities serialization.
Diffstat (limited to 'Software/Visual_Studio/Tango.BL/ObservableEntity.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.BL/ObservableEntity.cs | 81 |
1 files changed, 50 insertions, 31 deletions
diff --git a/Software/Visual_Studio/Tango.BL/ObservableEntity.cs b/Software/Visual_Studio/Tango.BL/ObservableEntity.cs index ab451eb1e..e98e48422 100644 --- a/Software/Visual_Studio/Tango.BL/ObservableEntity.cs +++ b/Software/Visual_Studio/Tango.BL/ObservableEntity.cs @@ -380,68 +380,87 @@ Maybe you have deleted an entity that was no yet inserted into database?", LogCa return GetDbContextFromEntity(this); } - public virtual List<string> GetIgnoreProperties(EntitySerializationFlags flags) + public virtual EntitySerializationStrategy GetDefaultSerializationStrategy(EntitySerializationFlags flags) { - var ignored = new List<string>() + EntitySerializationStrategy st = new EntitySerializationStrategy(); + + st.Ignore(() => this.HasErrors); + st.Ignore(() => this.Parameters); + st.Ignore(() => this.DesignMode); + st.Ignore(() => this.LogManager); + st.Ignore(() => this.ObjectType); + st.Ignore(() => this.TemporaryManager); + st.Ignore(() => this.ValidateOnPropertyChanged); + st.Ignore(() => this.LastUpdated); + st.Ignore(() => this.ValidationErrors); + st.Ignore(() => this.ID); + + st.Ignore(this.GetType().GetField("_entityWrapper")); + + if (flags.HasFlag(EntitySerializationFlags.IgnoreGuids)) { - nameof(this.HasErrors), - nameof(this.Parameters), - nameof(this.DesignMode), - nameof(this.LogManager), - nameof(this.ObjectType), - nameof(this.TemporaryManager), - nameof(this.ValidateOnPropertyChanged), - nameof(this.ValidationErrors), - nameof(this.LastUpdated), - nameof(this.ID), - "_entityWrapper", - }; + st.Ignore(() => this.Guid); + } - if (flags.HasFlag(EntitySerializationFlags.SkipGuids)) + if (flags.HasFlag(EntitySerializationFlags.IgnoreReferenceTypes)) { - ignored.Add(nameof(this.Guid)); + var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => !x.PropertyType.IsPrimitive && !x.PropertyType.IsGenericType && x.PropertyType != typeof(byte[]) && x.PropertyType != typeof(String) && x.PropertyType != typeof(DateTime)).ToList(); + + foreach (var prop in props) + { + st.Ignore(prop); + } } - if (flags.HasFlag(EntitySerializationFlags.SkipReferenceTypes)) + if (flags.HasFlag(EntitySerializationFlags.IgnoreCollections)) { - ignored.AddRange( - typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) - .Where(x => !x.PropertyType.IsPrimitive && x.PropertyType != typeof(String) && x.PropertyType != typeof(DateTime)) - .Select(x => x.Name)); + var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsGenericType).ToList(); + + foreach (var prop in props) + { + st.Ignore(prop); + } } - if (flags.HasFlag(EntitySerializationFlags.SkipCollections)) + foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod == null)) { - ignored.AddRange( - typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) - .Where(x => x.PropertyType.IsGenericType) - .Select(x => x.Name)); + st.Ignore(prop); } - return ignored; + return st; } - public static T FromJson(String json, EntitySerializationFlags flags = EntitySerializationFlags.Default) + public static T FromJson(String json, EntitySerializationStrategy serializationStrategy, EntitySerializationFlags flags) { var settings = new JsonSerializerSettings() { - ContractResolver = new SerializableEntityContractResolver(flags), + ContractResolver = new SerializableEntityContractResolver(serializationStrategy, flags), }; settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); return JsonConvert.DeserializeObject<T>(json, settings); } - public String ToJson(EntitySerializationFlags flags = EntitySerializationFlags.Default) + public String ToJson(EntitySerializationStrategy serializationStrategy, EntitySerializationFlags flags) { var settings = new JsonSerializerSettings() { - ContractResolver = new SerializableEntityContractResolver(flags), + ContractResolver = new SerializableEntityContractResolver(serializationStrategy, flags), }; settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); String json = JsonConvert.SerializeObject(this, Formatting.Indented, settings); return json; } + public static T FromJson(String json) + { + return FromJson(json, new EntitySerializationStrategy(), EntitySerializationFlags.IgnoreReferenceTypes); + } + + public String ToJson() + { + return ToJson(new EntitySerializationStrategy(), EntitySerializationFlags.IgnoreReferenceTypes); + } + #endregion #region Private Methods |
