/// <summary> /// 绑定一个循环域 /// </summary> /// <remarks> /// <list type="bullet"> /// <item><description>绑定选项信息,参见:<see cref="BindOption"/>类</description></item> /// </list> /// </remarks> /// <param name="groupName">循环域名称</param> /// <param name="table">数据表</param> /// <param name="option">绑定选项</param> public void BindGroup(string groupName, DataTable table, BindOption option) { DataTable dt = table; if (string.IsNullOrEmpty(groupName)) { throw new ArgumentNullException("groupName"); } if (option == null) { throw new ArgumentNullException("option"); } Group group = GetItem <Group>(groupName); if (group != null) { if (dt == null) { if (group.SQL == null) { throw new ArgumentException("未设置数据源,且Group节点中未配置SQL节点"); } if (string.IsNullOrEmpty(group.SQL.Value)) { throw new ArgumentException("未设置数据源,且Group节点中SQL节点为空"); } string sql = group.SQL.Value; if (sql.IndexOf("[BusinessGUID]") != -1) { sql = sql.Replace("[BusinessGUID]", "@BusinessGUID"); var parameter = new { BusinessGUID = _businessType.BusinessGUID }; dt = CPQuery.From(sql, parameter).FillDataTable(); } else { dt = CPQuery.From(sql).FillDataTable(); } } if (dt.Rows.Count == 0 || group.GroupItems.Count == 0) { return; } GroupItem baseItem = CloneGroupItem(group.GroupItems[0]); group.GroupItems.Clear(); Dictionary <string, int> dictDomains = new Dictionary <string, int>(baseItem.Domains.Count); int index = 0; foreach (Domain domain in baseItem.Domains) { dictDomains[domain.Name] = index; index++; } StringBuilder sb = new StringBuilder(); string compareDomainName = ""; if (group.Name.EndsWith("列表")) { compareDomainName = group.Name + "比较域"; } else { compareDomainName = group.Name + "列表比较域"; } Domain compareDomain = GetItem <Domain>(compareDomainName); bool requireHash = false; if (compareDomain != null && option.HashColumns != null && option.HashColumns.Count > 0) { requireHash = true; } for (int i = 0; i < dt.Rows.Count; i++) { DataRow row = dt.Rows[i]; GroupItem newItem = CloneGroupItem(baseItem); newItem.RowIndex = i + 1; foreach (DataColumn col in dt.Columns) { string domainName = ""; if (option.ColumnMap != null) { option.ColumnMap.TryGetValue(col.ColumnName, out domainName); } else { domainName = col.ColumnName; } if (string.IsNullOrEmpty(domainName)) { continue; } if (dictDomains.TryGetValue(domainName, out index)) { Domain domain = newItem.Domains[index]; BindValue(domain, group, row[col], col.DataType); if (requireHash && option.HashColumns.Contains(col.ColumnName)) { object val = row[col]; if (val != null && val != DBNull.Value) { sb.AppendFormat("!{0}", val.ToString()); } } } } if (!string.IsNullOrEmpty(option.IdentityDomain)) { if (dictDomains.TryGetValue(option.IdentityDomain, out index)) { Domain domain = newItem.Domains[index]; domain.Value = newItem.RowIndex.ToString(); } } group.GroupItems.Add(newItem); } if (requireHash) { compareDomain.Value = sb.ToString().GetHashCode().ToString(); } } }
/// <summary> /// 绑定业务域 /// </summary> /// <remarks> /// <list type="bullet"> /// <item><description>绑定选项信息,参见:<see cref="BindOption"/>类</description></item> /// </list> /// </remarks> /// <param name="table">数据表</param> /// <param name="option">绑定选项</param> public void Bind(DataTable table, BindOption option) { if (option == null) { throw new ArgumentNullException("option"); } DataTable dt = table; if (dt == null) { if (_businessType.SQL == null) { throw new ArgumentException("未设置数据源,且XML文件中SQL节点为空"); } if (string.IsNullOrEmpty(_businessType.SQL.Value)) { throw new ArgumentException("未设置数据源,且XML文件中SQL节点为空"); } string sql = _businessType.SQL.Value; if (sql.IndexOf("[BusinessGUID]") != -1) { sql = sql.Replace("[BusinessGUID]", "@BusinessGUID"); var parameter = new { BusinessGUID = _businessType.BusinessGUID }; dt = CPQuery.From(sql, parameter).FillDataTable(); } else { dt = CPQuery.From(sql).FillDataTable(); } } if (dt.Rows.Count > 0) { Dictionary <string, Domain> dictDomains = new Dictionary <string, Domain>(_businessType.Item.Count); foreach (BaseItem item in _businessType.Item) { Domain domain = item as Domain; if (domain != null) { dictDomains[domain.Name] = domain; } } DataRow row = dt.Rows[0]; foreach (DataColumn col in dt.Columns) { string domainName = ""; if (option.ColumnMap != null) { option.ColumnMap.TryGetValue(col.ColumnName, out domainName); } else { domainName = col.ColumnName; } if (string.IsNullOrEmpty(domainName)) { continue; } Domain domain; if (dictDomains.TryGetValue(domainName, out domain)) { BindValue(domain, null, row[col], col.DataType); } } } }