Is it possible to reduce the number of constructors I use in this code? Multi-class constructor overloading and forwarding

I wrote this code earlier today. The purpose of this code is to implement ICommand in such a way that it only executes one method at a time and is silent for others.

I wrote it for use with a multi-platform user interface so that multiple keystrokes are not logged.

It has a ton of constructors and I'm not very good at overloading this type. After that, I take Design Patterns next semester and Object Oriented Programming for a semester, so hopefully my code will be cleaner after that!

Is there a way to shorten the code (reduce the number of constructors needed)?

using System;
using Xamarin.Forms;
using System.Windows.Input;
using System.Threading.Tasks;

namespace dobjenkins
{
    public interface IAsyncCommand : ICommand
    {
        Task ExecuteAsync (object parameter);
    }

    public class AsyncCommand : IAsyncCommand
    {
        private readonly Func<object, Task> execute;
        private readonly Func<object, bool> canExecute;

        ///
        /// Constructors and initializors
        ///
        protected AsyncCommand ()
        {
        }

        public AsyncCommand (Func<object, Task> execute, Func<object, bool> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public AsyncCommand (Func<object, Task> execute)
        {
            this.execute = execute;
        }

        public AsyncCommand (Func<Task> execute, Func<bool> canExecute)
        {
            this.execute = _ => execute ();
            this.canExecute = _ => canExecute ();
        }

        public AsyncCommand (Func<Task> execute)
        {
            this.execute = _ => execute ();
        }

        ///
        ///  Execute Methods
        ///


        public async Task ExecuteAsync (object parameter)
        {
            await execute (parameter);
        }

        public async void Execute (object parameter)
        {
            await ExecuteAsync (parameter);
        }


        /// 
        /// CanExecute methods/Event
        ///

        public event EventHandler CanExecuteChanged;

        public void ChangeCanExecute ()
        {
            var ev = CanExecuteChanged;
            if (ev != null) {
                ev (this, EventArgs.Empty);
            }
        }

        public bool CanExecute (object parameter)
        {
            return canExecute == null || canExecute (parameter);
        }
    }

    public sealed class AsyncCommand<T> : AsyncCommand
    {
        private readonly Func<T, Task> execute;
        private readonly Func<T, bool> canExecute;

        public AsyncCommand (Func<T, Task> execute)
        {
            this.execute = execute;
        }

        public AsyncCommand (Func<T, Task> execute, Func<T, bool> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        ///
        ///  Execute Methods
        ///

        public async Task ExecuteAsync (T parameter)
        {
            await execute (parameter);
        }

        public async void Execute (T parameter)
        {
            await ExecuteAsync (parameter);
        }

        public bool CanExecute (T parameter)
        {
            return canExecute == null || canExecute (parameter);
        }
    }

    public class ExclusiveCommand : ICommand
    {
        protected ICommand Backing;
        protected static bool IsBusy = false;


        // 
        //  Constructors
        //
        #region Constructors
        public ExclusiveCommand()
        {
        }

        //
        // SYNC (normal) CONSTRUCTORS
        //

        public ExclusiveCommand (Action<object> execute, Func<object, bool> canExecute)
        {
            Backing = new Command (execute, canExecute);
        }

        public ExclusiveCommand (Action<object> execute)
        {
            Backing = new Command (execute);
        }

        public ExclusiveCommand (Action execute, Func<bool> canExecute)
        {
            Backing = new Command (execute, canExecute);
        }

        public ExclusiveCommand (Action execute)
        {
            Backing = new Command (execute);
        }

        //
        // ASYNC CONSTRUCTORS
        //


        public ExclusiveCommand (Func<object, Task> execute, Func<object, bool> canExecute)
        {
            Backing = new AsyncCommand (execute, canExecute);
        }

        public ExclusiveCommand (Func<object, Task> execute)
        {
            Backing = new AsyncCommand (execute);
        }

        public ExclusiveCommand (Func<Task> execute, Func<bool> canExecute)
        {
            Backing = new AsyncCommand (execute, canExecute);
        }

        public ExclusiveCommand (Func<Task> a)
        {
            Backing = new AsyncCommand (a);
        }

        // 
        //  End Constructors
        //
        #endregion Constructors

        // Execute

        public async void Execute (object parameter)
        {
            if (IsBusy) {
                return;
            }
            IsBusy = true;

            var async = Backing as AsyncCommand;
            if (async != null) {
                await async.ExecuteAsync (parameter);
            } else {
                Backing.Execute (parameter);
            }

            IsBusy = false;
        }

        //
        /// Can execute
        //

        public event EventHandler CanExecuteChanged;

        public void ChangeCanExecute ()
        {
            var ev = CanExecuteChanged;
            if (ev != null) {
                ev (this, EventArgs.Empty);
            }
        }

        public bool CanExecute (object parameter)
        {
            return Backing.CanExecute (parameter);
        }
    }
}

public sealed class ExclusiveCommand<T> : ExclusiveCommand
{
    /// 
    ///  Constructors
    ///
    #region Constructors
    public ExclusiveCommand()
    {
    }

    //
    // SYNC (normal) CONSTRUCTORS
    //

    public ExclusiveCommand (Action<T> execute, Func<T, bool> canExecute)
    {
        Backing = new Command<T> (execute, canExecute);
    }

    public ExclusiveCommand (Action<T> execute)
    {
        Backing = new Command<T> (execute);
    }

    //
    // ASYNC CONSTRUCTORS
    //


    public ExclusiveCommand (Func<T, Task> execute, Func<T, bool> canExecute)
    {
        Backing = new AsyncCommand<T> (execute, canExecute);
    }

    public ExclusiveCommand (Func<T, Task> execute)
    {
        Backing = new AsyncCommand<T> (execute);
    }

    //
    //  End Constructors
    //
    #endregion Constructors
}

      

+3


source to share


1 answer


You can use optional parameters:

public AsyncCommand (Func<object, Task> execute, Func<object, bool> canExecute = null)
{
    this.execute = execute;
    this.canExecute = canExecute;
}

      



This will remove the constructor AsyncCommand (Func<object, Task> execute)

.

You can also just reload Func<object, Task>

and reload Func<Task>

and require clients to write _ => stuff

lambda. But this may or may not be acceptable depending on your requirements.

+4


source







All Articles