public ToManyRelation(Type relation, String fieldName, Key parentId, bool isParentNew) { m_relation = relation; m_fieldName = fieldName; m_parentId = parentId; m_isParentNew = isParentNew; }
public DomainObject getDomainObjectIfLoaded(Type type, Key id) { Hashtable typeMap = (Hashtable)m_typeMaps[type]; if(typeMap == null) { return null; } DomainObject domainObject = (DomainObject)(typeMap[id]); if(domainObject != null) { return domainObject; } return null; }
internal EmployeeImpl( Key id, Timestamp timestamp, params DictionaryEntry[] parameters) { markClean(); m_id = id; m_timestamp = timestamp; FieldInfo fInfo; Type thisType = this.GetType(); Type domainObjectType = typeof(Employee); IEnumerator enumerator = parameters.GetEnumerator(); while(enumerator.MoveNext()) { if(((DictionaryEntry)enumerator.Current).Value != null) { if(((DictionaryEntry)enumerator.Current).Value.GetType() == typeof(ToOneRelation)) { if((String)((DictionaryEntry)enumerator.Current).Key == "ReportsTo") { m_ReportsTo = (ToOneRelation)((DictionaryEntry)enumerator.Current).Value; continue; } } if(((DictionaryEntry)enumerator.Current).Value.GetType() == typeof(ToManyRelation)) { if((String)((DictionaryEntry)enumerator.Current).Key == "ReportedBy") { m_ReportedBy = (ToManyRelation)((DictionaryEntry)enumerator.Current).Value; continue; } if((String)((DictionaryEntry)enumerator.Current).Key == "EmployeeTerritories") { m_EmployeeTerritories = (ToManyRelation)((DictionaryEntry)enumerator.Current).Value; continue; } } } if(this.GetType().GetProperty((String)((DictionaryEntry)enumerator.Current).Key) != null) { string variable = "m_" + (String)((DictionaryEntry)enumerator.Current).Key; if((fInfo = thisType.GetField(variable, BindingFlags.DeclaredOnly|BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance)) != null) { fInfo.SetValue(this, ((DictionaryEntry)enumerator.Current).Value); } } } }
public ToOneRelation(Type relation, String fieldName, Key targetId) { // if(m_targetId == null && targetId == null) // m_isLoaded = true; // else // { // } m_relation = relation; m_fieldName = fieldName; m_targetId = targetId; if(targetId != null) { foreach(object item in targetId) { if(item == null) { m_targetId = null; break; } } } }
public DomainObject getDomainObject(Key id, Type type) { Hashtable typeMap = (Hashtable)m_typeMaps[type]; if(typeMap == null) { typeMap = new Hashtable(); m_typeMaps[type] = typeMap; } DomainObject domainObject = (DomainObject)(typeMap[id]); if(domainObject != null) { if(domainObject.State == DomainObject.ObjectState.Deleted) { return null; } return domainObject; } //IMapper mapper = domainObject.Mapper; //domainObject = mapper.Find(id, m_unitOfWork.Connection); domainObject = Registry.Instance.getFinder(type).findById(id); return domainObject; }
public DomainObject findById(Key id) { return Registry.Instance.getMapper(typeof(Employee)).findById(id); }
public Product loadFields(SqlDataReader reader) { Timestamp m_timestamp = safeGetTimestamp(reader); Key m_key = new Key(new object[]{ safeGetValue(reader, "ProductID") }); object m_ProductName = safeGetString(reader, "ProductName"); object m_QuantityPerUnit = safeGetDouble(reader, "QuantityPerUnit"); object m_UnitPrice = safeGetDouble(reader, "UnitPrice"); object m_Category = safeGetString(reader, "Category"); Product product = (Product)UnitOfWork.Instance.IdentityMap.getDomainObjectIfLoaded(typeof(Product), m_key); if(product == null) { product = (Product)Registry.Instance.getFactory(typeof(Product)).createFromParameters( m_key, m_timestamp, new DictionaryEntry("ProductName", m_ProductName), new DictionaryEntry("QuantityPerUnit", m_QuantityPerUnit), new DictionaryEntry("UnitPrice", m_UnitPrice), new DictionaryEntry("Category", m_Category), new DictionaryEntry("OrderDetails", new ToManyRelation(typeof(OrderDetail), "ProductID", m_key, false))); UnitOfWork.Instance.IdentityMap.registerDomainObject(product); } return product; }
public Region loadFields(SqlDataReader reader) { Timestamp m_timestamp = safeGetTimestamp(reader); Key m_key = new Key(new object[]{ safeGetValue(reader, "RegionID") }); object m_Description = safeGetString(reader, "RegionDescription"); Region region = (Region)UnitOfWork.Instance.IdentityMap.getDomainObjectIfLoaded(typeof(Region), m_key); if(region == null) { region = (Region)Registry.Instance.getFactory(typeof(Region)).createFromParameters( m_key, m_timestamp, new DictionaryEntry("Description", m_Description), new DictionaryEntry("Territories", new ToManyRelation(typeof(Territory), "RegionID", m_key, false))); UnitOfWork.Instance.IdentityMap.registerDomainObject(region); } return region; }
public DomainObject createFromParameters( Key id, Timestamp timestamp, params DictionaryEntry[] parameters) { return new EmployeeImpl(id, timestamp, parameters); }
public IList ResolveToManyRelation(Key id, String fieldName) { SqlDataReader reader = null; ArrayList array = new ArrayList(); SqlConnection connection = UnitOfWork.Instance.Connection; SqlCommand command; connection.Open(); try { command = new SqlCommand( "SELECT PersonID, chTimestamp FROM Persons WHERE " + fieldName + " = @FK", connection); command.Parameters.Add("@FK", id[0]); reader = command.ExecuteReader(); ArrayList keys = new ArrayList(); Timestamp timestamp; while(reader.Read()) { object personid = safeGetValue(reader, "PersonID"); timestamp = safeGetTimestamp(reader); Key currentKey = new Key(new object[]{personid}); DomainObject domobj = UnitOfWork.Instance.IdentityMap.getDomainObjectIfLoaded( typeof(Person), currentKey); if(domobj == null) { keys.Add(currentKey); } else if(!domobj.Timestamp.Equals(timestamp)) { UnitOfWork.Instance.IdentityMap.unregisterDomainObject(domobj); keys.Add(currentKey); } else { array.Add(domobj); } } reader.Close(); if(keys.Count > 0) { int primKeysCount = 1; ArrayList primKeys = new ArrayList(); primKeys.Add("PersonID"); StringBuilder selectAdStmt = new StringBuilder(); selectAdStmt.Append("SELECT * FROM "); selectAdStmt.Append("Persons"); selectAdStmt.Append(" WHERE "); StringBuilder whereStmt; String[] andStmts = new String[keys.Count]; for(int i = 0; i < keys.Count; i++) { String[] primKeyCriterias = new String[primKeysCount]; for(int j = 0; j < primKeysCount; j++) { primKeyCriterias[j] = primKeys[j] + " = @key" + i +j; } whereStmt = new StringBuilder(); whereStmt.Append("("); whereStmt.Append(String.Join(" AND ", primKeyCriterias)); whereStmt.Append(")"); andStmts[i] = whereStmt.ToString(); } selectAdStmt.Append(String.Join(" OR ", andStmts)); command = new SqlCommand( selectAdStmt.ToString(), connection); for(int i = 0; i < keys.Count; i++) { for(int j = 0; j < primKeysCount; j++) { String keyParam = "@key" + i + j; command.Parameters.Add(keyParam, ((Key)keys[i])[j]); } } reader = command.ExecuteReader(); while(reader.Read()) { Person person = loadFields(reader); array.Add(person); } reader.Close(); } } catch(SqlException sqle) { throw new ApplicationException(sqle.Message); } catch(Exception e) { throw new ApplicationException(e.Message); } finally { connection.Close(); } return array; }
public DomainObject ResolveToOneRelation(Key id, String fieldName) { SqlDataReader reader = null; SqlConnection connection = UnitOfWork.Instance.Connection; connection.Open(); try { String selectStmt = "SELECT * FROM Persons WHERE " + fieldName + " = @FK"; SqlCommand command = new SqlCommand(selectStmt, connection); command.Parameters.Add("@FK", id[0]); reader = command.ExecuteReader(); Person person = null; if(reader.Read()) { person = loadFields(reader); } if(reader != null) { reader.Close(); } connection.Close(); return person; } catch(SqlException sqle) { throw new ApplicationException(sqle.Message); } catch(Exception e) { throw new ApplicationException(e.Message); } finally { connection.Close(); } }
public DomainObject findById(Key id) { try { SqlDataReader reader = null; SqlConnection connection = UnitOfWork.Instance.Connection; connection.Open(); string sqlStmt = "SELECT * from Persons WHERE PersonID = @PersonId"; SqlCommand command = new SqlCommand(sqlStmt, connection); command.Parameters.Add("@PersonId", id[0]); reader = command.ExecuteReader(); Person person = null; if(reader.Read()) { person = loadFields(reader); } if(reader != null) reader.Close(); connection.Close(); return person; } catch(SqlException sqle) { throw new ApplicationException(sqle.Message); } catch(Exception e) { throw new ApplicationException(e.Message); } }
public Person loadFields(SqlDataReader reader) { Timestamp m_timestamp = safeGetTimestamp(reader); Key m_key = new Key(new object[]{ safeGetValue(reader, "PersonID") }); object m_Name = safeGetString(reader, "Name"); object m_Password = safeGetString(reader, "Password"); Person person = (Person)UnitOfWork.Instance.IdentityMap.getDomainObjectIfLoaded(typeof(Person), m_key); if(person == null) { person = (Person)Registry.Instance.getFactory(typeof(Person)).createFromParameters( m_key, m_timestamp, new DictionaryEntry("Name", m_Name), new DictionaryEntry("Password", m_Password), new DictionaryEntry("Addresses", new ToManyRelation(typeof(Address), "PersonID", m_key, false)), new DictionaryEntry("Orders", new ToManyRelation(typeof(Order), "PersonID", m_key, false))); UnitOfWork.Instance.IdentityMap.registerDomainObject(person); } return person; }
public Territory loadFields(SqlDataReader reader) { Timestamp m_timestamp = safeGetTimestamp(reader); Key m_key = new Key(new object[]{ safeGetValue(reader, "TerritoryID") }); object m_TerritoryId = safeGetString(reader, "TerritoryID"); object m_TerritoryDescription = safeGetString(reader, "TerritoryDescription"); Territory territory = (Territory)UnitOfWork.Instance.IdentityMap.getDomainObjectIfLoaded(typeof(Territory), m_key); if(territory == null) { territory = (Territory)Registry.Instance.getFactory(typeof(Territory)).createFromParameters( m_key, m_timestamp, new DictionaryEntry("TerritoryId", m_TerritoryId), new DictionaryEntry("TerritoryDescription", m_TerritoryDescription), new DictionaryEntry("Region", new ToOneRelation(typeof(Region), "RegionID", new Key(new object[] { safeGetValue(reader, "RegionID")}))), new DictionaryEntry("EmployeeTerritories", new ToManyRelation(typeof(EmployeeTerritory), "TerritoryID", m_key, false))); UnitOfWork.Instance.IdentityMap.registerDomainObject(territory); } return territory; }
public DomainObject findById(Key id) { try { SqlDataReader reader = null; SqlConnection connection = UnitOfWork.Instance.Connection; connection.Open(); string sqlStmt = "SELECT * from OrderDetails WHERE OrderID = @Order AND ProductID = @Product"; SqlCommand command = new SqlCommand(sqlStmt, connection); command.Parameters.Add("@Order", id[0]); command.Parameters.Add("@Product", id[1]); reader = command.ExecuteReader(); OrderDetail orderdetail = null; if(reader.Read()) { orderdetail = loadFields(reader); } if(reader != null) reader.Close(); connection.Close(); return orderdetail; } catch(SqlException sqle) { throw new ApplicationException(sqle.Message); } catch(Exception e) { throw new ApplicationException(e.Message); } }
public Order loadFields(SqlDataReader reader) { Timestamp m_timestamp = safeGetTimestamp(reader); Key m_key = new Key(new object[]{ safeGetValue(reader, "OrderID") }); object m_OrderDate = safeGetDateTime(reader, "OrderDate"); object m_ShippedDate = safeGetDateTime(reader, "ShippedDate"); object m_OrderState = safeGetString(reader, "OrderState"); Order order = (Order)UnitOfWork.Instance.IdentityMap.getDomainObjectIfLoaded(typeof(Order), m_key); if(order == null) { order = (Order)Registry.Instance.getFactory(typeof(Order)).createFromParameters( m_key, m_timestamp, new DictionaryEntry("Person", new ToOneRelation(typeof(Person), "PersonID", new Key(new object[] { safeGetValue(reader, "PersonID")}))), new DictionaryEntry("OrderDate", m_OrderDate), new DictionaryEntry("ShippedDate", m_ShippedDate), new DictionaryEntry("OrderState", m_OrderState), new DictionaryEntry("OrderDetails", new ToManyRelation(typeof(OrderDetail), "OrderID", m_key, false))); UnitOfWork.Instance.IdentityMap.registerDomainObject(order); } return order; }
public Employee loadFields(SqlDataReader reader) { Timestamp m_timestamp = safeGetTimestamp(reader); Key m_key = new Key(new object[]{ safeGetValue(reader, "EmployeeID") }); object m_LastName = safeGetString(reader, "LastName"); object m_FirstName = safeGetString(reader, "FirstName"); object m_Title = safeGetString(reader, "Title"); object m_BirthDate = safeGetDateTime(reader, "BirthDate"); object m_City = safeGetString(reader, "City"); object m_HireDate = safeGetDateTime(reader, "HireDate"); Employee employee = (Employee)UnitOfWork.Instance.IdentityMap.getDomainObjectIfLoaded(typeof(Employee), m_key); if(employee == null) { employee = (Employee)Registry.Instance.getFactory(typeof(Employee)).createFromParameters( m_key, m_timestamp, new DictionaryEntry("LastName", m_LastName), new DictionaryEntry("FirstName", m_FirstName), new DictionaryEntry("Title", m_Title), new DictionaryEntry("BirthDate", m_BirthDate), new DictionaryEntry("City", m_City), new DictionaryEntry("HireDate", m_HireDate), new DictionaryEntry("ReportsTo", new ToOneRelation(typeof(Employee), "ReportsTo", new Key(new object[] { safeGetValue(reader, "ReportsTo")}))), new DictionaryEntry("ReportedBy", new ToManyRelation(typeof(Employee), "ReportsTo", m_key, false)), new DictionaryEntry("EmployeeTerritories", new ToManyRelation(typeof(EmployeeTerritory), "EmployeeID", m_key, false))); UnitOfWork.Instance.IdentityMap.registerDomainObject(employee); } return employee; }
public DomainObject findById(Key id) { return Registry.Instance.getMapper(typeof(OrderDetail)).findById(id); }
public Address loadFields(SqlDataReader reader) { Timestamp m_timestamp = safeGetTimestamp(reader); Key m_key = new Key(new object[]{ safeGetValue(reader, "AddressID") }); object m_City = safeGetString(reader, "City"); object m_Street = safeGetString(reader, "Street"); object m_PostalCode = safeGetString(reader, "PostalCode"); object m_Phone = safeGetString(reader, "Phone"); object m_Email = safeGetString(reader, "Email"); Address address = (Address)UnitOfWork.Instance.IdentityMap.getDomainObjectIfLoaded(typeof(Address), m_key); if(address == null) { address = (Address)Registry.Instance.getFactory(typeof(Address)).createFromParameters( m_key, m_timestamp, new DictionaryEntry("Person", new ToOneRelation(typeof(Person), "PersonID", new Key(new object[] { safeGetValue(reader, "PersonID")}))), new DictionaryEntry("City", m_City), new DictionaryEntry("Street", m_Street), new DictionaryEntry("PostalCode", m_PostalCode), new DictionaryEntry("Phone", m_Phone), new DictionaryEntry("Email", m_Email)); UnitOfWork.Instance.IdentityMap.registerDomainObject(address); } return address; }
public OrderDetail loadFields(SqlDataReader reader) { Timestamp m_timestamp = safeGetTimestamp(reader); Key m_key = new Key(new object[]{ safeGetValue(reader, "OrderID"), safeGetValue(reader, "ProductID") }); object m_UnitPrice = safeGetDouble(reader, "UnitPrice"); object m_Quantity = safeGetInt32(reader, "Quantity"); OrderDetail orderdetail = (OrderDetail)UnitOfWork.Instance.IdentityMap.getDomainObjectIfLoaded(typeof(OrderDetail), m_key); if(orderdetail == null) { orderdetail = (OrderDetail)Registry.Instance.getFactory(typeof(OrderDetail)).createFromParameters( m_key, m_timestamp, new DictionaryEntry("Order", new ToOneRelation(typeof(Order), "OrderID", new Key(new object[] { safeGetValue(reader, "OrderID")}))), new DictionaryEntry("Product", new ToOneRelation(typeof(Product), "ProductID", new Key(new object[] { safeGetValue(reader, "ProductID")}))), new DictionaryEntry("UnitPrice", m_UnitPrice), new DictionaryEntry("Quantity", m_Quantity)); UnitOfWork.Instance.IdentityMap.registerDomainObject(orderdetail); } return orderdetail; }