using System;
using System.Windows.Input;
using Chernobyl.App.Core;
using Chernobyl.Creation;
using GalaSoft.MvvmLight.CommandWpf;
namespace Chernobyl.App.Creation
{
///
/// View model representing an instance that can create objects.
///
public abstract class FactoryViewModel : ViewModel
{
///
protected FactoryViewModel(Action create, string creationName)
{
_create = create;
CreationName = creationName;
CreateCommand = new RelayCommand(_create);
}
///
/// Invoked when an items must be created.
///
public ICommand CreateCommand { get; }
///
/// The action being taken.
///
public string CreateCommandText => $"Create {CreationName}";
///
/// The name of the item getting created.
///
public string CreationName { get; }
// RelayCommand holds the Action as a weak reference, so we have to hold onto it to prevent
// it from getting garbage collected.
// ReSharper disable PrivateFieldCanBeConvertedToLocalVariable
readonly Action _create;
// ReSharper restore PrivateFieldCanBeConvertedToLocalVariable
}
///
/// A generic version of .
///
public class FactoryViewModel : FactoryViewModel
{
///
public FactoryViewModel(IFactory factory, string creationName)
: base(() => factory.Create(), creationName ?? "")
{
Factory = factory;
}
///
/// The instance used for construction.
///
public IFactory Factory { get; }
}
}