Monday, September 24, 2018

F# Enum's Usage

I have a web service project and I found my team constantly needs to convert enum to a string. The only conversion is adding space. It wastes lots of time and involves in reflection which slows down the run time performance.

Enum type is one of the favorite types in F#. C#'s enum type does not have the ability to define a value with space. Having an enum value with space is very important because this feature can save me lots of time to output enum value as string.

For example, if the enum value can be "Post Release", the ToString function can output a nice string and won't have to use attribute and reflection to do the job. C# does have the ability to have space in enum but it will need to use reflect and emit to generate the type. If you can use F#, the problem can be easily solved. Please check the following F# code:

namespace ClassLibrary1

type public EnumEng =
    Registration = 0
    | ``Under Review``=1
    | Approval = 2
    | Release = 3
    | ``Post Release`` = 4

type public EnumChn =
    注册 = 0
    | 审批 = 1
    | 批准 = 2
    | 发布 = 3
    | 发布后 = 4

type public EnumIndex =
    Reg = 0
    | Review = 1
    | Approval = 2
    | Release = 3
    | PostRelase = 4


module Test =
    let a = EnumEng.``Under Review``
    let b = EnumChn.审批

In the C# side, the intellisense won't display the enum value if it contains space. However, it will be displayed in debug mode. You can execute the following code and stop at the end of the function.

        static void Main(string[] args)
        {
            //get all string from enum0
            var strs = Enumerable.Range(0, 5)
                                 .Select(n => (ClassLibrary1.EnumEng)n)
                                 .Select(n => n.ToString())
                                 .ToList();

            // get all string from enum2
            var i = Enumerable.Range(0, 5)
                              .Select(n => (ClassLibrary1.EnumChn)n)
                              .Select(n => n.ToString())
                              .ToList();

            // parse string to enum
            var v = strs.Select(n => Enum.Parse(n))
                        .ToList();

            var x = ClassLibrary1.EnumIndex.PostRelase;

            var str = (ClassLibrary1.EnumChn)x;
        }

Both # and C# support non-English variable name, it will provide a way to localize the output as well. From the sample above, the EnumIndex is the type can used in C#/F# code. Once the value needs to be output as string, it can be convert to EnumEnglish (English string) or EnumChinese(Chinese string).



Is that convenient?

Thursday, September 13, 2018

SelfNote: WebAssembly With Blazor & .NET Core Upgrade

After set up the HTML5/Typescript roadmap for my group, I started to move my interest to other UI/visualization technology. With the Blazor is in the starting phase, I feel this is an opportunity to get real-time web rendering and .net core in one shoot. The get start part in Blazor is very helpful. Only one small bug for the project creation if you upgrade .NET core.

From PowerShell window, you can find the .Net core version by using "dotnet --info". The Blazor service generates the global.json file, which sits beside the solution file. This file is required to load the .Net core. The default value in the file is 2.1.300. 

{
  "sdk": {
    "version": "2.1.300"
  }
}
My computer is new and I directly installed 2.1.402 version. Now I got the error complains about "cannot import package". After change that version to 402, I can now manually add those created projects (Blazor Server and Client) to the solution.  

Sunday, September 9, 2018

A Tech Manager's View on Full Stack Developer

As a hiring manager, I was asked by agent about full stack developer. I am not a big fan of so-called full stack developers. 

No one can have unlimited memory and time to be expert in all area. I am more interested in knowing what the depth one can go. Those average stuff can be searched from internet. Those experience does not demonstrate the critical thinking skills. Those solution, with high probability, introduces high maintenance cost down the road.

Being deep in one area requires perseverance and talent, those attributes will provide a foundation to long lasting solution. 

Being a technology manager, maintaining different technology stack has a huge cost factor and segment team into small silos. The team effort will be lost. The tech plan and road map is missing. 

I have read some job description that requires tens of unrelated technology. This kind of position really makes me thinking how effective the tech management is and what the team looks like.