public bool ActivateFor(Row row) { if (ReferenceEquals(null, Target)) { return(false); } attr = Target.GetAttribute <LinkingSetRelationAttribute>(); if (attr == null) { return(false); } if (!(row is IIdRow)) { throw new ArgumentException(String.Format("Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " + "but it doesn't implement IIdRow!", Target.PropertyName ?? Target.Name, row.GetType().FullName)); } var listType = Target.ValueType; if (!listType.IsGenericType || listType.GetGenericTypeDefinition() != typeof(List <>)) { throw new ArgumentException(String.Format("Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " + "but its property type is not a generic List (e.g. List<int>)!", Target.PropertyName ?? Target.Name, row.GetType().FullName)); } var rowType = attr.RowType; if (rowType.IsAbstract || !typeof(Row).IsAssignableFrom(rowType)) { throw new ArgumentException(String.Format( "Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " + "but specified row type is not valid row class!", Target.PropertyName ?? Target.Name, row.GetType().FullName)); } if (!typeof(IIdRow).IsAssignableFrom(rowType)) { throw new ArgumentException(String.Format( "Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " + "but specified row type doesn't implement IIdRow!", Target.PropertyName ?? Target.Name, row.GetType().FullName)); } listFactory = FastReflection.DelegateForConstructor <IList>(listType); rowFactory = FastReflection.DelegateForConstructor <Row>(rowType); listHandlerFactory = FastReflection.DelegateForConstructor <IListRequestProcessor>( typeof(ListRequestHandler <>).MakeGenericType(rowType)); saveHandlerFactory = FastReflection.DelegateForConstructor <ISaveRequestProcessor>( typeof(SaveRequestHandler <>).MakeGenericType(rowType)); saveRequestFactory = FastReflection.DelegateForConstructor <ISaveRequest>( typeof(SaveRequest <>).MakeGenericType(rowType)); deleteHandlerFactory = FastReflection.DelegateForConstructor <IDeleteRequestProcessor>( typeof(DeleteRequestHandler <>).MakeGenericType(rowType)); var detailRow = rowFactory(); thisKeyField = detailRow.FindFieldByPropertyName(attr.ThisKey) ?? detailRow.FindField(attr.ThisKey); if (ReferenceEquals(thisKeyField, null)) { throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified for a linking set relation in field '{2}' of row type '{3}'.", attr.ThisKey, detailRow.GetType().FullName, Target.PropertyName ?? Target.Name, row.GetType().FullName)); } itemKeyField = detailRow.FindFieldByPropertyName(attr.ItemKey) ?? detailRow.FindField(attr.ItemKey); if (ReferenceEquals(itemKeyField, null)) { throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified for a linking set relation in field '{2}' of row type '{3}'.", attr.ItemKey, detailRow.GetType().FullName, Target.PropertyName ?? Target.Name, row.GetType().FullName)); } return(true); }
public bool ActivateFor(IRow row) { if (Target is null) { return(false); } attr = Target.GetAttribute <LinkingSetRelationAttribute>(); if (attr == null) { return(false); } if (!(row is IIdRow)) { throw new ArgumentException(string.Format("Field '{0}' in row type '{1}' has a [LinkingSetRelation] attribute " + "but it doesn't implement IIdRow!", Target.PropertyName ?? Target.Name, row.GetType().FullName)); } var listType = Target.ValueType; if (!listType.IsGenericType || listType.GetGenericTypeDefinition() != typeof(List <>)) { throw new ArgumentException(string.Format("Field '{0}' in row type '{1}' has a [LinkingSetRelation] attribute " + "but its property type is not a generic List (e.g. List<int>)!", Target.PropertyName ?? Target.Name, row.GetType().FullName)); } rowType = attr.RowType; if (rowType.IsAbstract || !typeof(IRow).IsAssignableFrom(rowType) || rowType.IsInterface) { throw new ArgumentException(string.Format( "Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " + "but specified row type is not valid row class!", Target.PropertyName ?? Target.Name, row.GetType().FullName)); } if (!typeof(IIdRow).IsAssignableFrom(rowType)) { throw new ArgumentException(string.Format( "Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " + "but specified row type doesn't implement IIdRow!", Target.PropertyName ?? Target.Name, row.GetType().FullName)); } listFactory = () => (IList)Activator.CreateInstance(listType); rowFactory = () => (IRow)Activator.CreateInstance(rowType); var detailRow = rowFactory(); thisKeyField = detailRow.FindFieldByPropertyName(attr.ThisKey) ?? detailRow.FindField(attr.ThisKey); if (thisKeyField is null) { throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified for a linking set relation in field '{2}' of row type '{3}'.", attr.ThisKey, detailRow.GetType().FullName, Target.PropertyName ?? Target.Name, row.GetType().FullName)); } thisKeyCriteria = new Criteria(thisKeyField.PropertyName ?? thisKeyField.Name); itemKeyField = detailRow.FindFieldByPropertyName(attr.ItemKey) ?? detailRow.FindField(attr.ItemKey); if (itemKeyField is null) { throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified for a linking set relation in field '{2}' of row type '{3}'.", attr.ItemKey, detailRow.GetType().FullName, Target.PropertyName ?? Target.Name, row.GetType().FullName)); } if (!string.IsNullOrEmpty(attr.FilterField)) { filterField = detailRow.FindFieldByPropertyName(attr.FilterField) ?? detailRow.FindField(attr.FilterField); if (filterField is null) { throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified for a linking set relation as FilterField in field '{2}' of row type '{3}'.", attr.FilterField, detailRow.GetType().FullName, Target.PropertyName ?? Target.Name, row.GetType().FullName)); } filterCriteria = new Criteria(filterField.PropertyName ?? filterField.Name); filterValue = filterField.ConvertValue(attr.FilterValue, CultureInfo.InvariantCulture); if (filterValue == null) { filterCriteria = filterCriteria.IsNull(); queryCriteria = filterField.IsNull(); } else { filterCriteria = filterCriteria == new ValueCriteria(filterValue); queryCriteria = filterField == new ValueCriteria(filterValue); } } queryCriteria &= ServiceQueryHelper.GetNotDeletedCriteria(detailRow); return(true); }