AddInventoryCommand

With our repository available, the three InventoryCommand classes can be completed. The first, AddInventoryCommand, is shown as follows:

internal class AddInventoryCommand : NonTerminatingCommand, IParameterisedCommand
{
private readonly IInventoryContext _context;

internal AddInventoryCommand(IUserInterface userInterface, IInventoryContext context)
: base(userInterface)
{
_context = context;
}

public string InventoryName { get; private set; }

/// <summary>
/// AddInventoryCommand requires name
/// </summary>
/// <returns></returns>
public bool GetParameters()
{
if (string.IsNullOrWhiteSpace(InventoryName))
InventoryName = GetParameter("name");

return !string.IsNullOrWhiteSpace(InventoryName);
}

protected override bool InternalCommand()
{
return _context.AddBook(InventoryName);
}
}

The first thing to note is that the repository, IInventoryContext, is injected in the constructor along with the IUserInterface interface described in the previous chapter. The command also requires a single parameter, nameto be supplied. This is retrieved in the GetParameters method that implements the IParameterisedCommand interface, which was also covered in the previous chapter. The command is then run in the InternalCommand method, which simply executes the AddBook method on the repository and returns a bool indicating whether the command has executed successfully.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.218.184