I have a model called WorkOrder
which I want to be able to manage from a single window.
The required operations (for now) are only Insert and view, so this is what I came up with:
My Model:
public class WorkOrder
{
public int WorkOrderId { get; set; }
public string WorkOrderName { get; set; }
public string PartNumber { get; set; }
public DateTime Created { get; set; }
public DateTime? Closed { get; set; }
public long FirstNumber { get; set; }
public long LastNumber { get; set; }
}
My View:
<Window.DataContext>
<vm:WorkOrderManagerViewModel/>
</Window.DataContext>
<StackPanel>
<!--This textbox is incharge of searching the work order in the DB.
If the value was found, a variable of type WorkOrder is set in the viewmodel,
otherwise the boolean IsNewWorkOrder is set to true and the rest of the window gets enabled-->
<TextBlock Text="Work Order Name"/>
<TextBox Text="{Binding WorkOrderName}" AcceptsReturn="False">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding GetWorkOrderCommand}"
CommandParameter="{Binding Path=Text,
RelativeSource={RelativeSource AncestorType=TextBox}}"/>
</TextBox.InputBindings>
</TextBox>
<StackPanel IsEnabled="{Binding IsNewWorkOrder}">
<TextBlock Text="Part Number"/>
<TextBox Text="{Binding SelectedWorkOrder.PartNumber}"/>
<TextBlock Text="Created"/>
<TextBox Text="{Binding SelectedWorkOrder.Created}"/>
<TextBlock Text="Closed"/>
<TextBox Text="{Binding SelectedWorkOrder.Closed}"/>
<TextBlock Text="First Number"/>
<TextBox Text="{Binding SelectedWorkOrder.FirstNumber}"/>
<TextBlock Text="Last Number"/>
<TextBox Text="{Binding SelectedWorkOrder.LastNumber}"/>
<Button Content="Save"/>
</StackPanel>
</StackPanel>
The ViewModel:
public class WorkOrderManagerViewModel : ObservableObject
{
private ApplicationDbContext db = new ApplicationDbContext();
//bound to the work order name textbox
private string _workOrderName;
public string WorkOrderName
{
get { return _workOrderName; }
set
{
_workOrderName = value;
//on text changed - clear all values
SelectedWorkOrder = null;
}
}
private WorkOrder _selectedWorkOrder;
public WorkOrder SelectedWorkOrder
{
get { return _selectedWorkOrder; }
set
{
_selectedWorkOrder = value;
RaisePropertyChanged();
}
}
//set to true if in "new work order mode"
private bool _isNewWorkOrder;
public bool IsNewWorkOrder
{
get { return _isNewWorkOrder; }
set
{
_isNewWorkOrder = value;
RaisePropertyChanged();
}
}
//after enter was pressed - used to search if exists or not
#region GetWorkOrderCommand
private ICommand _getWorkOrderCommand;
public ICommand GetWorkOrderCommand
{
get
{
if (_getWorkOrderCommand == null)
{
_getWorkOrderCommand = new CommandBase(GetWorkOrder, CanGetWorkOrder);
}
return _getWorkOrderCommand;
}
}
private bool CanGetWorkOrder(object arg)
{
string name = (arg ?? "").ToString();
return !string.IsNullOrWhiteSpace(name);
}
private void GetWorkOrder(object parameter)
{
IsNewWorkOrder = false;
string name = WorkOrderName;
var wo = db.WorkOrders.Where(x => x.WorkOrderName.ToUpper() == name.ToUpper()).FirstOrDefault();
if (wo != null)
{
SelectedWorkOrder = wo;
}
else
{
SelectedWorkOrder = new WorkOrder()
{
WorkOrderName = parameter.ToString(),
Created = DateTime.Now,
FirstNumber = 0,
LastNumber = 0
};
IsNewWorkOrder = true;
}
}
#endregion
#region SaveChanges Command
private ICommand _saveChangesCommand;
public ICommand SaveChangesCommand
{
get
{
if (_saveChangesCommand == null)
{
_saveChangesCommand = new CommandBase(SaveChanges, CanSaveChanges);
}
return _saveChangesCommand;
}
}
private bool CanSaveChanges(object arg)
{
if (SelectedWorkOrder == null)
return false;
return true;
}
private void SaveChanges(object obj)
{
if (SelectedWorkOrder.FirstNumber >= SelectedWorkOrder.LastNumber)
{
MessageBox.Show("Numeric Error");
return;
}
if (IsNewWorkOrder)
{
#region check for overlaps
var lowerOverlap = db.WorkOrders.Where(x =>
x.FirstNumber <= SelectedWorkOrder.FirstNumber
&& x.LastNumber >= SelectedWorkOrder.FirstNumber).FirstOrDefault();
if (lowerOverlap != null)
{
MessageBox.Show("Overlap Error");
return;
}
var upperOverlap = db.WorkOrders.Where(x =>
x.FirstNumber <= SelectedWorkOrder.LastNumber
&& x.LastNumber >= SelectedWorkOrder.LastNumber).FirstOrDefault();
if (upperOverlap != null)
{
MessageBox.Show("Overlap Error");
return;
}
#endregion
SelectedWorkOrder.Created = DateTime.Now;
db.WorkOrders.Add(SelectedWorkOrder);
int insertResult = db.SaveChanges();
if (insertResult > 0)
MessageBox.Show("New Work Order Created");
}
}
#endregion
}
Is this Implementation correct? Are there any guidelines or design patterns for handling this kind of Insert\Select\Update functionality? The part that I really don't like in this code is the IsNewWorkOrder
variable, any way to implement it differently?