Posts

....
Technical Blog for .NET Developers ©

Thursday, October 21, 2021

Basic Validator

When we need to add logic rules with simplicity, we have the possibility of implement basic validations based on IoC principles

In this post we check the implementation of a ready-made generic validator


  
    public interface IValidator<M>
    {
        IValidator<M> AddRule(Expression<Func<M, bool>> rule);

        bool CheckRules(M item);
    }  
  
  


The interface is based on add boolean expressions


  
    public class Validator<M> : IValidator<M>
    {
        private List<Expression<Func<M, bool>>> Rules { get; set; } = new();

        public IValidator<M> AddRule(Expression<Func<M, bool>> rule)
        {
            this.Rules.Add(rule);

            return this;
        }

        public bool CheckRules(M item)
        {
            return this.Rules.TrueForAll(rule => rule.Compile()(item));
        }
    }
  
  


The implementation collects the rules


        static void Main(string[] args)
        {
            Console.WriteLine("Test Validator");

            Demo demo = new()
            {
                Kata = 200,
                Title = "Test"
            };

            IValidator<Demo> demoValidator = new Validator<Demo>();

            demoValidator.AddRule(d => d.Kata > 100)
                         .AddRule(d => !string.IsNullOrEmpty(d.Title));

            _ = demoValidator.CheckRules(demo);
        } 
  
  


METHOD SOFTWARE ® 2021

Thursday, July 29, 2021

FSharp : InvokeFast()

Microsoft defines InvokeFast() method to invoke an F# first class function value with two curried arguments. In some cases this will result in a more efficient application than applying the arguments successively
https://github.com/MicrosoftDocs/visualfsharpdocs/blob/master/docs/conceptual/fsharpfunc.invokefast

In this post we implement a basic use of FSharpFunc's InvokeFast, with a curried functions' structure

We begin top-to-bottom, so we first define a FSharpFunc which will takes a string param and will return a double value


              
              FSharpFunc<string, double> doublefunc = FuncConvert.ToFSharpFunc<string, double>((item) => getDouble(item));
              
              //
              
              static double getDouble(string item) => Convert.ToDouble(item);
  
After, we define a FSharpFunc which will takes a parameter type int, and will curry the function just created, with this implementation


  		FSharpFunc<int, FSharpFunc<string, double>> func = FuncConvert
                     .ToFSharpFunc<int, FSharpFunc<string, double>>(i => doublefunc);
  
Now we initialize a variable type int to test the functionality


  
            int o = 2;

            double test = FSharpFunc<int, string>.InvokeFast<double>(func, o, await getString_async(await getNumber_async(o)));  
  
  
<METHOD SOFTWARE 2021 ©>

Monday, January 4, 2021

Creating and Extending Cultures

When we are making use of Resources in order to display tags of different languages, we may need different displays for the same culture, depending on a specific area (i.e. dialects), or customizations in the content depending on the requirements

Registering a new culture is an action which requires admin privileges elevated, ideally from cmd. You have more info in MSDN

In this post we are extending an existing culture, and testing the customizations on the new culture. We are making use of System.Globalization namespace, and additionally adding a reference to sysglobl assembly



The code for registering the culture begins with the creation of the culture in disk


        CreateCultureAndSaveIt(@"D:\culture.xml");



This is the implementation:


        private static void CreateCultureAndSaveIt(string fileName)
        {
            CultureAndRegionInfoBuilder builder = new CultureAndRegionInfoBuilder("en-US-NY", CultureAndRegionModifiers.Neutral);

            CultureInfo parent = new CultureInfo("en-US");
            builder.LoadDataFromCultureInfo(parent);
            builder.LoadDataFromRegionInfo(new RegionInfo("US"));
            builder.Parent = parent;

            builder.RegionEnglishName = "New York";
            builder.RegionNativeName = "New York";
            builder.CultureEnglishName = "New York (United States)";
            builder.CultureNativeName = "New York (United States)";
            builder.NumberFormat.CurrencySymbol = "*";

            builder.Save(fileName);
        }



This method has created an xml file with the customizations specified



The next step is loading the culture into the system, this is made reading the ldml (language definition markup language) defined in the xml


            LoadCultureAndRegisterIt(fileName);



With the next implementation:


        private static void LoadCultureAndRegisterIt(string fileName)
        {
            CultureAndRegionInfoBuilder builder = CultureAndRegionInfoBuilder.CreateFromLdml(fileName);
            builder.Register();
        }



As result we have the culture installed into the system, in Windows/Globalization dir



Now it's time to test the new culture

Our web site has 2 sets of resources, and the next code in the index view







Sunday, January 3, 2021

Rosetta Code

This post is an implementation of this theorem proposed by RosettaCode.org, titled 100 doors



The only doors that remain open are those whose numbers are perfect squares.
Opening only those doors is an optimization that may also be expressed; however, as should be obvious, this defeats the intent of comparing implementations across programming languages.

I wrote the next algorithm in F#, with a slight difference which lead me to discover also the theorem of the distribution of odd numbers

The type Door:



The implementation of Visit Doors, and Set Matrix Doors:



The use of this algorithm is with C#, with a set of 1000 doors to review the results



Notice the distribution patterns of odds, fibonacci, and perfect square numbers on the board




METHOD SOFTWARE