Fransiscus Setiawan | EV Charging & Azure Solution Architect | Sydney

Technical Insights: Azure, .NET, Dynamics 365 & EV Charging Architecture

TRY_CONVERT in SQL Server 2012

One of the new TSQL feature in SQL Server 2012 is TRY_CONVERT, basically it’s a function that will return null if the object passed is not compatible with the expected casting. In earlier version of SQL Server we don’t have this feature which means you need to make sure your data is cast-able to the expected format

Earlier Version of SQL Server – you can run this

SELECT CONVERT(INT, ‘abc’)

and it will throw an error

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value ‘abc’ to data type int.

Then you will normally do work around with this to avoid the error which means no record will be returned when the object is not a valid data type

SELECT CONVERT(INT, ‘abc’) AS TEST WHERE ISNUMERIC(‘abc’) = 1

SQL Server 2012

SELECT TRY_CONVERT(INT, ‘abc’) AS TEST

This will still return a record but with NULL value when the data is not cast-able

SQL Server udf Join

I found some interesting article to share in regards of joining the UDF into your join which ends up in executing the UDF as many as number of records being joined to. The solution is to put it into a temp table before joining it

http://sqlblog.com/blogs/rob_farley/archive/2011/11/08/when-is-a-sql-function-not-a-function.aspx

Tracing Dynamic SQL in the SQL Profiler

The default template in SQL profiler doesn’t trace the dynamic SQL executed by the stored procedure. In order to trace the Dynamic SQL, you need to turn on these 2 options in your tracing profile

SP:stmtstarting and SQL:stmtstarting

Tips: How to find Recursive Parent-Child relationship in SQL Server

Below is the sample in how to reproduce a recursive parent-child relationship and how to find out which records are the troublesome one

CREATE TABLE RecursiveSample
(
REF INT NOT NULL,
PARENT_REF INT NOT NULL,
NAME VARCHAR(255)
)
GO
/*Inserting the parents*/
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(1, 0, ‘Parent 1’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(2, 0, ‘Parent 2’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(3, 0, ‘Parent 3’)
GO
/*Inserting the child*/
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(4, 1, ‘Child Parent 1’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(5, 2, ‘Child Parent 2’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(6, 3, ‘Child Parent 3’)
GO
/*Inserting the grand child*/
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(7, 4, ‘Grand Child Parent 1’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(8, 5, ‘Grand Child Parent 2’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(9, 6, ‘Grand Child Parent 3’)
GO
/*Let’s update the record to make it recursive*/
UPDATE RecursiveSample SET PARENT_REF = 7 WHERE REF = 4
GO
/*Run this query to find the Parent child structure*/
;WITH CTE
AS(
SELECT *,[PATH]=CAST(REF AS NVARCHAR(1000))FROM RecursiveSample WHERE PARENT_REF =0
UNION ALL
SELECT A.*,[PATH]=CAST(C.[PATH]+’/’+RTRIM(C.REF) AS NVARCHAR(1000))
FROM CTE C
INNER JOIN RecursiveSample A ON C.REF=A.PARENT_REF AND PATINDEX(‘/’+RTRIM(A.REF)+’/%’,’/’+C.[PATH]+’/’)=0
)
SELECT * FROM CTE
/*
REF PARENT_REF NAME PATH

Simulate Deadlock in SQL Server

How to simulate Deadlock in SQL Server, pretty simple

1. Create a table

CREATE TABLE MyDeadlockTable
(
REF INT PRIMARY KEY NOT NULL,
NAME VARCHAR(255)
)
GO

2. Populate a table with the record
INSERT INTO MyDeadlockTable(REF, NAME) VALUES(1, ‘Record 1’)
GO
INSERT INTO MyDeadlockTable(REF, NAME) VALUES(2, ‘Record 2’)
GO

3. On the first window let’s run this
BEGIN TRAN
UPDATE MyDeadlockTable set NAME=NAME WHERE REF = 1;
WAITFOR DELAY ’00:00:15′
UPDATE MyDeadlockTable set NAME=NAME WHERE REF = 2;
COMMIT

4. Open a 2nd window let’s run this

BEGIN TRAN
UPDATE MyDeadlockTable SET NAME=NAME WHERE REF = 2;
WAITFOR DELAY ’00:00:15′
UPDATE MyDeadlockTable SET NAME=NAME WHERE REF = 1;
COMMIT

Microsoft All in One Script Framework

I’ve just found that Microsoft has just released a repository for all scripts which are driven by Technet script repository and Microsoft Customer Script & Support, the sample scripts are based on the real world scenario which are expected to save the IT admin/developer to reinvent the wheel for a particular task and instead they can build upon it or modify it. The repository covers sample for Microsoft Office, Microsoft SQL Server, Microsoft Exchange, Microsoft Sharepoint, Microsoft Windows 7, Microsoft Windows Server 2008 and Office 365

http://blogs.technet.com/b/onescript/

Coding Standard for Javascript

This is the javascript coding standard that my colleague found on Github, but seriously this is a good standard and I recommend it to any developer

https://github.com/rwldrn/idiomatic.js/

Styles in Silverlight

In silverlight, normally we apply style per XAML file by adding the style into UserControl.Resources section. Sample below

http://pastebin.com/embed_iframe.php?i=FgjnHqAT

and to apply the style in the control we do

http://pastebin.com/embed_iframe.php?i=veWGiyNh

but let’s say you want to have a global style to all of your XAML, how do you do that?You can put your style into app.xaml and then the way to refer the style in your control is as per above so basically the way you apply the style will remain the same but the difference is only where you put the style

http://pastebin.com/embed_iframe.php?i=8cw0x8h0

Process of Improving Software Quality

I would like to share my working experience at VMWare, in this last 1 year we have been improving a lot in our software development process

Last year, We had much much higher regression rate and it’s all back to the battle against developers and QA. We as developer always try to make our self look right and always feel confident that we are taking the right approach and there is no flaw in our code.

Once the build is started and installed, then QA start raising bugs to us and this process of back and forth is really not efficient and causing the software released to be delayed, frustrated developers and frustrated QA and it affects the whole thing

So how do we improve the quality

1. Code Review

-We are using Review Board to do code review before we check in the code and not after. We need to get “Review Passed” by other developer before we can check in (we use post-review command line with the changeset number to post the review)

-We need to write down our “Unit Testing” (e.g login to admin, press button A then it shows ABC) in our review

*We don’t use TFS but TFS has its own code review, but if you want to use Review Board for TFS then you can read awesome article here

2. User Acceptance Criteria

-This is a simple things that we never thought before, what we are doing now is to sit to have a developer and QA to sit together and draft the test cases for a task that the developer is going to do

-Important, we don’t start the coding first. By doing this, the developer can think about their solution carefully before writing the code and look from the broader perspective

3. Unit testing

when you have your code base flexible or modular then you can start writing unit test first before start writing the code. So you write the code to pass that unit testing criteria – TDD (Test Driven Development)

-When I say the code base is flexible or modular means that it is possible for you to inject your unit testing with some fake data/implementation

4. Developer need to be pride with its own code

We as the developer need to put pride in the code, we need to be able to explain of what your code does to other developer

-Developer need to be able to explain why your coding/solution approach is better than other coding/solution approach

-Always think maintainability, make sure you are not over engineered the solution and make sure the code is easy to understand and maintained. Make sure the code can be extensible easily in the future by other developer

-Always think scalability, how scalable is your solution when the system is growing larger

-Don’t forget to put comment in your code because it will help a lot for other developers

Conclusion

This all will take longer to start doing this, but once you and the team get used to this process then you will see the better quality of software and less frustration for the developers and QA. It will be faster at the end of the day because of less bounce back from the QA team to the developer and it results on more productivity for both teams.

It’s not a simple process but one keyword that you need to keep in mind “Believe” that this process will work

MSMQ – Basic Tutorial

I write this article in advance for my technical presentation. MSMQ is a messaging platform by Microsoft and it is built-in on the OS itself.

Installation

1. To install MSMQ, you can go to “Add/Remove program” then go to “Turn Windows features on or off” and then check “Microsoft Message Queue” Server

2. Check in the Services (services.msc), it will install “Message Queuing” service and “Net.Msmq Listener Adapter” and it should be automatically started once you have installed it

3. Make sure that these ports are not blocked by your firewall because MSMQ are using this ports

TCP: 1801
RPC: 135, 2101*, 2103*, 2105*
UDP: 3527, 1801

Basic Operation

1. in order to see your queue, you can go to “computer management – right click my computer and select manage”. Go to Services and Applications node and there will be a sub node called as “Message Queuing”

2. From this console, you can see all the messages that you want to see

3. in my presentation slides there are definitions of private queues and public queues or you can get more detail from MSDN.

4. For this tutorial, please create a private queue called as “Sample Queue” by right clicking the private queue and select “Add”

Coding tutorial

*Please import System.Messaging

1. How to send a message into a queue

Code Snippet
  1. private const string MESSAGE_QUEUE = @”.\Private$\Sample Queue”;
  2.         private MessageQueue _queue;
  3.         private void SendMessage(string message)
  4.         {
  5.             _queue = new MessageQueue(MESSAGE_QUEUE);
  6.             Message msg = new Message();
  7.             msg.Body = message;
  8.             msg.Label = “Presentation at “ + DateTime.Now.ToString();
  9.             _queue.Send(msg);
  10.             lblError.Text = “Message already sent”;
  11.         }

2. Check the queue through MMC console – right click and select refresh

2. Right click on the message and go to Body then you can see that the message is being stored as XML

3. How to process the queue?See the code snippet below

Code Snippet
  1. private const string MESSAGE_QUEUE = @”.\Private$\Sample Queue”;
  2.         private static void CheckMessage()
  3.         {
  4.             try
  5.             {
  6.                 var queue = new MessageQueue(MESSAGE_QUEUE);
  7.                 var message = queue.Receive(new TimeSpan(0, 0, 1));
  8.                 message.Formatter = new XmlMessageFormatter(
  9.                                     new String[] { “System.String,mscorlib” });
  10.                 Console.WriteLine(message.Body.ToString());
  11.             }
  12.             catch(Exception ex)
  13.             {
  14.                 Console.WriteLine(“No Message”);
  15.             }
  16.         }

Queue.Receive is a synchronous process and by passing the timespan into the function, meaning that it will throw exception of Timeout if it hasn’t received any within the duration specified

-The formatter is used to cast back to the original type

-Then you can collect the message by using “Message.Body”

-Once it’s done the message will be removed from your queue

Conclusion

Pros:

Ready to be used – It provides simple queuing for your application without you need to recreate one/reinvent the wheel

Interoperability – It allows other application to collect/process the message from MSMQ

Cons:

-Message poisoning can happen (when a message cannot be process and blocks entire queue)
-Message and queues are in proprietary format which cannot be edited directly
-The only tool is MMC administration console, or you can buy QueueExplorer (3rd party software
)

My Slides:

http://portal.sliderocket.com/vmware/MSMQ-Microsoft-Message-Queue

*DISCLAIMER:this tutorial does not represent the company that I’m working for in any way. This is just a tutorial that I created personally

 

Page 6 of 19

Powered by WordPress & Theme by Anders Norén