private object ReadObject(Type type, XElement element) { if (type == null) { throw new ArgumentNullException("type"); } element = ApplyMessageDeserializationBehaviorIfNecessary(type, element); if (HaveCustomValueConvertor(type)) { var convertorType = reflection.GetGenericTypeOf(typeof(IValueConvertor <>), type); var convertor = serviceLocator.Resolve(convertorType); return(reflection.InvokeFromElement(convertor, element)); } if (HaveCustomSerializer(type)) { var customSerializer = customElementSerializers.First(s => s.CanSerialize(type)); return(customSerializer.FromElement(type, element)); } if (type == typeof(byte[])) { return(Convert.FromBase64String(element.Value)); } if (CanParseFromString(type)) { return(FromString(type, element.Value)); } if (ShouldTreatAsDictionary(type)) { return(ReadDictionary(type, element)); } if (typeof(IEnumerable).IsAssignableFrom(type)) { return(ReadList(type, element)); } object instance = reflection.CreateInstance(type); foreach (var prop in element.Elements()) { var property = prop; reflection.Set(instance, prop.Name.LocalName, typeFromProperty => { var propType = reflection.GetTypeFromXmlNamespace(property.Name.NamespaceName); return(ReadObject(propType ?? typeFromProperty, property)); }); } return(instance); }
public TSaga Get(Guid id) { byte[] val = null; dictionary.Read(reader => reader.TryGetValue(id, out val)); if (val == null) { return(null); } using (var ms = new MemoryStream(val)) { var saga = serviceLocator.Resolve <TSaga>(); saga.Id = id; var state = messageSerializer.Deserialize(ms)[0]; reflection.Set(saga, "State", type => state); return(saga); } }
public TSaga Get(Guid id) { var state = session.Connection.Query <byte[]>("select State from Saga where Id = @Id", new { Id = id }).SingleOrDefault(); if (state == null) { return(null); } using (var ms = new MemoryStream(state)) { var saga = serviceLocator.Resolve <TSaga>(); saga.Id = id; reflection.Set(saga, "State", type => new BinaryFormatter().Deserialize(ms)); return(saga); } }