开发者

User Menu Options

开发者 https://www.devze.com 2023-03-01 07:16 出处:网络
I need to make a user menu, if the file exists that will shows the message, \"The file exist, do you want overwrite? Y/N\" I have this method in data access layer, and can\'t send the messages direct

I need to make a user menu, if the file exists that will shows the message, "The file exist, do you want overwrite? Y/N" I have this method in data access layer, and can't send the messages direct presentation layer. First the message will send to Business layer, and then to Presentation layer. So what is the best way to do this?? I try with exceptions, but is not decorous and is not eficient. How can I do?

/*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 over开发者_Python百科write 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;                      
        }
    }
}


The DAL should never ever initiate UI actions. Your architecture is wrong. UI actions should only be initiated by the presentation layer. Your DAL should only provide metadata so that the business layer can decide what actions need to be taken, and thus inform the presentation layer.


Conventionally the UI layer should check for the existence of the file before it passes control down to the business/data layers, so that the UI and Business logic remain nicely separated.

If the UI doesn't know what logic should be applied or what checks it should perform to validate the action before it calls the DAL, then try to break the DAL implementation into two phases:

1) call a Validate() method to determine if it is OK to proceed - this would return a result to the UI that either indicates "everything is ok, go ahead" (i.e. when the file is absent) or information defining a question to ask the user (i.e. when the file is present).

2) If necessary, the UI then asks the question, and only if the response is "yes" does it then call the Execute() part of the DAL operation to actually apply the action.

This keeps the business logic and UI strictly separated, but still allows interaction during a process that the UI itself doesn't know much about.


Adjusting your Data access layer and your presentation layer to something like this would solve your problem:

/* 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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号