我需要创建一个用户菜单,如果该文件存在,则会显示该消息,“文件存在,是否要覆盖?是/否”我在数据访问层中有此方法,并且无法发送消息直接表示层。首先,消息将发送到Business层,然后发送到Presentation层。那么最好的方法是什么?我尝试了例外,但不是高雅而且没有效率。我该怎么办?
/*This method is in data access layer*/
public void MenuControl(string binaryfilePath)
{
if (File.Exists(binaryFilePath))
{
string overwrite = "-2";
Program.DisplayUserOptionMessage("The file: " + binaryFileName
+ " exist. You want to overwrite it? Y/N");
overwrite = Console.ReadLine();
while (overwrite != null)
{
if (overwrite.ToUpper() == "Y")
{
WriteBinaryFile(frameCodes, binaryFilePath);
break;
}
else if (overwrite.ToUpper() == "N")
{
throw new CustomException("Aborted by User...");
}
else
throw new CustomException("!!Please Select a Valid Option!!");
overwrite = Console.ReadLine();
//continue;
}
}
}
答案 0 :(得分:0)
DAL永远不应该启动UI操作。你的架构是错误的。 UI操作应仅由表示层启动。您的DAL应该只提供元数据,以便业务层可以决定需要采取的操作,从而通知表示层。
答案 1 :(得分:0)
传统上,UI层应该在将控制权传递给业务/数据层之前检查文件是否存在,以便UI和业务逻辑保持良好的分离。
如果UI不知道应该应用什么逻辑或者在调用DAL之前应该执行哪些检查以验证操作,那么尝试将DAL实现分为两个阶段:
1)调用Validate()方法来确定是否可以继续 - 这会将结果返回给UI,该结果指示“一切正常,继续”(即文件不存在时)或信息定义询问用户的问题(即文件何时出现)。
2)如有必要,UI会询问问题,并且只有当响应为“是”时,它才会调用DAL操作的Execute()部分来实际应用操作。
这使得业务逻辑和UI严格分离,但仍然允许在UI本身不太了解的过程中进行交互。
答案 2 :(得分:0)
将数据访问层和表示层调整为这样可以解决您的问题:
/* Presentation Layer */
if (DAL.FileExists(binaryPath)
{
console.WriteLine("Do you wish to overwrite?");
if (Console.ReadKey() == "Y")
{
DAL.Save(binaryPath); //proper classes in your dal etc here
}
}
else
{
DAL.Save(binaryPath);
}
/* DAL */
public bool FileExists(string path)
{
if (string.IsNullOrWhitespace(path)) return false;
return File.Exists(path);
}
public void Save(string path)
{
WriteBinaryFile(frameCodes, path);
}