/// <summary> /// Reloads a businessObject from the datasource using the id of the object. /// A dirty object will not be refreshed from the database and the appropriate error will be raised. /// Cancel all edits before refreshing the object or save before refreshing. /// </summary> /// <exception cref="HabaneroDeveloperException">Exception thrown if the object is dirty and refresh is called.</exception> /// <param name="businessObject">The businessObject to refresh</param> public IBusinessObject Refresh(IBusinessObject businessObject) { if (_businessObjectLoaders.ContainsKey(businessObject.GetType())) { return(_businessObjectLoaders[businessObject.GetType()].Refresh(businessObject)); } return(_defaultBusinessObjectLoader.Refresh(businessObject)); }
///<summary> /// Add an object of type business object to the transaction. /// The DBTransactionCommiter wraps this Business Object in the /// appropriate Transactional Business Object ///</summary> ///<param name="businessObject"></param> public void AddBusinessObject(IBusinessObject businessObject) { if (_myDataAccessor == null) { _myDataAccessor = GetDataAccessorForType(businessObject.GetType()); _transactionCommitter = _myDataAccessor.CreateTransactionCommitter(); } else { IDataAccessor dataAccessorToUseForType = GetDataAccessorForType(businessObject.GetType()); if (dataAccessorToUseForType != _myDataAccessor) { throw new HabaneroDeveloperException("A problem occurred while trying to save, please see log for details", string.Format("A BusinessObject of type {0} was added to a TransactionCommitterMultiSource which has been set up with a different source to this type.", businessObject.GetType().FullName)); } } _transactionCommitter.AddBusinessObject(businessObject); }
public BOTester(IBusinessObject businessObject) { if (businessObject == null) { throw new ArgumentNullException("businessObject"); } _boTestFactory = new BOTestFactory(businessObject.GetType()); BusinessObject = businessObject; }
private bool DoesVirtualPropertyHaveSetter() { string virtualPropName = PropertyName.Substring(1, PropertyName.Length - 2); var propertyInfo = ReflectionUtilities.GetPropertyInfo(_businessObject.GetType(), virtualPropName); bool virtualPropertySetExists = propertyInfo != null && propertyInfo.CanWrite; return(virtualPropertySetExists); }
private static IEnumerable <IBusinessObjectRule> GetBusinessObjectRules(IBusinessObject bo) { if (bo == null) { return(null); } var privateMethodInfo = ReflectionUtilities.GetPrivateMethodInfo(bo.GetType(), "GetBusinessObjectRules"); return(privateMethodInfo.Invoke(bo, new object[0]) as IList <IBusinessObjectRule>); }
private static List <IBusinessObject> GetChildren(IBusinessObject businessObject) { var children = businessObject.GetType() .GetProperties() .Where(p => p.GetCustomAttribute(typeof(ChildAttribute)) != null) .Select(p => p.GetValue(businessObject)) .OfType <IBusinessObject>() .ToList(); return(children); }
public void UseBusinessObjectClassService() { IBusinessObjectClassService mockService = _mockRepository.StrictMock <IBusinessObjectClassService>(); IBusinessObjectClass expectedClass = _mockRepository.Stub <IBusinessObjectClass>(); IBusinessObject businessObjectFromOtherBusinessObjectProvider = _mockRepository.Stub <IBusinessObject>(); Type typeFromOtherBusinessObjectProvider = businessObjectFromOtherBusinessObjectProvider.GetType(); IBusinessObjectReferenceProperty property = CreateProperty("Scalar", typeFromOtherBusinessObjectProvider); Expect.Call(mockService.GetBusinessObjectClass(typeFromOtherBusinessObjectProvider)).Return(expectedClass); _mockRepository.ReplayAll(); _bindableObjectProvider.AddService(typeof(IBusinessObjectClassService), mockService); IBusinessObjectClass actualClass = property.ReferenceClass; _mockRepository.VerifyAll(); Assert.That(actualClass, Is.SameAs(expectedClass)); }
public CustomObjectDataSource(IEnumerable <IBusinessObject> businessObjects) : base() { this.businessObjects = businessObjects; IBusinessObject b = businessObjects.First(); businessObjectType = b.GetType(); TypeName = "Caisis.BOL.ObjectDataSourceAdapter`1[[" + businessObjectType.FullName + ",Caisis.BOL]],Caisis.BOL"; ObjectCreating += new ObjectDataSourceObjectEventHandler(CustomObjectDataSource_ObjectCreating); Inserting += new ObjectDataSourceMethodEventHandler(CustomObjectDataSource_Inserting); Updating += new ObjectDataSourceMethodEventHandler(CustomObjectDataSource_Updating); Updated += new ObjectDataSourceStatusEventHandler(CustomObjectDataSource_Updated); Deleting += new ObjectDataSourceMethodEventHandler(CustomObjectDataSource_Deleting); }
// ReSharper restore RedundantAssignment private IBusinessObject GetLoadedBusinessObject (IBusinessObject bo, IDataRecord dataReader, ISelectQuery selectQuery, out bool objectUpdatedInLoading) { objectUpdatedInLoading = false; PopulateBOFromReader(bo, dataReader, selectQuery); IPrimaryKey key = bo.ID; IBusinessObject boFromObjectManager = GetObjectFromObjectManager(key, bo.ClassDef.ClassType); var boManager = BORegistry.BusinessObjectManager; if (boFromObjectManager == null) { objectUpdatedInLoading = true; boManager.Add(bo); return(bo); } //This is a Hack to deal with the fact that Class table inheritance does not work well // i.e. if ContactPerson inherits from Contact and you load a collection of Contacts then // the contact person will load as type contact. If you later try load this as a ContactPerson // then the incorrect type is loaded from the BusinessObjectManager. if (!bo.GetType().IsInstanceOfType(boFromObjectManager) && bo.ClassDef.IsUsingClassTableInheritance()) { boManager.Remove(boFromObjectManager); boManager.Add(bo); return(bo); } // if the object is new it means there is an object in the BusinessObjectManager that has the same primary // key as the one being loaded. We want to return the one that was loaded without putting it into the // BusinessObjectManager (as that would cause an error). This is only used to check for duplicates or in // similar scenarios. if (boFromObjectManager.Status.IsNew) { boFromObjectManager = bo; } if (boFromObjectManager.Status.IsEditing) { return(boFromObjectManager); } objectUpdatedInLoading = PopulateBOFromReader(boFromObjectManager, dataReader, selectQuery); return(boFromObjectManager); }
private static IEnumerable<IBusinessObjectRule> GetBusinessObjectRules(IBusinessObject bo) { if (bo == null) return null; var privateMethodInfo = ReflectionUtilities.GetPrivateMethodInfo(bo.GetType(), "GetBusinessObjectRules"); return (privateMethodInfo.Invoke(bo, new object[0]) as IList<IBusinessObjectRule>); }
public BOTester(IBusinessObject businessObject) { if (businessObject == null) throw new ArgumentNullException("businessObject"); _boTestFactory = new BOTestFactory(businessObject.GetType()); BusinessObject = businessObject; }
/// <summary> /// Creates the appropriate PropertyMapper based on the BusinessObject and propertyName. /// </summary> /// <param name="businessObject"></param> /// <param name="propertyName"></param> /// <returns></returns> public static IBOPropertyMapper CreateMapper(IBusinessObject businessObject, string propertyName) { var originalPropertyName = propertyName; if (IsReflectiveProp(propertyName)) { IBusinessObject relatedBo = businessObject; while (propertyName.Contains(RELATIONSHIP_SEPARATOR)) { //Get the first property name string relationshipName = propertyName.Substring(0, propertyName.IndexOf(".")); propertyName = propertyName.Remove(0, propertyName.IndexOf(".") + 1); var newRelatedBo = relatedBo.Relationships.GetRelatedObject(relationshipName); if (newRelatedBo == null) { var invalidReason = string.Format("The '{0}' relationship of the '{1}' returned null, therefore the '{2}' property could not be accessed.", relationshipName, relatedBo.GetType().Name, propertyName); return(new NullBOPropertyMapper(originalPropertyName, invalidReason) { BusinessObject = businessObject }); } relatedBo = newRelatedBo; } return(new ReflectionPropertyMapper(propertyName) { BusinessObject = relatedBo }); } try { return(new BOPropertyMapper(propertyName) { BusinessObject = businessObject }); } catch (InvalidPropertyException) { //If the BOProp is not found then try a ReflectiveProperty. return(new ReflectionPropertyMapper(propertyName) { BusinessObject = businessObject }); } }
private void FillVertex(IBusinessObject businessObject, OVertex vertex) { var props = (from prop in businessObject.GetType().GetProperties() let attr = prop.GetCustomAttribute <DocumentPropertyAttribute>() where attr != null select new { Prop = prop, Attr = attr }).ToList(); foreach (var propertyInfo in props) { var value = propertyInfo.Prop.GetValue(businessObject); if (propertyInfo.Attr.Required && (value == null || value is string && string.IsNullOrEmpty(value.ToString()))) { throw new Exception($"The property '{nameof(propertyInfo.Attr.Key)}' is required"); } if (vertex.ContainsKey(propertyInfo.Attr.Key)) { vertex[propertyInfo.Attr.Key] = value; } else { vertex.Add(propertyInfo.Attr.Key, value); } } var children = (from prop in businessObject.GetType().GetProperties() let attr = prop.GetCustomAttribute <ChildAttribute>() where attr != null select new { Prop = prop, Attr = attr }).ToList(); foreach (var referenceList in children) { var child = (IBusinessObject)referenceList.Prop.GetValue(businessObject); if (string.IsNullOrEmpty(child.Id)) { transactionItems.Add(new TransactionItem(ETransaction.Create, child)); transactionEdges.Add(new TransactionEdge(businessObject, child, referenceList.Attr.EdgeClassName, ETransaction.Create)); } else { transactionItems.Add(new TransactionItem(ETransaction.Update, child)); } } var referenceLists = (from prop in businessObject.GetType().GetProperties() let attr = prop.GetCustomAttribute <ReferenceListAttribute>() where attr != null select new { Prop = prop, Attr = attr }).ToList(); foreach (var referenceList in referenceLists) { var list = (IList)referenceList.Prop.GetValue(businessObject); foreach (var referenceBo in list.OfType <IBusinessObject>()) { if (!string.IsNullOrEmpty(referenceBo.Id)) { transactionEdges.Add(new TransactionEdge(businessObject, referenceBo, referenceList.Attr.EdgeClassName, ETransaction.Create)); } } } }
public CacheKey(IBusinessObject businessObject, HashAlgorithmName hashAlgorithm = HashAlgorithmName.Default) : this(businessObject.GetPrimaryKey(true), businessObject.GetType(), hashAlgorithm) { }
public CacheKey(IBusinessObject businessObject, string operation, OperationReturnType returnType, HashAlgorithmName hashAlgorithm = HashAlgorithmName.Default) : this(businessObject.GetPrimaryKey(true), businessObject.GetType(), operation, returnType, true, hashAlgorithm) { }
public static int UpdateData( Expression <Func <K, P> > select, Expression <Func <K, bool> > where, IBusinessObject newValue, string leafTable, GetDatabaseFieldNameDelegate getField, bool hasModifyAudit, ContextStartup startup, IDbConnection connection, System.Data.Common.DbTransaction transaction ) { if (startup == null) { startup = new ContextStartup(null); } using (var dc = new DataContext(connection)) { var template = dc.GetTable <K>(); using (var cmd = BusinessEntityQuery.GetCommand <K, P>(dc, template, select, where)) { if (!startup.DefaultTimeout && startup.CommandTimeout > 0) { cmd.CommandTimeout = startup.CommandTimeout; } else { cmd.CommandTimeout = DEFAULTTIMEOUT; } if (transaction != null) { cmd.Transaction = transaction; } var parser = LinqSQLParser.Create(cmd.CommandText, LinqSQLParser.ObjectTypeConstants.Table); var index = 0; var sql = "declare @totalcount int ; set @totalcount = 0;"; foreach (var field in parser.FieldList) { sql += "UPDATE [" + parser.GetTableAlias(field.Name, leafTable) + "]\r\n"; var value = newValue.GetType().GetProperty(field.Name).GetValue(newValue, null); sql += "SET [" + parser.GetTableAlias(field.Name, leafTable) + "].[" + field.Name + "] = @newValue" + index + "\r\n"; if (hasModifyAudit && (field.Name != "ModifiedBy")) { sql += ", [" + parser.GetTableAlias(field.Name, leafTable) + "].[ModifiedBy] = NULL\r\n"; } if (hasModifyAudit && (field.Name != "ModifiedDate")) { sql += ", [" + parser.GetTableAlias(field.Name, leafTable) + "].[ModifiedDate] = sysdatetime()\r\n"; } sql += parser.GetFromClause(new QueryOptimizer()) + "\r\n"; sql += parser.GetWhereClause() + ";"; if (value == null) { cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("newValue" + index, System.DBNull.Value)); } else { cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("newValue" + index, value)); } sql += ";SET @totalcount = @totalcount + @@rowcount;"; index++; } sql += "select @totalcount"; sql = "set ansi_nulls off;" + sql; cmd.CommandText = sql; object p = cmd.ExecuteScalar(); return((int)p); } } }
public static void SaveBusinessObjectChanges(IBusinessObject businessObject, XDocument document, Dictionary <string, object> forcedElements, string dataType) { IVersionedBusinessObject versionedObject = businessObject as IVersionedBusinessObject; //force parent to save to change the main object version //these conditions are to secure a situation where both parent and its alternate version //are forced to save and therefore there is 1 additional update to the same record if (businessObject.Parent is IVersionedBusinessObject) { if (businessObject.Parent.AlternateVersion == null || ((IVersionedBusinessObject)businessObject.Parent.AlternateVersion).ForceSave == false) { ((IVersionedBusinessObject)businessObject.Parent).ForceSave = true; if (((IVersionedBusinessObject)businessObject.Parent).Status == BusinessObjectStatus.Unknown) { ((IVersionedBusinessObject)businessObject.Parent).Status = BusinessObjectStatus.Modified; } } } Type t = businessObject.GetType(); DatabaseMappingCache[] classCaches = BusinessObject.ClassDatabaseMappingCache[t]; foreach (DatabaseMappingCache classCache in classCaches) //foreach tableName { DatabaseMappingAttribute objAttribute = classCache.Attribute; //find or create table element XElement table = document.Root.Element(objAttribute.TableName); if (table == null) { table = new XElement(objAttribute.TableName); document.Root.Add(table); } //create new entry element XElement entry = new XElement("entry"); table.Add(entry); if (businessObject.Status != BusinessObjectStatus.Deleted) { BusinessObjectHelper.SaveBusinessObjectColumns(businessObject, t, objAttribute, entry, dataType); Guid newVersion; if (versionedObject != null) { versionedObject.NewVersion = Guid.NewGuid(); newVersion = versionedObject.NewVersion.Value; } else { newVersion = Guid.NewGuid(); } if (businessObject.Status == BusinessObjectStatus.New) { entry.Add(new XAttribute("action", "insert")); entry.Add(new XElement("version", newVersion.ToUpperString())); } else //BusinessObjectStatus.Modified or child elements changed { entry.Add(new XAttribute("action", "update")); entry.Add(new XElement("_version", newVersion.ToUpperString())); } if (forcedElements != null) { foreach (string key in forcedElements.Keys) { entry.Add(new XElement(key, forcedElements[key])); } } } else { entry.Add(new XElement("id", businessObject.Id.ToUpperString())); entry.Add(new XElement("version", businessObject.Version.ToUpperString())); entry.Add(new XAttribute("action", "delete")); } } }