demoproject.zip
DemoProject
Test
(ClassDiagram1).cd
bin
Debug
Test.exe
Test.vshost.exe
Test.vshost.exe.manifest
XPeriment.Persistence.dll
Properties
Settings.settings
Test.csproj.user
XPeriment.Architect
bin
Debug
BizAgi.Architect.vshost.exe
BizAgi.Architect.vshost.exe.manifest
Experiment.Architect.exe
Experiment.Architect.vshost.exe
Experiment.Architect.vshost.exe.manifest
Properties
Settings.settings
XPeriment.Management.suo
XPeriment.Persistence
(ClassDiagram).cd
bin
Debug
XPeriment.Persistence.dll
DataAcessTasks
(ClassDiagram).cd
(ClassDiagram1).cd
SqlServer
PersistentObjects
(ClassDiagram).cd
Attributes
Properties
Settings.settings
XPeriment.Persistence.csproj.user
|
using System;
using System.Collections.Generic;
using System.Text;
using XPeriment.DataAccessTasks;
using System.Data;
namespace XPeriment.Persistence.PersistentObjects
{
public class PersistentObject
{
private bool created = false;
[IgnoreMapping]
public bool Created
{
get
{
return created;
}
}
internal void CreateOnTransaction(PerformerTransaction trans, bool saveSubobjects)
{
ObjectMapping map = PersistenceCatalog.GetTypeMapping(this.GetType());
List<PersistentObject> subobjects = map.GetChildPersistentObjects(this);
if (saveSubobjects)
{
foreach (PersistentObject po in subobjects)
po.SaveOnTransaction(trans, saveSubobjects);
}
Dictionary<string, object> foraignkeyvalues = map.GetForaignKeyFalues(this);
InsertTask insert = new InsertTask(ReflectionUtility.GetTargetTable(this));
foreach (string key in foraignkeyvalues.Keys)
insert.Set(key, foraignkeyvalues[key]);
Dictionary<string, object> mappings = map.GetFieldValues(this);
insert.CurrentTransaction = trans;
foreach (string key in mappings.Keys)
insert.Set(key, mappings[key]);
insert.Execute();
map.SetPrimaryKeyValue(this, insert.ScopeIdentity);
}
public void CheckData()
{
}
internal void UpdateOnTransaction(PerformerTransaction trans, bool saveSubobjects)
{
ObjectMapping map = PersistenceCatalog.GetTypeMapping(this.GetType());
List<PersistentObject> subobjects = map.GetChildPersistentObjects(this);
if (saveSubobjects)
{
foreach (PersistentObject po in subobjects)
po.SaveOnTransaction(trans, saveSubobjects);
}
Dictionary<string, object> foraignkeyvalues = map.GetForaignKeyFalues(this);
UpdateTask update = new UpdateTask(ReflectionUtility.GetTargetTable(this));
foreach (string key in foraignkeyvalues.Keys)
update.Set(key, foraignkeyvalues[key]);
Dictionary<string, object> mappings = map.GetFieldValues(this);
update.CurrentTransaction = trans;
foreach (string key in mappings.Keys)
if (mappings[key].GetType().IsPrimitive || mappings[key] is string)
update.Set(key, mappings[key]);
update.Where(map.GetPrimaryKeysFilter(this));
update.Execute();
}
internal void DeleteOnTransaction(PerformerTransaction trans, bool deleteSubobjects)
{
ObjectMapping mapping = PersistenceCatalog.GetTypeMapping(this.GetType());
DeleteTask delete = new DeleteTask(mapping.TargetTable, mapping.GetPrimaryKeysFilter(this));
delete.CurrentTransaction = trans;
delete.Execute();
}
public void Save(bool saveSubobjects)
{
CheckData();
PerformerTransaction transaction = new QueryTask("s").Performer.CreateTransaction();
try
{
SaveOnTransaction(transaction, saveSubobjects);
transaction.Commit();
}
catch(Exception mie)
{
transaction.Rollback();
throw mie;
}
}
public void Save()
{
Save(true);
created = true;
}
public void SaveOnTransaction(PerformerTransaction trans, bool saveSubobjects)
{
if (!created)
CreateOnTransaction(trans, saveSubobjects);
else
UpdateOnTransaction(trans, saveSubobjects);
}
public void Load(object identifier)
{
Dictionary<string, object> mappings = ReflectionUtility.GetValueMappings(this);
QueryTask query = new QueryTask(ReflectionUtility.GetTargetTable(this));
query.Where(ReflectionUtility.GetPrimaryKeyField(this) + " = " + query.Performer.FormatValue(identifier));
DataTable results = query.Execute();
if (results.Rows.Count == 0)
throw new Exception("No information found to load for type '"+ this.GetType().Name +"' with key '"+identifier.ToString()+"'.");
ObjectMapping mapping = PersistenceCatalog.GetTypeMapping(this.GetType());
mapping.SetPropertyValues(this, results.Rows[0]);
created = true;
}
public void Delete()
{
Delete(false);
}
public void Delete(bool deleteSubobjects)
{
PerformerTransaction transaction = new QueryTask("").Performer.CreateTransaction();
DeleteOnTransaction(transaction, deleteSubobjects);
transaction.Commit();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of use and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.