/// <summary> /// Deletes the specified object from the database /// </summary> /// <typeparam name="ObjectType">Object type</typeparam> /// <param name="Object">Object to delete</param> public virtual void Delete <ObjectType>(ObjectType Object) where ObjectType : class, new() { foreach (IDatabase Database in Mappings.Keys.Where(x => x.Writable).OrderBy(x => x.Order)) { IMapping Mapping = Mappings[Database].FirstOrDefault(x => x.ObjectType == typeof(ObjectType)); if (Mapping != null) { using (MicroORM ORMObject = new MicroORM(Database.Name)) { System.Collections.Generic.List <Command> JoinCommands = new System.Collections.Generic.List <Command>(); foreach (IProperty Property in Mapping.Properties) { if (!Property.Cascade && Mapping.ObjectType == Property.Type) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).JoinsDelete(Object, ORMObject)); } if (Property.Cascade) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).CascadeJoinsDelete(Object, ORMObject)); } } foreach (Command JoinCommand in JoinCommands) { ORMObject.ChangeCommand(JoinCommand); ORMObject.ExecuteNonQuery(); } foreach (IProperty Property in Mapping.Properties) { if (Property.Cascade) { ((IProperty <ObjectType>)Property).CascadeDelete(Object, ORMObject); } } ORMObject.Map <ObjectType>().Delete(Object); } } } }
/// <summary> /// Saves an object to the database /// </summary> /// <typeparam name="ObjectType">Object type</typeparam> /// <typeparam name="PrimaryKeyType">Primary key type</typeparam> /// <param name="Object">Object to save</param> /// <param name="Parameters">Extra parameters used in saving the object</param> public virtual void Save <ObjectType, PrimaryKeyType>(ObjectType Object, params IParameter[] Parameters) where ObjectType : class, new() { foreach (IDatabase Database in Mappings.Keys.Where(x => x.Writable).OrderBy(x => x.Order)) { IMapping Mapping = Mappings[Database].FirstOrDefault(x => x.ObjectType == typeof(ObjectType)); if (Mapping != null) { using (MicroORM ORMObject = new MicroORM(Database.Name)) { foreach (IProperty Property in Mapping.Properties) { if (Property.Cascade) { ((IProperty <ObjectType>)Property).CascadeSave(Object, ORMObject); } } System.Collections.Generic.List <IParameter> Params = Parameters.ToList(); foreach (IProperty Property in Mapping.Properties) { IParameter Parameter = ((IProperty <ObjectType>)Property).GetAsParameter(Object); if (Parameter != null) { Params.Add(Parameter); } } ORMObject.Map <ObjectType>().Save <PrimaryKeyType>(Object, Params.ToArray()); System.Collections.Generic.List <Command> JoinCommands = new System.Collections.Generic.List <Command>(); foreach (IProperty Property in Mapping.Properties) { if (!Property.Cascade && (Property is IManyToMany || Property is IManyToOne || Property is IIEnumerableManyToOne || Property is IListManyToMany || Property is IListManyToOne)) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).JoinsDelete(Object, ORMObject)); } if (Property.Cascade) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).CascadeJoinsDelete(Object, ORMObject)); } } foreach (Command JoinCommand in JoinCommands) { ORMObject.ChangeCommand(JoinCommand); ORMObject.ExecuteNonQuery(); } JoinCommands = new System.Collections.Generic.List <Command>(); foreach (IProperty Property in Mapping.Properties) { if (!Property.Cascade && (Property is IManyToMany || Property is IManyToOne || Property is IIEnumerableManyToOne || Property is IListManyToMany || Property is IListManyToOne)) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).JoinsSave(Object, ORMObject)); } if (Property.Cascade) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).CascadeJoinsSave(Object, ORMObject)); } } foreach (Command JoinCommand in JoinCommands) { ORMObject.ChangeCommand(JoinCommand); ORMObject.ExecuteNonQuery(); } } } } }