DataLayer中的异常处理返回到Form

编程入门 行业动态 更新时间:2024-10-26 20:27:38
本文介绍了DataLayer中的异常处理返回到Form的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好

再次感谢所有的帮助,看起来我们已经到了那里并开始有所作为。

我想开始转向DataLayer中的异常处理,以及如何在文本框中回复错误数据的最佳方法。让我们首先从文本框开始。

我们收集表格中的所有信息,从BO中创建一个传递给BA层的对象,然后我们设置一个ErrorCode = 0 at在验证方法的开头,它将"掉落"。通过例程,如果任何值不正确,它会将ErrorCode设置为一个数字,然后我们可以在表单中使用它来显示一些Message ..例如。 ErrorCode = 1" Please enter Company Name",ErrorCode = 2" Please enter address" ........有没有更好的方法来处理这个问题,其次是如果可以,有没有办法创建一个查找,我们已经可以填充TextMessages而无需在类中输入所有内容? (类似于SQL PrimKey和Description。然后在表单上指向对应于返回的ErrorCode的消息?

BA类和两种方法。

公共类CompanyInfoBA { public Connection db = new Connection(); //此处声明应用程序所需的查询和数据库操作 public int SaveNewCompany(CompanyInfoBO objCompanyInfo) { CompanyInfoDA ComInfo = new CompanyInfoDA(); //验证将在创建要发送到DAL $ b的对象之前完成$ b int ErrorCode = 0; ErrorCode = textvalidation(objCompanyInfo); if(ErrorCode == 0) { ErrorCode = ComInfo.InsertCompanyData(objCompanyInfo); } 返回ErrorCode; } public int textvalidation(CompanyInfoBO Textfield) { int ErrorCode = 0; if(Textfield.CompanyName.Length == 0) { ErrorCode = 1; } if(Textfield.Address1.Length == 0) { ErrorCode = 2; } if(Textfield.Address2.Length == 0) { ErrorCode = 3; } 返回ErrorCode; } }

公共部分类FrCustomers:表格 { CompanyInfoBO ComInfo = new CompanyInfoBO(); public FrCustomers() { InitializeComponent(); } private void btExit_Click(object sender,EventArgs e) { this.Close(); } private void btnSave_Click(object sender,EventArgs e) { CompanyInfoBA comInfoBA = new CompanyInfoBA(); int success = 0; ComInfo.CompanyName = txtCustomerName.Text; ComInfo.Address1 = txtAddress1.Text; ComInfo.Address2 = txtAddress2.Text; ComInfo.City = txtCity.Text; ComInfo.Region = txtRegion.Text; ComInfo.PostalCode = txtPostalCode.Text; ComInfo.PhoneNumber = txtPhoneNumber.Text; success = comInfoBA.SaveNewCompany(ComInfo); if(success == 0) { MessageBox.Show(" All good"); } 其他 { MessageBox.Show("有些错误ErrorCode:" + success); } } }

其次,我听说有一个全局异常处理程序,使用谷歌似乎有很多信息在但是有不同的意见,将Exception代码恢复到用户形式的最佳方法是什么。 (了解Try Catch应该是来电者。

公共类CompanyInfoDA { DataTable dt = new DataTable() ; 连接db = new Connection(); // CompanyInfoBO info = new CompanyInfoBO(); public int InsertCompanyData(CompanyInfoBO info) { SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText =" INSERT INTO tbCompanyMainDetails VALUES('" + info。 CompanyName +"','" + info.Address1 +"')'" ;; return db.ExeNonQuery(cmd); } 公共对象SelectAllCompanyData () { Connection con = new Connection(); return dt; } }

需要一些尝试捕获wh尝试从上面检索数据到Datalayer,但是如何回复调用者,该方法只得到一个int返回一个值,在这个阶段使用的只是给一个数字。

public class Connection { public SqlConnection conn = new SqlConnection(" Data Source = WIN10_SSD \\PULSESQLEXPRESS; Initial Catalog = WCSData; Persist Security Info = True ; User ID = sa; Password = *****"); public SqlConnection getcon() { if(conn.State == System.Data.ConnectionState.Closed) { conn.Open( ); } 返回conn; } public int ExeNonQuery(SqlCommand cmd)//插入删除并附加到SQL数据库 { cmd.Connection = getcon(); int rowaffected = -1; conn.Close(); 返回rowaffected; } 公共对象ExeScalar(SqlCommand cmd)//从DB或Query中检索单个值 { cmd.Connection = getcon(); object obj = -1; obj = cmd.ExecuteScalar(); conn.Close(); return obj; } public DataTable ExeReader(SqlCommand cmd)//执行选择查询的Exe Reader { cmd.Connection = getcon(); SqlDataReader sdr; DataTable dt = new DataTable(); sdr = cmd.ExecuteReader(); dt.Load(sdr); 返回dt; }

labjac

解决方案

我认为你所描述的有几个问题。

首先。每个用户输入都需要进行验证。好的,这个验证应该在哪里?尽可能接近用户 - 形式。它倾向于使用一些可以与WinForm一起使用的UI模式。这是MVP模式。当你看到MVP有一些东西作为模型对象。它独立于BO对象。此对象应在presenter对象中验证,或者演示者对模型有效是负责任的。在winform上有关于验证器的非常好的文章(我找不到它,所以我稍后再提供)。

为什么在只调用数据表时使用业务层?验证不是业务规则,是吗?因此,MVP模式的演示者可以直接调用数据层。

更好的解决方案是创建应用程序层,它只是应用程序的外观,因此它实现了系统可能具有的所有功能。可能有SaveSomething方法验证输入模型。这意味着它基于模型对象,在UI和系统之间转换。

我认为数据层不应该验证用户输入。它是UI层。然后,演示者从模型(基于MVP模式)创建dto对象,并使用此模型调用系统方法。系统首先验证模型。然后数据应该是有效的并且可以存储在数据库中 - dto对象被转移到数据实体并由datalayer存储。

Hallo

Again just want to say thanks for all the assistance this far, seems like we are getting there and start making some sense.

I want to start moving onto the Exception handling in the DataLayer, and also how the best way to reply to incorrect data in text boxes. Let's start with the text boxes first.

We collect all the information in the form a create a object from the BO which is passed to the BA layer, then we set a ErrorCode = 0 at the beginning of the validation method, it will "fall" throught the routine and if any of the values is incorrect it will set ErrorCode to a number which we can then use in the form to display some Message.. eg. ErrorCode = 1 "Please enter Company Name", ErrorCode = 2 "Please enter address" ........ Is there a better way to handle this, and secondly if this is OK is there a way to create a lookup that we can already populate the TextMessages without typing it all in the Class? (Similar to SQL PrimKey and Description. Then Point on form to the message corresponding to the returned ErrorCode?

The BA Class and two methods.

public class CompanyInfoBA { public Connection db = new Connection(); // here declare the queries and db operations needed for the application public int SaveNewCompany(CompanyInfoBO objCompanyInfo) { CompanyInfoDA ComInfo = new CompanyInfoDA(); //validation will be done before createing the object to be send to DAL int ErrorCode = 0; ErrorCode = textvalidation(objCompanyInfo); if (ErrorCode == 0) { ErrorCode = ComInfo.InsertCompanyData(objCompanyInfo); } return ErrorCode; } public int textvalidation(CompanyInfoBO Textfield) { int ErrorCode = 0; if (Textfield.CompanyName.Length == 0) { ErrorCode = 1; } if (Textfield.Address1.Length == 0) { ErrorCode = 2; } if (Textfield.Address2.Length == 0) { ErrorCode = 3; } return ErrorCode; } }

public partial class FrCustomers : Form { CompanyInfoBO ComInfo = new CompanyInfoBO(); public FrCustomers() { InitializeComponent(); } private void btExit_Click(object sender, EventArgs e) { this.Close(); } private void btnSave_Click(object sender, EventArgs e) { CompanyInfoBA comInfoBA = new CompanyInfoBA(); int success = 0; ComInfo.CompanyName = txtCustomerName.Text; ComInfo.Address1 = txtAddress1.Text; ComInfo.Address2 = txtAddress2.Text; ComInfo.City = txtCity.Text; ComInfo.Region = txtRegion.Text; ComInfo.PostalCode = txtPostalCode.Text; ComInfo.PhoneNumber = txtPhoneNumber.Text; success = comInfoBA.SaveNewCompany(ComInfo); if (success == 0) { MessageBox.Show("All good"); } else { MessageBox.Show("Something is wrong ErrorCode:" + success); } } }

Secondly, I hear about a Global Exception handler, using Google there seems to be a lot of information in the matter but with different opinions, What is the best way to get the Exception code back to the userform. (Understand the Try Catch should be with the Caller.

public class CompanyInfoDA { DataTable dt = new DataTable(); Connection db = new Connection(); // CompanyInfoBO info = new CompanyInfoBO(); public int InsertCompanyData(CompanyInfoBO info) { SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "INSERT INTO tbCompanyMainDetails VALUES('" + info.CompanyName + "','" + info.Address1 + "')'"; return db.ExeNonQuery(cmd); } public object SelectAllCompanyData() { Connection con = new Connection(); return dt; } }

Need some try catches when trying to retrieve data from the above to the Datalayer, but how to reply back to the caller, the method only got one int return a value which at this stage is being use just to give a number.

public class Connection { public SqlConnection conn = new SqlConnection("Data Source=WIN10_SSD\\PULSESQLEXPRESS;Initial Catalog=WCSData;Persist Security Info=True;User ID=sa;Password=*****"); public SqlConnection getcon() { if (conn.State == System.Data.ConnectionState.Closed) { conn.Open(); } return conn; } public int ExeNonQuery(SqlCommand cmd) //Insert Delete and Append to SQL database { cmd.Connection = getcon(); int rowaffected = -1; conn.Close(); return rowaffected; } public object ExeScalar(SqlCommand cmd) //To Retrieve a Single value from DB or Query { cmd.Connection = getcon(); object obj = -1; obj = cmd.ExecuteScalar(); conn.Close(); return obj; } public DataTable ExeReader (SqlCommand cmd) // Exe Reader to Perform Select Query { cmd.Connection = getcon(); SqlDataReader sdr; DataTable dt = new DataTable(); sdr = cmd.ExecuteReader(); dt.Load(sdr); return dt; }

labjac

解决方案

I think there are severals problem what you are describing.

First. Each user input needs to be validate. Ok, where this validation should be? So close as it possible to user - in form. It tends to use some pattern for UI which could be used with WinForm. It is MVP pattern. When you see MVP there is something as model object. It is independent to BO object. This object should be validate in presenter object, or presenter is reponsible to model is valid. There is very nice article with validators on winform (I cannot find it so I supply it later).

Why you use business layer when there is only calling into datalyer? Validation is not business rule, is it? So presenter from MVP pattern could call directly datalayer.

Better solution is create application layer which is only facade to application, so it implements all functions which system could have. There could be SaveSomething method which validates input model. It means it based on models object which are transfered between UI and system.

I think datalayer should not validate user input. It is UI layer. Then presenter creates dto object from model (based on MVP pattern) and call system method with this model. System validate model at first. Then data should be valid and could be stored in database - dto object is transfered into data entity and stored by datalayer.

更多推荐

DataLayer中的异常处理返回到Form

本文发布于:2023-05-31 10:03:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389521.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   DataLayer   Form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!