图 1
一、从图1发现了什么问题呢?
答案:1.参数传的的太多了;2.另外注释也没写好。
说明:一个方法中,传参数的数量最好不要超过5个。
应该采用:struct或class,或一个字典类都行。其中:字典类:Dictionary,存键值对的集合类。这个一次再进一步补充说明;
struct:
由于最近的项目里面用到了这个,在此说明下:
来做一个新增数据功能:
主要有三层:1. Web 2. BusinessRules 下有 DTO 和 Logic 3.DBService
一、 Web:
1.前台代码:
<form id="form1" runat="server" submitdisabledcontrols="true"
onkeydown="if(event.keyCode==13){return false;}">
<input type="hidden" id="hd_newCount" runat="server" />//隐藏域
<asp:Button ID="btnSubmit" runat="server" Text="submit" CssClass="button5" OnClientClick="return savePageValue();"
OnClick="btnSubmit_Click" />//提交按钮
//绑定数据的代码:
<tittle:CustomDataGrid ID="grdResult" runat="server" DataKeyField="PartID" AutoGenerateColumns="False"Sort="False" CellPadding="0" FreezeColumns="0" FreezeHeader="False" GridHeight="240px"FreezeRows="0" PageSize="20" AllowSorting="false" OnItemDataBound="grdResult_ItemDataBound">
<PagerStyle HorizontalAlign="Center" Mode="NumericPages"></PagerStyle>
<Columns>
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Center" Width="20px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<input type="checkbox" id="chkl" disabled="disabled" runat="server" style="width: 100%"
value='<%# Eval("PartID") %>' /></ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="200px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<HeaderTemplate>部品代码</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblMaterialCode" runat="server" Text='<%# Eval("Materialcode")%>'></asp:Label>
</ItemTemplate>
<FooterStyle Wrap="False"></FooterStyle>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="200px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<HeaderTemplate>部品名称</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblMaterialName" runat="server" Text='<%# Eval("Materialname")%>'></asp:Label>
</ItemTemplate>
<FooterStyle Wrap="False"></FooterStyle>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="200px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<HeaderTemplate>数量</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblQty" runat="server" Text='<%# Eval("Qty")%>'></asp:Label>
</ItemTemplate>
<FooterStyle Wrap="False"></FooterStyle>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="150px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<HeaderTemplate>价格</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price")%>'></asp:Label>
</ItemTemplate>
<FooterStyle Wrap="False"></FooterStyle>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle CssClass="hidden"></HeaderStyle>
<ItemStyle CssClass="hidden" />
<ItemTemplate>
<input type="text" id="reSysID" runat="server" value='<%# Eval("PartID")%>' /></ItemTemplate>
<FooterStyle CssClass="hidden"></FooterStyle>
</asp:TemplateColumn>
</Columns>
</tittle:CustomDataGrid>
主要JS:
//数据保存
function savePageValue() {
var val = "";
var newcount = 0;
var tr = document.getElementById("grdResult").getElementsByTagName("tr");
//备注长度check
var remark = document.getElementById("txtRemark").value;
if (remark.length > 2000) {
alert("备注长度不能超过2000!");
return false;
}
//默认有条表头数据
if (tr.length < 2) {
alert(“必须添加一条明细”);
return false;
}
for (i = 1; i < tr.length; i++) {
var td = tr[i].getElementsByTagName("td");
var par = /^[0-9]*[1-9][0-9]*$/;
if (td[1].children[0].value == ""
|| td[2].children[0].value == "") {
alert("第" + i + "行")+ "的部品编码不能为空!");
return false;
}
if (!par.test(td[3].children[0].value)) {
alert("第" + i + "行" + "输入的数量不正确");
td[3].children[0].focus();
td[3].children[0].value = "";
return false;
}
if (td[1].children[0].defaultValue != ""|| td[2].children[0].defaultValue != ""|| td[3].children[0].defaultValue != "")
val = td[1].children[0].value + "$"+ td[2].children[0].value + "$"+ td[3].children[0].value + "#" + val;
else {
newcount++;
document.forms[0].hd_newCount.value = newcount;
}
}
return true;
}
2.后台代码:
//新增
public List<PartsFactory> addList = new List<PartsFactory>();//需要引入:using System.Collections.Generic;
//取得数据
private void getProcessData()
{
int i = 0;
PartsFactory parts = new PartsFactory();//实例化struct(结构)
#region 新增数据
int newRowCount = 0;
if (this.hd_newCount.Value != "")
newRowCount = int.Parse(this.hd_newCount.Value);
if (newRowCount > 0)
{
//qty
string[] qty = Request.Form["txtQty"].ToString().Split(',');
string[] MaterialID = Request.Form["MaterialID"].ToString().Split(',');
string[] Materialcode = Request.Form["Materialcode"].ToString().Split(',');
string[] Materialname = Request.Form["Materialname"].ToString().Split(',');
string[] price = Request.Form["txtPriceText"].ToString().Split(',');
for (i = 0; i < MaterialID.Length; i++)
{
parts = new PartsFactory();//实例化结构(struct)
parts.qty = qty[i];
parts.materialcode = Materialcode[i];
parts.materialname = Materialname[i];
parts.materialCodeID = MaterialID[i];
parts.price = string.IsNullOrEmpty(price[i]) ? "0" : price[i];
addList.Add(parts);
}
}
#endregion
//提交按钮
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
getProcessData();//获取数据
//数据操作
dto.SysID = logic.Submit(dto);
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
三、 BusinessRules下的Logic:
public class test
{
//X单明细
private List<PartsFactory> _lstParts;
public List<PartsFactory> LstParts
{
get { return _lstParts; }
set { _lstParts = value; }
}
}
public struct PartsFactory
{
public string detailID;//明细ID
public string materialCodeID;//部品ID
public string qty;//数量
public string recepitQty;//接受数量
public string materialname;//部品名称
public string materialcode;//部品代码
public string price;//价格
}
二、 BusinessRules下的DTO:
for (int i = 0; i < dto.LstParts.Count; i++)
{
string guid2 = Guid.NewGuid().ToString();
if (String.IsNullOrEmpty(guid2))
{
throw new Exception("取得GUID错误");
}
PartsFactory partsList = dto.LstParts[i];
string[] parametersDetailExtension = new string[6];
parametersDetailExtension[0] = guid2;
parametersDetailExtension[1] = partsList.price;//单价
parametersDetailExtension[2] = partsList.qty;//数量
parametersDetailExtension[3] = partsList.materialname;//物流名称
parametersDetailExtension[4] = partsList.materialCodeID;//物流ID
parametersDetailExtension[5] = dto.SysID;//关联主单据
//然后传到DBService进行处理
}
public class States_Command
{
//传入结构对象
public void SetState(Display displayArgs)
{
Entity entity_all = new Entity(displayArgs.EntityName);
entity_all[displayArgs.EntityName + "id"] = displayArgs.EntityId;
entity_all[displayArgs.ParamName] = new OptionSetValue(displayArgs.ParamValue);
entity_all[displayArgs.ParamDataName] = DateTime.UtcNow;
_service.Update(entity_all);
} } //结构
struct Display {
public string EntityName;
public string ParamName;
public int ParamValue;
public string ParamDataName;
public Guid EntityId;
}
不足的地方还请大家多多指教,提出来,大家一起学习。