我正在开展一个项目,其中关注点分为 3 层(演示、业务逻辑和数据访问)。使用异常冒泡,我能够将异常从一层传递到另一层,直到我最终捕获它并显示来自表示层的消息。但是,每当在 datagridview 中更新单元格时,我都不知道如何执行此操作。
//Business entity:
class Clients : Person
{
public override string FirstName
{
set
{
try
{
fc.lettersOnlyUpcaseFirstChar(ref value, false);
dal.update(this.GetType().Name, "FirstName", value, "ClientID", clientID.ToString());
}
catch (Exception)
{
throw;
}
}
}
}
//Format checking helper class:
class formatChecks
{
public void lettersOnlyUpcaseFirstChar(ref string input, bool nullable)
{
input = Regex.Replace(input, @"\s+", "");
if (input == "" && !nullable)
throw new ArgumentOutOfRangeException("Value can not be null.");
if (!input.All(Char.IsLetter))
throw new FormatException("Value may only contain letters (a-z, A-Z).");
char[] letters = input.ToLower().ToCharArray();
letters[0] = char.ToUpper(letters[0]);
string outs = new string(letters);
}
}
//Business layer helper class:
class BLHandler
{
// initialize list of Clients by using a Data Access class...
ArrClients = dal.selectAll("Clients", typeof(Clients).AssemblyQualifiedName).Cast<Clients>().ToList();
}
//The list bound to a datagridview in the presentation layer:
public frmMain()
{
dgvCustomers.DataSource = BHandler.ArrClients;
}
我知道我可以在表示层中进行格式检查,但我更愿意这样做。有什么办法可以使这项工作?