private bool SaveObject(IPersistable obj) { string filename = GetObjectFilename(obj); FileInfo file = _scope.Context.GetResource(filename).File; string path = file.DirectoryName; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } lock (this.SyncRoot) { MemoryStream ms = new MemoryStream(); AMFWriter writer = new AMFWriter(ms); writer.UseLegacyCollection = false; writer.WriteString(obj.GetType().FullName); //amfSerializer.WriteData(ObjectEncoding.AMF0, obj); obj.Serialize(writer); writer.Flush(); byte[] buffer = ms.ToArray(); ms.Close(); using (FileStream fs = new FileStream(file.FullName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) { fs.Write(buffer, 0, buffer.Length); fs.Close(); } } return(true); }
protected string GetObjectId(IPersistable obj) { string name = obj.GetType().Name; if (!obj.Path.StartsWith("/")) { name = name + "/"; } name = name + obj.Path; if (!name.EndsWith("/")) { name = name + "/"; } string str2 = obj.Name; if (str2 == null) { str2 = "__null__"; } if (str2.StartsWith("/")) { str2 = str2.Substring(1); } return(name + str2); }
/// <summary> /// Saves the specified instance the the options file(s). /// </summary> public void SaveInstance(IPersistable persistableObject) { Type t = persistableObject.GetType(); if (!typeCache.ContainsKey(t)) { RegisterInstance(persistableObject); } TypeReflectionCache cache = typeCache[t]; foreach (TypeReflectionCacheItem item in cache.Properties) { bool created; XmlNode valueNode = OptionsMerger.LocateOrCreateNode(item, persistableObject, out created); object newValue = item.PropertyInfo.GetValue(persistableObject, null); OptionsMerger.SetNodeValue(valueNode, newValue.ToString(), item.PersistanceInformation.Encrypted); } // Don't save the files to disk if we are currently in batch update mode. if (!batchInProgress) { SaveFiles(); } }
protected string GetObjectId(IPersistable obj) { // The format of the object id is <type>/<path>/<objectName> string result = obj.GetType().Name; if (!obj.Path.StartsWith("/")) { result += "/"; } result += obj.Path; if (!result.EndsWith("/")) { result += "/"; } string name = obj.Name; if (name == null) { name = PersistenceNoName; } if (name.StartsWith("/")) { // "result" already ends with a slash name = name.Substring(1); } return(result + name); }
public void Serialize(StateMap sd, SerializationHelper helper) { sd.Set("Type", _persistable.GetType().SimpleQualifiedName()); _persistable.Serialize(sd, helper); //stateToSerialize["PersistableStateMap"] = _persistableStateMap; }
public static bool Insert <T>(this IPersistable <T> obj, bool recursive) { var inserts = new List <Func <bool> >(); if (recursive) { var mis = obj.GetType().GetMembers().Where(m => m.CanPreserve() && !m.GetDeclarationType().IsSimpleType()).Select(m => obj.Get(m.Name)); mis.ForEach(m => { var insert = GetInsert(m); if (insert != null) { inserts.Add(insert); } }); if (inserts.Count > 0) { using (var tran = new TransactionScope()) { inserts.ForEach(insert => insert()); DataContext.GetEntry <T>().Insert((T)obj); tran.Complete(); return(true); } } } return(DataContext.GetEntry <T>().Insert((T)obj) > 0); }
private bool SaveObject(IPersistable obj) { string objectFilename = this.GetObjectFilename(obj); FileInfo file = base._scope.Context.GetResource(objectFilename).File; string directoryName = file.DirectoryName; if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } lock (base.SyncRoot) { MemoryStream stream = new MemoryStream(); AMFWriter writer = new AMFWriter(stream) { UseLegacyCollection = false }; writer.WriteString(obj.GetType().FullName); obj.Serialize(writer); writer.Flush(); byte[] buffer = stream.ToArray(); stream.Close(); using (FileStream stream2 = new FileStream(file.FullName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) { stream2.Write(buffer, 0, buffer.Length); stream2.Close(); } } return(true); }
public bool UpdateEntity(IPersistable entity) { if (entity.GetType() == typeof(Rentee)) { return(handler.UpdateRentee(entity as Rentee)); } else if (entity.GetType() == typeof(Order)) { return(handler.UpdateOrder(entity as Order)); } else if (entity.GetType() == typeof(Bike)) { return(handler.UpdateBike(entity as Bike)); } else { return(false); } }
/// <summary> /// Loads the specified instance from the options file(s). /// </summary> public void LoadInstance(IPersistable persistableObject) { Type t = persistableObject.GetType(); if (!typeCache.ContainsKey(t)) { RegisterInstance(persistableObject); } TypeReflectionCache cache = typeCache[t]; foreach (TypeReflectionCacheItem item in cache.Properties) { bool created; XmlNode valueNode = LocateOrCreateNode(item, persistableObject, out created); string value = valueNode.InnerText; if (item.PersistanceInformation.Encrypted) { value = OptionsMerger.DecryptString(value); } Type propertyType = item.PropertyInfo.PropertyType; if (propertyType == typeof(string)) { item.PropertyInfo.SetValue(persistableObject, value, null); } else if (propertyType == typeof(Single)) { item.PropertyInfo.SetValue(persistableObject, Convert.ToSingle(value), null); } else if (propertyType == typeof(int)) { item.PropertyInfo.SetValue(persistableObject, Convert.ToInt32(value), null); } else if (propertyType == typeof(bool)) { item.PropertyInfo.SetValue(persistableObject, Convert.ToBoolean(value), null); } else { throw new Exception("An unknown type was encountered while loading named instance '" + persistableObject.InstanceName + "' - " + propertyType); } if (created) { SaveInstance(persistableObject); } } }
/// <summary> /// Create a TypeReflectionCache object from the specified IPersistable instance. /// </summary> public TypeReflectionCache(IPersistable instance) { type = instance.GetType(); EventInfo[] events = type.GetEvents(); foreach (PropertyInfo pi in type.GetProperties()) { List <PropertyInfo> cachedProperties = new List <PropertyInfo>(); object[] attributes = pi.GetCustomAttributes(typeof(PersistableOption), true); if (attributes.Length > 0) { // This is a persistable option. PersistableOption optAtt = attributes[0] as PersistableOption; // Add this property to the cache list. TypeReflectionCacheItem item = new TypeReflectionCacheItem(); item.PropertyInfo = pi; item.PersistanceInformation = optAtt; // Link up any events tagged with with UpdateHandler attribute. foreach (EventInfo info in events) { if (info.EventHandlerType == typeof(EventHandler)) { // Check to see if the event is tagged as an OptionUpdateNotifier. object[] eventAttributes = info.GetCustomAttributes(typeof(OptionUpdateNotifier), true); if (eventAttributes.Length > 0) { // If has the proper attribute - if the name matches, we have a linked event. OptionUpdateNotifier updateAtt = eventAttributes[0] as OptionUpdateNotifier; if (updateAtt.LinkedOptionName == optAtt.Name) { // A linked event was found. item.EventInfo = info; } } } } // Add the cache item to the properties list. properties.Add(item); } } }
public void DeleteDependencies(IPersistable obj) { Type objType = obj.GetType(); foreach (var dependency in obj.DbDependencies) { if (!dependency.AutoDelete) { continue; } PropertyInfo propInfo = objType.GetProperty(dependency.Property); var persistable = propInfo.GetValue(obj, null) as IPersistable; if (persistable != null) { Delete(persistable); } } }
protected string GetObjectId(IPersistable obj) { // The format of the object id is <type>/<path>/<objectName> string result = obj.GetType().Name; if (!obj.Path.StartsWith("/")) result += "/"; result += obj.Path; if (!result.EndsWith("/")) result += "/"; string name = obj.Name; if (name == null) name = PersistenceNoName; if (name.StartsWith("/")) { // "result" already ends with a slash name = name.Substring(1); } return result + name; }
/// <summary> /// Registers an instance with the SettingsManager, linking up any auto-update events. /// </summary> protected void RegisterInstance(IPersistable instance) { Type t = instance.GetType(); // Retreive the type's cache, or create one if it does not already exist. if (!typeCache.ContainsKey(t)) { typeCache.Add(t, new TypeReflectionCache(instance)); } TypeReflectionCache cache = typeCache[t]; // Hook up any existing auto-update event handlers for this instance. foreach (TypeReflectionCacheItem item in cache.Properties) { if (item.EventInfo != null) { item.EventInfo.AddEventHandler(instance, new EventHandler(UpdateHandler)); } } }
public void Write(IPersistable item) { Write(item.GetType().FullName); item.Save(this); }
private bool SaveObject(IPersistable obj) { string filename = GetObjectFilename(obj); FileInfo file = _scope.Context.GetResource(filename).File; string path = file.DirectoryName; if (!Directory.Exists(path)) Directory.CreateDirectory(path); lock (this.SyncRoot) { MemoryStream ms = new MemoryStream(); AMFWriter writer = new AMFWriter(ms); writer.UseLegacyCollection = false; writer.WriteString(obj.GetType().FullName); //amfSerializer.WriteData(ObjectEncoding.AMF0, obj); obj.Serialize(writer); writer.Flush(); byte[] buffer = ms.ToArray(); ms.Close(); using (FileStream fs = new FileStream(file.FullName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) { fs.Write(buffer, 0, buffer.Length); fs.Close(); } } return true; }