public bool RegisterRemoved(ORMFramework.DomainObject obj) { if (obj == null || obj.IsGhost()) { return(false); } object key = obj.getKey(); //1. Neu la obj moi, thi loai bo khoi danh sach them moi, ko can quan tam if (newObjects.ContainsKey(key)) { newObjects.Remove(key); return(true); } //2. Neu da co trong dsach loai bo roi thi ko can them nua if (removedObjects.ContainsKey(key)) { return(false); } //3. Neu can loai bo, kiem tra co thuoc danh sach dirty hay ko, neu co thi loai luon if (dirtyObjects.ContainsKey(key)) { dirtyObjects.Remove(key); } //4. Loai bo khoi danh sach cac thuoc tinh hien tai if (identityMap.ContainsKey(key)) { identityMap.Remove(key); } //5. Them vao danh sach loai bo removedObjects.Add(key, obj); return(true); }
public bool RegisterFromDatabase(ORMFramework.DomainObject obj) { if (obj == null) { return(false); } identityMap.Add(obj.getKey(), obj); return(true); }
public bool RegisterNew(ORMFramework.DomainObject obj) { if (obj == null) { return(false); } object key = obj.getKey(); if (identityMap.ContainsKey(key)) { return(false); } newObjects.Add(key, obj); return(true); }
public bool RegisterDirty(ORMFramework.DomainObject obj) { if (obj == null) { return(false); } object key = obj.getKey(); // Khong phai la object moi duoc tao, chua duoc them lan nao, chua bi loai bo if (removedObjects.ContainsKey(key) || dirtyObjects.ContainsKey(key) || newObjects.ContainsKey(key)) { return(false); } dirtyObjects.Add(key, obj); return(true); }
public void DoLoadLine(ORMFramework.DomainObject obj) { // Neu da co san thi khong can load lai lan nua if (unitOfWork.IsExisted(obj.getKey())) { return; } // Tim trong database Dictionary <string, object> sqlParams = new Dictionary <string, object>(); sqlParams.Add(GetPrimaryKeyName(), obj.getKey()); string sqlQuery = _sqlAdapter.QueryString(GetDataSQL(), tableName, sqlParams); _sqlDriver.ExecuteReader(sqlQuery, sqlParams); bool foundInDB = false; while (_sqlDriver.Read()) { int i = 0; object[] reader = _sqlDriver.GetReaderValue(); foreach (string fieldName in columns.Keys) { PropertyInfo propertyInfo = obj.GetType().GetProperty(fieldName); propertyInfo.SetValue(obj, Convert.ChangeType(reader[i++], propertyInfo.PropertyType)); } foundInDB = true; } if (foundInDB) //Neu tim thay trong DB, them vao DS nhung object da co { unitOfWork.RegisterFromDatabase(obj); } else //Neu khong co trong DB, object moi, them vao DS nhung object moi { unitOfWork.RegisterNew(obj); } // HasOne if (hasOne.Count > 0) { foreach (KeyValuePair <string, string> tmp in hasOne) { Session session = Session.getCurSession(); MethodInfo method = session.GetType().GetMethod("Table").MakeGenericMethod(obj.GetType().GetProperty(tmp.Key).PropertyType); var table = method.Invoke(session, null); method = table.GetType().GetMethod("GetPrimaryKeyName"); var priCol = method.Invoke(table, null); // get foreignKey string fieldName = this.GetForeignKeyFieldName(tmp.Value); object fieldValue = obj.GetType().GetProperty(fieldName).GetValue(obj); method = table.GetType().GetMethod("FindOneHaveValue"); var list = method.Invoke(table, new object[] { priCol, fieldValue }); PropertyInfo propertyInfo = obj.GetType().GetProperty(tmp.Key); propertyInfo.SetValue(obj, Convert.ChangeType(list, propertyInfo.PropertyType)); } } // has many if (hasMany.Count > 0) { foreach (KeyValuePair <string, string> tmp in hasMany) { Session session = Session.getCurSession(); MethodInfo method = session.GetType().GetMethod("Table").MakeGenericMethod(obj.GetType().GetProperty(tmp.Key).PropertyType.GetGenericArguments()[0]); var table = method.Invoke(session, null); method = table.GetType().GetMethod("GetRefColumnName"); var refCol = method.Invoke(table, new object[] { tableName }); method = table.GetType().GetMethod("FindAllHaveValue"); var list = method.Invoke(table, new object[] { refCol, obj.getKey() }); PropertyInfo propertyInfo = obj.GetType().GetProperty(tmp.Key); propertyInfo.SetValue(obj, Convert.ChangeType(list, propertyInfo.PropertyType)); } } }