Sunday, May 03, 2020

The command pattern




Encapsulate a request as an object, thereby allowing for the parameterization of clients with different requests and the queuing or logging of requests. It also allows for the support of undoable operations.


Problem

I'm building a digital game called War Tank, where the tank is capable of performing certain activities such as moving, firing and loading. These activities may be in a sequence, repetitive or reversed. The real-world implementation of this will result in a lot of code and complexity. But wait there's a Command pattern for this. 

Analysis 

We need to be able to encapsulate activities such as moving, loading and firing as objects. This would make issuing commands easy, by creating collections of commands that can be executed in any order.  

Solution 

Let's encapsulate each activity in a class of its own, now we have a MoveActivity class, LoadActivity class and FireActivity class. We create a layer of abstraction by inheriting these classes form a common abstract class called Activity.  Now we build our Tank class that's capable of executing these activities with a method called Action. The Action method can execute any Activity. The last thing we need is a TankCommander class that can invoke activities on a Tank. The TankCommander class binds the Tank to the Activities. 



Illustration





Code


    public class Program

    {

        static void Main()
        {
            Tank tank = new Tank();
            List Seq1 = new List {
                new LoadActivity(tank),
                new FireActivity(tank),
                new MoveActivity(tank)
            };

            TankCommander  tankCommander = new TankCommander();
            foreach (var command in Seq1)
            {  
                tankCommander.SetCommand(command);
                tankCommander.ExecuteCommand();
            }
            Console.ReadKey();
        }
    }
     
    public abstract class Activity
    {
        protected Tank battleVehicle;        
        public Activity(Tank battleVehicle)
        {
            this.battleVehicle = battleVehicle;
        }
        public abstract void Execute();
    }

    public class MoveActivity : Activity
    {
        public MoveActivity(Tank battleVehicle) :
          base(battleVehicle){}

        public override void Execute()
        {
            battleVehicle.Action("Moving Tank");
        }
    }

    public class LoadActivity : Activity
    {
        public LoadActivity(Tank battleVehicle) :
          base(battleVehicle){}

        public override void Execute()
        {
            battleVehicle.Action("Reloading guns");
        }
    }

    public class FireActivity : Activity
    { 
        public FireActivity(Tank battleVehicle) :
          base(battleVehicle){}

        public override void Execute()
        {
            battleVehicle.Action("Firing guns");
        }
    }

    public class Tank
    {
        public void Action(string _action)
        {
            Console.WriteLine(_action);
        }
    }
    public class TankCommander
    {
        private Activity command;

        public void SetCommand(Activity command)
        {
            this.command = command;
        }

        public void ExecuteCommand()
        {
            command.Execute();
        }
    }

Wednesday, April 29, 2020

Part 1 : Using Jenkins to build .Net Core Solutions/Projects



This is a walk through on how to setup a Jenkins project to build a .Net Core application on a windows 10 machine. The .Net code has its source code stored on  GitHub.

Install and setup Jenkins on windows 10

In this step you will download, install and setup Jenkins on your Win 10 operating system.

  1. Download Jenkins by going to this url
    https://www.jenkins.io/download/ then scroll down to the  below table and click on windows.

  2. You should have a zip file like below. After downloading it, unzip it and follow the installation prompts. This is pretty straight forward.

  3. Once the setup is complete you can load it in your browser by going to this url below.

    http://localhost:8080/
  4. You will be presented with a login screen and you will need the initial admin password. You can find this in your Jenkins installation folder. Look for the initialAdminPassword in the secrets folder like below.

    C:\Program Files (x86)\Jenkins\secrets\initialAdminPassword
  5. Once you have signed into Jenkins you can change your password. Strongly recommend you do that as a security precaution. 

Adding Plugins


In this step you will learn how to install the MSBuild plugin and configure the plugin.

  1. Once on the main page of Jenkin's you will need to navigate to  Manage Jenkins in the left hand navigation. 
  2. Then click on Manage Plugins like below.


  3. Then click on the Available tab. 
  4. Check the MSBuild checkbox under .NET Development.

  5. Scroll down and click on "Download now and install after restart". 
  6. Click on Manage Jenkins in the left hand navigation. 
  7. Then click on Global Tool Configuration.

  8. Scroll down to MSBuild and click on Add MSBuild 
  9. Give it a name E.g. VSBuild. 
  10. Set the path to your MSBuild executable in the Path to MSBuild field. This will be located in the folder below if you have Visual studio installed. Alternatively you could download the .Net framework and locate the MSBuild executable path in the .Net installation folder. It's important to note that the MSBuild.exe is required even though there is a warning that its only expecting a folder path.C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Msbuild.exe

  11. Scroll down and click Apply and Save

Creating your first Jenkins project

In this step you will learn how to setup a Jenkins project to build a .Net Core application.
  1. Click on the New Item in the  left hand navigation. 
  2. Enter an item name and select Freestyle project. 
  3. Scroll down and click OK. 
  4. Under Source Code Management choose GIT.


  5. Enter the GIT repo url in Repository URL. Based on the type of repository you may need to provide appropriate credentials. 
  6. Scroll down to Build and click on Add build step.
  7. Choose the MSBuild Version you setup in Jenkin setup above Choose the MSBuild Build File as a full path to the Solution/Project file.
  8. For Command Line Arguments add /p:Configuration=DEBUG or /p:Configuration=RELEASE depending on what you intend to build. 



** If you are not checking in the project assets like me. This is part of the .gitignore by default, you will need an additional
build step to restore them. 


  1. Click on Add build step.
  2. Choose the MSBuild Version you setup in Jenkin setup above.
  3. Choose the MSBuild Build File as a full path to the Solution/Project file.
  4. For Command Line Arguments add /t:restore.

Build your project

In this step you obtain Nirvana when you get to build your project.

  1. Click on the name of your project. 
  2. Then click on  Build Now in the left hand side.


  3. You should see your project build status now.


Tuesday, June 18, 2013

MVC Custom Model Binder

Recently I was looking at an unusual problem where we had to add custom fields to a view on an ad hoc basis. Not really a problem if it was an MVC app talking directly to a data source. But mine was over several REST based services that were talking to a CRM dynamics backend, spread across multiple domains. Which meant we had to change the data models through out the system. That's when it occurred to  me that we can have one dedicated field  that can store all the ad hoc field data using JASON or xml and we append it to one of the existing fields. Probably not the ideal solution, but that's when I came across custom binding for MVC. I would like to share with you how simple and easy it is to use custom binding to solve similar issues.

See here for more http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder?msg=4591496#xx4591496xx