Skip to content

ToolsPack.Net Usage Sample

DUONG Phu-Hiep edited this page May 13, 2016 · 3 revisions

This example will show the most useful methods in ToolsPack.Net

We'll connect to the database to get all "Payment Method"; feed them to a List<string>, and finally display this list, while evaluating time spent on each steps.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ToolsPack.Sql;
using log4net;
using ToolsPack.Log4net;
using System.Collections.Generic;
using ToolsPack.Displayer;

namespace Payment.Tests
{
  [TestClass]
  public class CallDboTests
  {
    private static readonly ILog Log = LogManager.GetLogger(typeof(CallDboTests));

    [TestInitialize]
    public void SetUp()
    {
      //Tell log4net to write all log to Console
      //see: https://github.com/duongphuhiep/ToolsPack.Net/wiki#toolspacklog4net
      Log4NetQuickSetup.SetUpConsole(); 

      Log.Info("Init test"); //print to console: same as Console.WriteLine("Init test")
    }

    /// <summary>
    /// This example will make "select" in the database to get all Payment Method Name to a list
    /// then print the list to console.
    /// </summary>
    [TestMethod]
    public void MakeQuery()
    {
      var paymentMethods = new List<string>();

      //read the connection string from app.config or use the default value
      //see https://github.com/duongphuhiep/ToolsPack.Net/wiki#configreader
      var connectionString = ConfigReader.Read("connectionString", @"Data Source=192.168.0.1;Initial Catalog=toto;User ID=dev_user;Password=papapapap;");

      //setup a micro-benchmark, to mesure each step 
      //see https://github.com/duongphuhiep/ToolsPack.Net/wiki#elapsedtimewatcher
      using (var etw = ElapsedTimeWatcher.Create(Log, "MakeQuery"))
      {

        //Use ADO.NET helper to access the database
        //see https://github.com/duongphuhiep/ToolsPack.Net/wiki#toolspacksql
        using (var db = new AdoHelper(connectionString))
        {
          //we use 'etw' to log message and the benchmark
          etw.DebugFormat("Database connect OK, {0}", connectionString); //it will also display elapsed time to connect to database

          db.BeginTransaction(); //show how to make transaction (useless in this example)

          const string query = "select * from payments_methods where operation_type=@tpy";
          using (var reader = db.ExecDataReader(query, "@tpy", 0))
          {
            etw.Debug("Exec query OK"); //it will also display elapsed time to execute the query

            while (reader.Read())
            {
              //use another ADO.Net sugar helper to read cells value
              var name = reader.GetValue<string>("payment_method");

              paymentMethods.Add(name); //feed the list
            }
            etw.Debug("Collect data OK"); //it will also display elapsed time to read all the rows
          }

          db.Commit(); //show how to make transaction (useless in this example)
        }
      } // end of the micro-benchmark, it will display the total time spent in this block

      //Convert an Array/List to a readable string
      //see https://github.com/duongphuhiep/ToolsPack.Net/wiki#toolspackdisplayer
      Log.InfoFormat("Payment method found {0}", paymentMethods.Display());
    }
  }
}

Console Output

11:37:17,379 [INFO ] Init test [CallDboTests:24]
11:37:17,402 [DEBUG] Begin MakeQuery [CallDboTests:0]
11:37:17,695 [DEBUG] MakeQuery - 288412 mcs - Database connect OK, Data Source=192.168.0.1;Initial Catalog=toto;User ID=dev_user;Password=papapapap; [CallDboTests:0]
11:37:17,801 [DEBUG] MakeQuery - 106062 mcs - Exec query OK [CallDboTests:0]
11:37:17,812 [DEBUG] MakeQuery - 11381 mcs - Collect data OK [CallDboTests:0]
11:37:17,869 [INFO ] End MakeQuery : Total elapsed 462548 mcs [CallDboTests:0]
11:37:17,869 [INFO ] Payment method found { CARD, RIB, PRELEVEMENT, CHEQUE, VOILA, OHMYGOD } [CallDboTests:71]

Imagine How much code you should write without ToolsPack.Net to acheive the same things?