Posts

....
Technical Blog for .NET Developers ©

Sunday, May 24, 2020

F# Experimental: BinaryTree

In this example we write the implementation of a very simple demo of use of FSharpX.Collections.Experimental.BinaryTree<T>

Binary Trees organize in the form of Root and Node(s) in the next way:



Install the Nuget for this demo:



We represent the hierarchy of a Demo class:


        class Demo
        {
            public int Id { get; set; }

            public int? HierarchyId { get; set; }

            public string Text { get; set; }
        }  
        
        // with the next function to get the collection
        
        static IEnumerable<Demo> GetDemoItems(int number)
        {
            for (int i = 0; i <= number; i++)
            {
                yield return new Demo
                {
                    Id = i,
                    Text = $"item{i}",
                    HierarchyId = (i == 0) ? default(int?) : (i < 3) ? 0 : (i < 5) ? 1 : (i < 7) ? 2 : 3
                };
            }
        }        
  


And now we simply build the binary tree structure and iterate over the branches with the print function


        static List<Demo> items;

        static void TestBinaryTree()
        {
            items = GetDemoItems(8).ToList();

            BuildBinaryTree(items);
        }

        static void BuildBinaryTree(List<Demo> items)
        {            
            BinaryTree<Demo> btree = GetBranched(items[0]);
            
            Print(btree);
        }

        static BinaryTree<Demo> GetBranched(Demo item)
        {
            if (item == null)
                return BinaryTree<Demo>.Leaf;

            Demo item1 = items.FirstOrDefault(i => i.HierarchyId == item.Id);

            Demo item2 = items.LastOrDefault(i => i.HierarchyId == item.Id);

            return BinaryTree<Demo>.NewBranch(item, GetBranched(item1), GetBranched(item2));
        }

        static void Print(BinaryTree<Demo> btree)
        {
            if (btree == null)
                return;

            if (btree.IsBranch)
            {
                BinaryTree<Demo>.Branch branch = btree as BinaryTree<Demo>.Branch;

                Console.WriteLine($"Id:{branch.Item1.Id} Text:{branch.Item1.Text}");

                Console.WriteLine("Items:");

                Print(branch.Item2);

                Print(branch.Item3);
            }
            else if (btree.IsLeaf)
            {
                Console.WriteLine("Leaf Tree");
            }
        }  
  
The result is this cmd:


<METHOD SOFTWARE 2020 ©>

Thursday, October 10, 2019

Constructing Expression Trees

Constructing Expression Trees in runtime encapsulates the logic of the tree into the Expression

In this post we have an example of Construct and Compile Lambda Expression, with a simple logic, the function receives a name and a number of request days, and for each day adds it to the result


            var test = ConstructRuntimeFunction();

            var i = test("test", 4);

            //
            //"test, 0 added, 1 added, 2 added, 3 added, 4 added"



The expression before compile is the next tree:



The implementation of ConstructRuntimeFunction is the next:



        static Func<string, int, string> ConstructRuntimeFunction()
        {
            var prmA = Expression.Parameter(typeof(string), "name");

            var prmB = Expression.Parameter(typeof(int), "request_days");

            var loopDays = Expression.Variable(typeof(int), "loop_days");

            var dayDate = Expression.Variable(typeof(DateTime), "day_date");

            var result = Expression.Variable(typeof(string), "result");

            var loopEnd = Expression.Label();

            MethodInfo method_add_day = Assembly.GetExecutingAssembly().GetType("Test").GetMethod("increaseDay");

            MethodInfo method_write = Assembly.GetExecutingAssembly().GetType("Test").GetMethod("addVisitDay");

            var func = Expression.Lambda<Func<string, int, string>>(

                Expression.Block(new[] { result }, Expression.Assign(result, prmA),

                Expression.Block(new[] { loopDays }, Expression.Assign(loopDays, Expression.Constant(0)),

                Expression.Block(new[] { dayDate }, Expression.Assign(dayDate, Expression.Constant(DateTime.Now))),

                Expression.Loop(
                    Expression.Block(
                        Expression.IfThen(
                            Expression.Not(
                                Expression.LessThanOrEqual(loopDays, prmB)),
                                Expression.Break(loopEnd)),

                    Expression.Block(new[] { dayDate }, Expression.Assign(dayDate, Expression.Call(method_add_day, loopDays))),

                    Expression.Assign(result, Expression.Call(method_write, result, loopDays)),

                    Expression.PostIncrementAssign(loopDays)),

                loopEnd)),

            result), prmA, prmB);
            
            return func.Compile();
        }



Sunday, October 6, 2019

LINQ Expression Trees

An expression tree is an efficient data representation of a query operator lambda expression. This data representation is evaluated all simultaneously, so that an individual query can be built and launched on a data source in once

Consider the next query with two operators expecting delegates as arguments:


        private static decimal[] celsiusDegrees = new decimal[] 
            {0.0M, 6.0M, 14.0M, 25.0M, 28.0M, 34.0M, 36.0M};

        private static IEnumerable<decimal> enumerableTemps = celsiusDegrees
            .Where(i => i > 30.0M)
            .OrderBy(i => i).ToArray();

        private static IQueryable<decimal> queryableTemps = celsiusDegrees.Where(c => c > 30.0M)
            .OrderBy(c => c).AsQueryable();



When the first code is compiled .NET IL emits code with two anonymous methods, one for each of the query lambda expressions

This query is evaluated and launched linearly, first Where operator, then OrderBy operator. This linear evaluation has performance for this example, but consider a query on a large dataset. These scena is when Expression Trees become necessary. Expression trees will be generated if the operator is declared to accept an expression of a delegate

These are the two different implementations of the Where operator:

Standard query operator in LINQ to Objects API, in System.Linq.Enumerable class


        public static IEnumerable<T> Where<T>(
                this IEnumerable<T> source,
                Func<T, bool> predicate);



Implementation in the LINQ to SQL API, in the System.Linq.Queryable class


        public static IQueryable<T> Where<T>(
                this IQueryable<T> source,
                System.Linq.Expressions.Expression<Func<int, bool>> predicate);



The result of this code is the next


        Console.WriteLine(enumerableTemps);

        foreach (var temp in enumerableTemps)
            Console.WriteLine(temp);

        Console.WriteLine(Environment.NewLine);

        Console.WriteLine(queryableTemps);
        foreach (var temp in queryableTemps)
            Console.WriteLine(temp);





The ease way to create expression trees is by using advanced LINQ features, however, a tree made up only of constans would be converted automatically at compile time, the aim is build parametric expressions, such as the formula to convert celsius to kelvin degrees : K = C + 273.15


        Func<decimal, decimal> CelsiusToKelvin = (c) => c + 273.15M;

        Expression<Func<decimal, decimal>> CelsiusToKelvinExp = (c) => c + 273.15M;



The first delegate instance is no different from a regular function, you can get an expression tree representation instead of the delegate simply declaring CelsiusToKelvin as an Expression<TDelegate>

The result of this code is the next


            Console.WriteLine(CelsiusToKelvin.ToString());
            Console.WriteLine(CelsiusToKelvin(34.2M));

            Console.WriteLine(CelsiusToKelvinExp.ToString());
            Console.WriteLine(CelsiusToKelvinExp.Compile()(34.2M));





Thursday, July 25, 2019

Performance Profiler

Measuring the responsivity of an application on the usage of resources is quitely important, on distributed applications such as ASP.NET

Performance Profiler is an analysis tool included in Visual Studio. In this sample we are using it on a Web application, this is the process and the results:

Click Performance Profiler menú or press Alt+F2, to open the Diagnostic Session

-> Select the target, click start and open the Performance Wizard



-> We are measuring CPU sampling



-> Indicate the Url and start navigating





The result is an interactive report of CPU usage



With representation of hierarchy calls



Method Software 2018 ©

Tuesday, July 23, 2019

Threading : Implementation of ContinueWith()

In Threading tasks can be ordered to run consecutively with the implementations of ContinueWith methods

In this post we implement examples of this method and make a review of the options enumerated in TaskContinuationOptions. With three examples of continuation and its options

Consider this code: (notice the commented lines to make functions 3 and 4 run)


        static void TasksContinuationsDemo()
        {
            CancellationTokenSource tokenSource = new CancellationTokenSource();

            CancellationToken token = tokenSource.Token;

            token.Register(() =>
            {
                Console.WriteLine("Cancelation's been requested");
            });

            Task<bool> initialTask = new Task<bool>(() => 
            {
                Console.WriteLine("Tasks continuation demo");

                token.ThrowIfCancellationRequested();

                // OnlyOnFaulted option
                // throw new Exception();

                Console.WriteLine(true);

                return true;
            }, 
            token);

            Task task2 = initialTask.ContinueWith(result =>
            {
                Console.WriteLine("Initial Task ran to completion");

                Console.WriteLine("Initial Task result is {0}", result.Result);

            }, TaskContinuationOptions.OnlyOnRanToCompletion);

            Task<int> task3 = initialTask.ContinueWith<int>(result =>
            {
                Console.WriteLine("Initial task faulted");

                Console.WriteLine("Task 3 result is {0}", 1);

                return 1;

            }, TaskContinuationOptions.OnlyOnFaulted);

            Task<bool> task4 = initialTask.ContinueWith<bool>(result =>
            {
                Console.WriteLine("Initial Task's been canceled");

                Console.WriteLine("Task 4 result is {0}", true);

                return true;

            }, TaskContinuationOptions.OnlyOnCanceled);

            initialTask.Start();

            // OnlyOnCanceled option
            // tokenSource.Cancel();
        }


With this result:



Now uncomment the //OnlyOnFaulted option

With the next result:



And now uncomment the //OnlyOnCanceled option

With the next result:



The enum TaskContinuationOptions presents these options:



MSDN

Monday, July 22, 2019

Reflection : Chapter 1

.NET Reflection is the art of meta-coding, obtaining information that describes assemblies, modules and types.

By Reflection it is possible to discover the structure of an assembly, instantiating types with its methods, fields, accessors, and so on.

In this sample we get a known method in a known type: Reflection.Sample.Sample.GetMultiply:


        public decimal GetMultiply(decimal initialData, double addedData)
        {
            return (initialData * 3.14159M) / (decimal) addedData;
        }


Set the call to the function:


            Assembly asm = typeof(Sample.Sample).Assembly;

            Module module = asm.GetModule("Reflection.Sample.dll");

            Type type = module.GetType("Reflection.Sample.Sample", true, true);

            MethodInfo method = type.GetMethod("GetMultiply");

            ParameterInfo[] @params = method.GetParameters();

            Type returnType = method.ReturnType;

            var result = Activator.CreateInstance(returnType);

            object[] setParams = new object[@params.Length];

            for (int i = 0; i < @params.Length; i++)
            {
                setParams[i] = Activator.CreateInstance((@params[i]).ParameterType);

                switch (setParams[i].GetType().Name)
                {
                    case "Decimal":

                        setParams[i] = 10.40M;

                        break;

                    case "Double":

                        setParams[i] = 256.2048;

                        break;
                }
            }

            var obj = Activator.CreateInstance(type);

            result = method.Invoke(obj, setParams);





Static Constructors

Static Constructors initialize the static fields/properties of the class/static class. Static Constructors donot allow access modifiers so will work in background with the instructions of initialization of static resources in the class

In this post we exemplify the construction of a class containing static fields


    public class Particle<T> where T : new()
    {
        static T StaticElement { get; set; }

        public T Element { get; set; }

        public double Density { get; set; }

        // ctor
        public Particle(double density)
        {
            Density = density;
        }

        // static ctor
        static Particle()
        {
            StaticElement = new T();
        }

        public Particle<T> MuteParticle(Func<T, Particle<T>> muteFunction)
        {
            return muteFunction(StaticElement);
        }
    }



.2018

Saturday, July 20, 2019

Composing Functions

In this post we implement the technique of Composing Functions

The idea of composing functions is based on the concatenation of operations

Consider the next code:


            int var1 = 100;
            int var2 = var1 * 1024;
            int var3 = var2 + 2048;



We could create functions to perform this and our code would be the next:


            int var1 = 100;
            int var2 = CalcVar2(var1);
            int var3 = CalcVar3(var2);


        int CalcVar2(int a)
        {
            return a * 1024;
        }

        int CalcVar3(int b)
        {
            return b + 2048;
        }



This then would be improved with Composition, which automates this step


        public static Func<TSource, TEndResult> Compose<TSource, TIntermediateResult, TEndResult>(
            this Func<TSource, TIntermediateResult> func1,
            Func<TIntermediateResult, TEndResult> func2)
        {
            return sourceParam => func2(func1(sourceParam));
        }




            var autoCalcVar1 = extensions.Compose(CalcVar2, CalcVar3);

            int result = autoCalcVar1(var1);

            Console.WriteLine(result);





Wednesday, July 17, 2019

WCF One Way Dual Methods

WCF Services can declare methods which are invoked in one way mode, this implies the method will be called but the client side will not expect a reply, so will continue its operations without time leaks waiting for a response. This configuration is optimal for operations which don't require feedback results

OneWay methods can be bi-directionals, through a dual configuration, allowing client and server initiate invocation calls to each other. This mechanism is very useful for long-running processes in case the client expects a reply

In this post we develop a dual communication through a wsDualHttpBinding endpoint. These are the key points of the mechanism:

  • The service will expose an interface with the definition of the callback method, this method will be also marked as OneWay
  • This interface will be implemented in the client side
  • The OneWay method in the service will use this implementation to communicate with the client side once finish tasks
  • The client side must not dispose the InstanceContext with the server til the communication ends, the callback from the server will be treated as an event


The first step will be the definition of the interface in the service

    
    [ServiceContract(CallbackContract = typeof(IBookServiceCallBack))]
    public interface IBookService
    {
        [OperationContract]
        int AddBook(string title, string isbn, string synopsis, int idAuthor);

        [OperationContract]
        int AddAuthor(string firstName, string lastName, string biography);

        [OperationContract(IsOneWay = true)]
        void SendNewTitlesToSubscriptors(DateTime booksAddedAfterDate);
    }
    


And this is the definition of IBookServiceCallBack


    [ServiceContract] 
    public interface IBookServiceCallBack
    {
        [OperationContract(IsOneWay = true)]
        void NotifyClient(string message);
    }
    


We will focus on the method SendNewTitlesToSubscriptors, which will invoke the notification event on the client side

    
    public void SendNewTitlesToSubscriptors(DateTime booksAddedAfterDate)
    {
        _subscriptorManager.SendNewTitlesToSubscriptors(booksAddedAfterDate);

        INotificationServiceCallBack notificationSend =
            OperationContext.Current.GetCallbackChannel();

        notificationSend.NotifyClient(string.Format(
            "New titles added since {0} have been sent successfully", booksAddedAfterDate));
    }
    


With this configuration the client reference will add the interface IBookServiceCallBack

      <service name="BookService.BookService">
        <endpoint address="CallBackService" 
        binding="wsDualHttpBinding" contract="BookService.IBookService"/>
      </service>


Now the code in the client side:


    private InstanceContext instance
    {
        get { return new InstanceContext(new BookStoreNotificationHandler()); }
    }

    private BookServiceClient client
    {
        get { return new BookServiceClient(instance); }
    }

    private void btnSendNewTitles_Click(object sender, RoutedEventArgs e)
    {
        DateTime booksAddedAfterDate = dpBooksAddedFromDate.SelectedDate.Value;

        Thread threadSendNewTitles = new Thread(() =>
            {                    
                lblSendStep.SetContent("Sending new titles, please wait...");

                client.SendNewTitlesToSubscriptors(booksAddedAfterDate);
            });

        threadSendNewTitles.Start();
    }
    


and the implementation of IBookServiceCallBack as an event handler:

    
    public class BookStoreNotificationHandler : IBookServiceCallback
    {
        public void NotifyClient(string message)
        {
            MainWindow.lblSendStep.SetContent(message);
        }
    }
    


* Notice that in both cases we have used the extension method SetContent in order to control the content of the components in threading, as specified in this post: WPF MainWindow Instance

The result of this mechanism is the next:





<METHOD SOFTWARE © 2016>

Tuesday, October 2, 2018

Stopwatch of System.Diagnostics

For the sake of performance, the response times of many processes should be tested

The namespace System.Diagnostics exposes diverse classes for this purpose, one of them is the class StopWatch, this class represents a stopwatch to measure the time delay of the synchronous processes

We can browse the code up to its definition, exposed below

     
    public class Stopwatch
    {
        public static readonly long Frequency;
        public static readonly bool IsHighResolution;
        public Stopwatch();
        public TimeSpan Elapsed { get; }
        public long ElapsedMilliseconds { get; }
        public long ElapsedTicks { get; }
        public bool IsRunning { get; }
        public static long GetTimestamp();
        public void Reset();
        public void Restart();
        public void Start();
        public static Stopwatch StartNew();
        public void Stop();
    }
    


In this example we are working on an Oracle database invoking a stored procedure to update all records in a table, and we need to know with high precission how much time it delays when it´s called from our web site

We have data of our Contacts and the next stored procs to retrieve and update the whole table

     
  create or replace
  PROCEDURE P_SELECT_CONTACTS
    (RES OUT PKG_CONTACTS.CURSOR_ALL)
  IS
  BEGIN
  
    OPEN RES FOR SELECT FirstName "First Name", LastName "Last Name", 
      Phone "Phone", EMail "E Mail", PreferredContactForm "Preferred Contact Form" 
    FROM C_CONTACTS
    ORDER BY FirstName, LastName;
    
    RETURN;
  
  END P_SELECT_CONTACTS;
  
  --
  
  create or replace
  PROCEDURE P_UPDATE_CONTACT_FORM
    (v_ContactForm C_CONTACTS.PreferredContactForm%TYPE,
     v_RecordsAffected OUT NUMBER)
  IS
  BEGIN
  
    UPDATE C_CONTACTS SET PreferredContactForm = v_ContactForm;
    v_RecordsAffected := sql%rowcount;
    
  END P_UPDATE_CONTACT_FORM;


In our web site this is the display



Now we update the table through the next code

     
using (connection = new OracleConnection(ConfigurationManager.ConnectionStrings["csMethod"].ConnectionString))
{
    System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
    stopWatch.Start();

    connection.Open();
    OracleCommand command = connection.CreateCommand();
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "P_UPDATE_CONTACT_FORM";
    OracleParameter pContactForm = new OracleParameter("v_ContactForm", OracleType.VarChar);
    pContactForm.Direction = ParameterDirection.Input;
    pContactForm.Value = ddContactForm.SelectedValue;
    OracleParameter pRecordsAffected = new OracleParameter("v_RecordsAffected", 
            OracleType.Number);
    pRecordsAffected.Direction = ParameterDirection.Output;

    command.Parameters.AddRange(new OracleParameter[] { pContactForm, pRecordsAffected });
    command.ExecuteNonQuery();

    Int64 recordsAffected = Convert.ToInt64(pRecordsAffected.Value);

    lblResult.Text = recordsAffected.ToString() + " records affected";

    stopWatch.Stop();
    string elapsedTime = stopWatch.Elapsed.ToString(@"mm\:ss\.ffff");

    lblStopWatch.Text = "Elapsed time: " + elapsedTime;
}


And the result is the next



As you can see, we have a measurement of round-trip time very accurate

<METHOD SOFTWARE © 2012>

Friday, August 17, 2018

Oracle Transactions

When we have a web application with an open connection to a database, in this case Oracle 11G, we have to improve the calls to the db server, and keep the ACID rules (Atomicity, Consistence, Isolation, and Durability) of the database

We have the next scenario, a web form for sumbit a new customer of our book store and all his preferences marked

In our SQL Developer, this is the schema



and the code neccessary for accomplish this task is as follows

     
create or replace
PACKAGE PKG_BOOKSTORE AS
  TYPE CURSOR_ALL IS REF CURSOR;
  PROCEDURE P_SELECT_GENRES;
  PROCEDURE NEW_CUSTOMER;
  PROCEDURE NEW_PREFERENCE;
END;

-- 

create or replace
PROCEDURE P_SELECT_GENRES
  (RES OUT PKG_BOOKSTORE.CURSOR_ALL)
IS
BEGIN

  OPEN RES FOR SELECT IdGenre, Genre FROM T_GENRES;
  
  RETURN;

END P_SELECT_GENRES;

--

create or replace
PROCEDURE NEW_CUSTOMER
  (v_FirstName IN T_CUSTOMERS.FirstName%TYPE, 
   v_LastName IN T_CUSTOMERS.LastName%TYPE,
   v_IdCustomer OUT T_CUSTOMERS.IdCustomer%TYPE)
IS BEGIN

  SELECT sqCustomers.NextVal INTO v_IdCustomer FROM DUAL;
  
  INSERT INTO T_CUSTOMERS VALUES (v_IdCustomer, v_FirstName, v_LastName);
  EXCEPTION
    WHEN DUP_VAL_ON_INDEX THEN
     RAISE_APPLICATION_ERROR(SQLCODE, 'Cannot insert duplicate value - ' + SQLERRM);
    WHEN OTHERS THEN
     RAISE_APPLICATION_ERROR(SQLCODE, SQLERRM);

END NEW_CUSTOMER;

-- 

create or replace
PROCEDURE NEW_PREFERENCE
  (v_IdCustomer IN T_CUSTOMERS.IdCustomer%TYPE, v_IdGenre IN T_GENRES.IdGenre%TYPE)
IS BEGIN

  INSERT INTO T_CUSTOMERS_GENRES VALUES (v_IdCustomer, v_IdGenre);

  EXCEPTION
    WHEN DUP_VAL_ON_INDEX THEN
     RAISE_APPLICATION_ERROR(SQLCODE, 'Cannot insert duplicate value - ' + SQLERRM);
    WHEN OTHERS THEN
     RAISE_APPLICATION_ERROR(SQLCODE, SQLERRM);
      
END NEW_PREFERENCE;


Now we include code to display all genres and get ready the form to submit a new customer

     
    private void displayGenres()
    {
        try
        {
            OracleDataReader drGenres = getGenres();

            while (drGenres.Read())
            {
                clbGenres.Items.Add(new ListItem() 
                  { Value = drGenres["IdGenre"].ToString(), 
                    Text = drGenres["Genre"].ToString() 
                  });
            }
        }
        catch (OracleException oex)
        {
            Response.Redirect("error.aspx?desc=" + oex.Message);
        }
        finally
        {
            if (connection.State == ConnectionState.Open)
                connection.Close();
        }
    }

    private OracleDataReader getGenres()
    {
        connection.Open();
        OracleCommand command = connection.CreateCommand();
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "P_SELECT_GENRES";
        OracleDataReader dr;
        OracleParameter pRes = new OracleParameter("RES", OracleType.Cursor);
        pRes.Direction = ParameterDirection.Output;

        command.Parameters.Add(pRes);

        dr = command.ExecuteReader(CommandBehavior.SingleResult);

        return dr;
    }


and we get this result set



The next step is adding the code to call the stored procs and stablish control over the transaction

     
    private void insertCustomer()
    {
        connection.Open();
        OracleCommand cmdCustomer = connection.CreateCommand();
        OracleTransaction trax_new_customer = 
           connection.BeginTransaction(IsolationLevel.Serializable);

        try
        {
            cmdCustomer.Transaction = trax_new_customer;
            cmdCustomer.CommandType = CommandType.StoredProcedure;
            cmdCustomer.CommandText = "NEW_CUSTOMER";

            cmdCustomer.Parameters.Add(new OracleParameter
               ("v_FirstName", txtFirstName.Text));
            cmdCustomer.Parameters.Add(new OracleParameter
               ("v_LastName", txtLastName.Text));

            OracleParameter v_IdCustomer = new OracleParameter
               ("v_IdCustomer", OracleType.Int32);
            v_IdCustomer.Direction = ParameterDirection.Output;

            cmdCustomer.Parameters.Add(v_IdCustomer);

            cmdCustomer.ExecuteNonQuery();

            int IdCustomer = (int)v_IdCustomer.Value;                

            foreach (ListItem genre in clbGenres.Items)
            {
                if (genre.Selected)
                {
                    OracleCommand cmdPreference = connection.CreateCommand();

                    cmdPreference.Transaction = trax_new_customer;
                    cmdPreference.CommandType = CommandType.StoredProcedure;
                    cmdPreference.CommandText = "NEW_PREFERENCE";

                    cmdPreference.Parameters.Add(new OracleParameter
                      ("v_IdCustomer", IdCustomer));
                    cmdPreference.Parameters.Add(new OracleParameter
                      ("v_IdGenre", genre.Value));

                    cmdPreference.ExecuteNonQuery();
                }
            }

            trax_new_customer.Commit();
        }
        catch (Exception ex)
        {
            trax_new_customer.Rollback();
            Response.Redirect("error.aspx?desc=" + ex.Message);
        }
    }


The .NET Framework Data Provider for Oracle only supports ReadCommitted and Serializable isolation levels

The result with a correct execution is the following



<METHOD SOFTWARE © 2012>

Sunday, March 4, 2018

HTML5 Drag and Drop

HTML5 API includes Drag and Drop (DnD) native functionality

The event listener methods for all the drag and drop events accept Event object which has a readonly attribute called dataTransfer. The event.dataTransfer returns DataTransfer object associated with the event

This is the list of events fired during the different stages

Event Description
dragstart Fires when the user starts dragging of the object.
dragenter Fired when the mouse is first moved over the target element while a drag is occuring. A listener for this event should indicate whether a drop is allowed over this location. If there are no listeners, or the listeners perform no operations, then a drop is not allowed by default.
dragover This event is fired as the mouse is moved over an element when a drag is occuring. Much of the time, the operation that occurs during a listener will be the same as the dragenter event.
dragleave This event is fired when the mouse leaves an element while a drag is occuring. Listeners should remove any highlighting or insertion markers used for drop feedback.
drag Fires every time the mouse is moved while the object is being dragged.
drop The drop event is fired on the element where the drop was occured at the end of the drag operation. A listener would be responsible for retrieving the data being dragged and inserting it at the drop location.
dragend Fires when the user releases the mouse button while dragging an object.



In this post we develop an application to handle the drag and drop events between two elements, and launch a HttpPost method in the server which will ends inserting the dragged value in database

The first step is the definition of the UXinterface, in sequence the display is this



We are adding h5utils.js file, with an implementation of AddEvent function to simplify our code


var AddEvent = (function () {
    if (document.addEventListener) {
        return function (el, type, fn) {
            if (el && el.nodeName || el === window) {
                el.addEventListener(type, fn, false);
            } else if (el && el.length) {
                for (var i = 0; i < el.length; i++) {
                    AddEvent(el[i], type, fn);
                }
            }
        };
    } else {
        return function (el, type, fn) {
            if (el && el.nodeName || el === window) {
                el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
            } else if (el && el.length) {
                for (var i = 0; i < el.length; i++) {
                    AddEvent(el[i], type, fn);
                }
            }
        };
    }
})();



Now the code to implement drag and drop events


    var pDragElement = document.createElement('p');

    var chemicalElements = document.querySelectorAll('div > p'), el = null;
    for (var i = 0; i < chemicalElements.length; i++) {

        el = chemicalElements[i];

        el.setAttribute('draggable', 'true');

        AddEvent(el, 'dragstart', dragStartElement);
        
        AddEvent(el, 'dragend', dragEndElement);        
    }

    function dragStartElement(e) {

        e.dataTransfer.effectAllowed = 'copy';
        e.dataTransfer.setData('Text', this.id);
        e.dataTransfer.setData('Type', this.innerHTML);
        
        this.style.backgroundColor = "#ffa31a";
    }
    
    function dragEndElement(e) {
        
        this.style.backgroundColor = "#fff9f0";
    }
    
    var divBoxElements = document.querySelector('#divBoxElements');

    AddEvent(divBoxElements, 'dragover', function (e) {

        if (e.preventDefault) e.preventDefault();
        e.dataTransfer.dropEffect = 'copy';
        return false;
    });

    AddEvent(divBoxElements, 'drop', function (e) {

        if (e.stopPropagation) e.stopPropagation();

        var element = e.dataTransfer.getData('Type');

        pDragElement.innerHTML = "Adding " + element + " element";

        var pClone = pDragElement.cloneNode(true);

        var newDiv = document.createElement("div");

        newDiv.appendChild(pClone);

        divBoxElements.appendChild(newDiv);

        InsertChemicalElement(element);

        return false;
    });



The function InsertChemicalElement will call the HttpPost server method


    function InsertChemicalElement(element) {
        
        var url = '@Url.Action("InsertChemicalElements", "Home")';

        $.post(url, { chemicalElement: element },
            
        function (data) {

            switch (data) {
                case 1:                    
                    divBoxElements.innerHTML = element + " inserted OK";
                    setTimeout(function() { divBoxElements.innerHTML = ""; }, 2000);
                    break;
                    
                default:
                    alert("Error inserting the element");
            }
        });
    }



The code in the server side makes use of EF, with one table and one stored procedure in the diagram



With a SOLID implementation, the code for inserting data is divided in two functions


    [HttpPost]
    public JsonResult InsertChemicalElements(string chemicalElement)
    {
        string[] elementData = chemicalElement.Split(':');

        string symbol = elementData[0].Trim();
        string name = elementData[1].Trim();

        int insertResult = _chemicalDatabase.InsertElement(symbol, name);

        return Json(insertResult);
    }


//////////


    public class ChemicalDatabase : IChemicalDatabase
    {
        public int InsertElement(string symbol, string name)
        {
            using (ChemicalsEntities entities = new ChemicalsEntities())
            {
                return entities.P_INSERT_ELEMENT(symbol, name);
            }
        }
    }


Saturday, October 14, 2017

MVC4 Multi-language

Application Resources can be created in ASP.NET applications with two different scopes, depending on the level you want to share these resources across the application

In this example we will create a set of global resources containing dictionaries for the application display in three different languages: English (en), Spanish (es), and Catalán (ca)



The application we will build is a data-entry form based on Guest model, and will be displayed in the selected language


    public class Guest
    {
        [Required(ErrorMessageResourceType = typeof(Resources.Dictionary), 
         ErrorMessageResourceName="RequiredFirstName")]
        public string FirstName { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Dictionary), 
         ErrorMessageResourceName = "RequiredLastName")]
        public string LastName { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}")]
        public DateTime? BirthDate { get; set; }
    }




We will add the next div element to the _layout.cshtml view in order to generate the language selector


    <div class="content-wrapper">
        @{
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
            string selectedLanguageStyle = "background-color:#FFDAA3;font-weight:bold;";
            <div class="float-right">                        
            @switch (currentCulture.TwoLetterISOLanguageName)
            {
                case "en":
                    <a href='@Url.Action("ChangeLanguage", 
                                            "Home", new { culture = "en-US" })'>English</a>
                    <a href='@Url.Action("ChangeLanguage", 
                                            "Home", new { culture = "es-ES" })'>Spanish</a>
                    <a href='@Url.Action("ChangeLanguage", 
                                            "Home", new { culture = "ca-ES" })'>Catalán</a>
                    break;
                case "es":
                    <a href='@Url.Action("ChangeLanguage", 
                                            "Home", new { culture = "en-US" })'>English</a>
                    <a href='@Url.Action("ChangeLanguage", 
                                            "Home", new { culture = "es-ES" })'>Spanish</a>
                    <a href='@Url.Action("ChangeLanguage", 
                                            "Home", new { culture = "ca-ES" })'>Catalán</a>
                    break;
                case "ca":
                    <a href='@Url.Action("ChangeLanguage", 
                                            "Home", new { culture = "en-US" })'>English</a>
                    <a href='@Url.Action("ChangeLanguage", 
                                            "Home", new { culture = "es-ES" })'>Spanish</a>
                    <a href='@Url.Action("ChangeLanguage", 
                                            "Home", new { culture = "ca-ES" })'>Catalán</a>
                    break;
            }
            </div>
        }
    </div>


This is the action method called from the language selectors (anchor elements), inside the HomeController


    public ActionResult ChangeLanguage(string culture)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
        Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;

        Session["lang"] = culture;

        return Redirect(Request.UrlReferrer.ToString());
    }


The approach is this: we will create a BaseController to handle the ExecuteCore method, and stablish the thread's current culture. ExecuteCore method invokes the action in the current controller context. The controllers in our application will inherit BaseController


    public class BaseController : Controller
    {
        //
        // GET: /Base/

        protected override void ExecuteCore()
        {
            var culture = Session["lang"] ?? "en-US";        

            Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture.ToString());
            Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;

            base.ExecuteCore();
        }

        protected override bool DisableAsyncSupport
        {
            get { return true; }
        }
    }


DysableAsyncSupport property gets whether to disable the asynchronous support for the controller. This flag is for backwards compatibility. ASP.NET MVC 4. allows a controller to support asynchronous patterns. This means ExecuteCore doesn't get called on derived classes. Derived classes can override this flag and set to true if they still need ExecuteCore to be called

The strongly-typed view based on Guest model is this:


@model MvcApp1.Models.Guest

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Resources.Dictionary.ViewTitle</h2>

<div>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <table>
            <tbody>
                <tr>
                    <td>
                        <div class="editor-field">
                            <span>@Resources.Dictionary.FirstName</span>
                        </div>                        
                    </td>
                    <td>
                        @Html.EditorFor(model => model.FirstName)
                        @Html.ValidationMessageFor(model => model.FirstName)                        
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="editor-field">
                            <span>@Resources.Dictionary.LastName</span>
                        </div>                        
                    </td>
                    <td>
                        @Html.EditorFor(model => model.LastName)
                        @Html.ValidationMessageFor(model => model.LastName)                        
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="editor-field">
                            <span>@Resources.Dictionary.BirthDate</span>
                        </div>                        
                    </td>
                    <td>
                        @Html.EditorFor(model => model.BirthDate)
                    </td>
                </tr>
            </tbody>
        </table>
        <input type="submit" value="Save"/>
    }    
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}


The result is the next:




<METHOD SOFTWARE © 2014>

Tuesday, May 9, 2017

Generics : Ordered Collections

Ordered Collections are correspondants collections of IEnumerable and IQueryable types

As we apply order by clauses the collections become IOrdered_

In this post we have a simple demo, intended to create a third ordered collection using the implementation of the method CreateOrderedEnumerable







Now we are creating a IOrderedQueryable collection from the first one, in its implementation of enumerable is the function of the queryable expression



Saturday, January 28, 2017

Json RPC Requests

Json RPC is becoming an extended messaging system as it is adjusted to most of actual web scenarios

Nugets for json de-serialization implement the structures to parametrize the request, we are adding Newtonsoft libraries



The method to invoke is in server side gurujsonrpc.appsoft.com, requires id and one param, and its response is a json structure with version, id, and result

The request class exposes one method returning the raw WebResponse of the invocation


    public class JsonRPCClient : HttpWebClientProtocol
    {
        public string MethodVerb { get; set; }

        public string Version { get; set; }

        public string Id { get; set; }

        public string InvokeUrl { get; set; }

        public string MethodName { get; set; }

        public object ParamsElements { get; set; }


        public WebResponse Invoke()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(InvokeUrl);

            webRequest.ContentType = "application/json-rpc";
            webRequest.Method = MethodVerb;

            JObject message = new JObject();
            message.Add(new JProperty("jsonrpc", Version));
            message.Add(new JProperty("id", Id));
            message.Add(new JProperty("method", MethodName));

            if (ParamsElements != null)
            {
                object[] _params = new [] { ParamsElements };

                JArray _elements = new JArray();
                _elements.Add(_params);

                message.Add(new JProperty("params", _elements));
            }

            string s = JsonConvert.SerializeObject(message);
            byte[] byteArray = Encoding.UTF8.GetBytes(s);
            webRequest.ContentLength = byteArray.Length;
            Stream dataStream = webRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse webResponse = webRequest.GetResponse();

            return webResponse;
        }

    }


The method response returns the json object with the results


            var client = new JsonRPCClient
                {
                    InvokeUrl = "https://gurujsonrpc.appspot.com/guru",
                    MethodName = "guru.test",
                    MethodVerb = "POST",
                    Version = "2.0",
                    Id = "123",
                    ParamsElements = "Guru"
                };

            var response = client.Invoke();

            Stream responseStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(responseStream);

            string result = reader.ReadToEnd();





Saturday, January 21, 2017

F# : Recursive loops

Recursive functions is one of key concepts of functional programming. F# prefix these functions with keyword rec

In this sample we have defined three functions, two of them recursives, and one of them returning a type int list. The result is the factorization of the number in parameter

Algorithm gets a lot simplified by step in the logic of factorization. This is the algorithm translated to F#



        let divide x s = (x % s) = 0

        let rec lowFactor x s = if (divide x s) then s else lowFactor x (s + 1)

        let rec factorize x =
            [
                if (x <> 1) then

                    let i = lowFactor x 2

                    yield i

                    if (divide x i) then

                        for k in factorize (x / i) do

                            yield k

            ]

        let tst = factorize 108416 


<METHOD SOFTWARE 2020 ©>

Monday, January 2, 2017

MVC4 Ajax Forms

ASP.NET MVC 4 includes Ajax functionalities based on jQuery javascript library. Most of the Ajax features in the framework build on or extend features in jQuery

In this post we will implement an asynchronous request form with validation

This is the Chemical model in our Application

 
    public class ChemicalElement
    {
        public int IdElement { get; set; }

        [DisplayName("Name")]
        [Required]
        public string Name { get; set; }

        [DisplayName("Latin name")]
        [Required]
        public string LatinName { get; set; }

        [DisplayName("Symbol")]
        [Required]
        [RegularExpression(@"\b[a-zA-Z]{1,3}\b")]
        public string Symbol { get; set; }
    }
        


We have imported the next SQL schema (table and stored procedure)



The code to release the search of chemical elements is the next


    public PartialViewResult SearchChemicalElements(string search)
    {
        var elementsModel = SearchElementsModel(search);

        return PartialView("Elements", elementsModel);
    }

    private IEnumerable<ChemicalElement> SearchElementsModel(string search)
    {
        var allElements = Elements.SearchElements(search);

        return allElements;
    }
        


And in the Elements class the implementation of the SearchElement method is this

   
	public static IEnumerable<ChemicalElement> SearchElements(string search)
    {
        using (CHEMICALSEntities context = new CHEMICALSEntities())
        {
            ObjectResult<SearchElementsBySymbol_Result> matchElements =
                context.SearchElementsBySymbol("%" + search + "%");

            foreach (var element in matchElements)
                yield return new ChemicalElement()
                {
                    IdElement = element.IdElement,
                    Name = element.Name,
                    LatinName = element.LatinName,
                    Symbol = element.Symbol
                };
        }
    }
        


The code in the View to make the process work asynchronously, is the next


@using (Ajax.BeginForm("SearchChemicalElements", "Chemical", new AjaxOptions
    {
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "GET",
        LoadingElementId = "preloader",
        UpdateTargetId = "searchResults",
    }))
        


This code renders an html form with the next marks



The information about how to perform the AJAX request is contained in the DOM, and it is released by the jquery.unobtrusive-ajax.js code

The PartialView for Elements is strongly typed and renders a table in a loop for each element



The validation rules are specified in the model, so the code in the view

 
@Html.TextBoxFor(m => m.Symbol, new { Name = "search"})


will renders the information into these data dash attributes


data-val="true" 
data-val-regex="The field Symbol must match the regular expression '\b[a-zA-Z]{1,3}\b'." 
data-val-regex-pattern="\b[a-zA-Z]{1,3}\b" 
data-val-required="The field Symbol is required." 
id="Symbol" name="search"
    


The Ajax request, and validation are implemented inside the jQuery scripts

  
  @section scripts {
    <script type="text/javascript" src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script type="text/javascript" src="~/Scripts/jquery.validate.min.js"></script>
    <script type="text/javascript" src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
}