Posts

Showing posts from 2015

Handy - Default Style in Step Definition in SpecFlow

Put this in App.config to make the default style method name underscores: <specFlow> <trace stepDefinitionSkeletonStyle= "MethodNameUnderscores" /> </specFlow>

Took a Codility Test : Decimal Reverse, Magnitude Pole and Most Number of Occurrence.

Image
I took a test few days ago in Codility, I say this really is a good way to validate the skill set of programmers, it shows clearly how they think and approach problem vs normal multiple choice type of question or by simply having a technical interview. Here's my answer to the Codility questions. Decimal Reverse: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public static int DecimalReverse ( int n) { char [] arr = n.ToString().ToCharArray(); string reversed = string .Empty; List< string > list = new List< string >(); foreach ( var item in arr.Reverse()) { list.Add(item.ToString()); } reversed = string .Join( "" , list); return Convert.ToInt32(reversed); } Magnitude Pole: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public static int MagnitudePole ( int [...

Creating an Automated Testing Framework using Selenium.

Instant T-SQL Formatter

Imagine you are maintaining an existing system, there are countless of SP's already in use, and there are few other devs touching the sp's every now and then, and normally they look it up via sp_helptext without pressing CTRL+T, they copy and paste the result, modify it and execute it. The result is SP having white spaces and not properly formatted, wouldn't it be nice if we can format it with a click of a button? Yes, there's a tool for that. I'm attaching a link to it giving credit to the the owner of the site and the one who created it. http://architectshack.com/PoorMansTSqlFormatter.ashx#Download_5

Identity vs Scope Identity vs Ident_Current

While doing some update on an existing Store Procedure I came across a code that retrieves an Identity value. 1 2 3 4 5 6 if @@ rowcount > 0 begin select @ id = scope_identity() end else select @ id = 0 Now let's discuss when to use Identity, Scope Identity and Ident_Current, I checked the internet and found an article from the SQL Authority . Here's a brief of what it says: Select  @@IDENTITY It returns the identity value generated on a connection regardless of table and the scope which produce the value, it means that in case a trigger was also called from the same connection and produced an identity, this identity will be returned. Select SCOPE_IDENTITY It returns the identity value generated on a connection and by a statement on the same scope regardless of the table the produced the value. it will return the value that was explicitly created by the statement and will not be affected by any trigger even if it creates an identity on the same ...

Adding an Obsolete Attribute to Class Methods in C#

As a developer we normally maintain our own framework, our own abstraction of things, be it database wrappers, our own implementations of persistence managers, in some cases we figure out a better way or new way of doing things so we decide to include it in the framework, how do we now tell other developers to use this instead of the old one? The answer is the Obsolete Attribute. 1 2 3 4 5 6 7 8 9 [Obsolete("ExpandoObject.CreateObject is deprecated, please use NameValueCollection.CreateObject instead.", true)] public static void CreateObject ( this ExpandoObject that, NameValueCollection querystring) { querystring.AllKeys.ToList().ForEach(x => { if (!that.ToDictionary(y => y.Key).ContainsKey(x)) ((IDictionary< string , object >)that)[x] = querystring[x]; }); } We simply add this attribute on top of the method or class and specify our message. BTW, the Boolean parameter if set to True, will throw and exceptio...

My First Android App Published in Google Play

Image
I decided to retool myself in Java Android Development few months back, here's the first product for a local company here in Manila. I will discuss in details the technical stuff involved in the project, a soon as I free myself up from some urgent task.

Serializing JSON string to ExpandoObject

In some case you want to create and Object from JSON, a great way of doing this to deserialize this JSON to an ExpandoObject. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /// <summary> /// Convert Json String to an Expando Object /// </summary> /// <param name="that">Json string to convert</param> /// <returns>Expando Object</returns> public static ExpandoObject ToExpando ( this string that) { ExpandoObject result; try { result = JsonConvert.DeserializeObject<ExpandoObject>(that, new ExpandoObjectConverter()); } catch (Exception e) { throw e; } return result; } Here we use JsonConvert.DeserializeObject, you need to add reference to NewtonSoft.Json see below. 1 2 3 using Newtonsoft.Json ; using Newtonsoft.Json.Converters ; using Newtonsoft.Json.Linq ;

ProgrammerBase

Every programmer should inherit from ProgrammerBase :) 1 2 3 4 abstract class ProgrammerBase : ILearn, ICode, IConquer { ... }

Inserting Results from a Stored Procedure Call to a Table

This is handy, bumped into this and I thought it would be nice to take note and share. What this code illustrate is that you can basically use a stored procedure's result and insert it into a table and do your own query. Now why do we want to do that? Say for example the existing query does something and is being used by different modules and touching it would require us to check/test those modules that has a dependency on the  SP. Applying the sample below allows us to just reuse the existing SP and get the resulting data and do our own filters, grouping etc.. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 DECLARE @ Table TABLE ( SPID INT , Status VARCHAR ( MAX ), LOGIN VARCHAR ( MAX ), HostName VARCHAR ( MAX ), BlkBy VARCHAR ( MAX ), DBName VARCHAR ( MAX ), Command VARCHAR ( MAX ), CPUTime INT , DiskIO INT , LastBatch VARCHAR ( MAX ), ProgramName VARCHAR ( MAX ),...

ExecuteScalar<T>

 I came across this while working on a existing code from an ISV. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40   public class SomeDBWrapper : IDisposable { public string ExecuteExecuteScalar_String () { object value = this .ExecuteExecuteScalar(); if ( value == DBNull.Value) return "" ; else return ( string ) value ; } public int ExecuteExecuteScalar_Int () { object value = this .ExecuteExecuteScalar(); if ( value == DBNull.Value) return 0 ; else { return Convert.ToInt32( value ); } } public Int64 ExecuteExecuteScalar_Int64 () { object value = this .ExecuteExecuteScalar(); if ( value == DBNull.Value) return 0 ; else return (Int64) value ; } ...

Automatically Discover and Assign Parameter with Values to a Stored Procedure Call in C#

I'm sure most programmers have run across a situation wherein they have to pass  a lot of parameters to a stored procedure call in C#. Instead of manually typing each parameter in and assigning a value, why not develop a way to automatically discover the parameter, add it to the parameter list of the command object and assign the corresponding value from an entity or model. Here's my implementation using extension method. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 /// <summary> /// Automatically assigns parameters to Command.Parameter Collection from List of parameter names /// </summary> /// <param name="that">Command object being extended</param> /// <param name="model">Expando model</param> /// <param name="conn">sQLConnection used to g...

Helpful Scripts in T-SQL

I've been doing a lot of querying lately for a DB that I'm not that familiar, I would like to take note of helpful scripts I often used to analyze db structure. Find column name  1 2 3 4 5 6 7 SELECT t.name AS table_name , SCHEMA_NAME (schema_id) AS schema_name , c .name AS column_name FROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c .OBJECT_ID WHERE c .name LIKE '%EmployeeID%' ORDER BY schema_name , table_name ; Find table name  1 2 3 SELECT * FROM sys.Tables WHERE name LIKE '%Address%' Find SP name  1 2 3 select * from sys.procedures where name like '%name_of_proc%'

Abstract Class vs Interfaces?

Looking back, a review of concepts. Abstract Class  - is a kind of class that cannot be instantiated, Abstract class can only be inherited from or subclassed, thus enforcing hierarchy and forces derived classes to inherit the same methods and properties. Abstract Class may contain abstract methods which does not contain implementations. Interfaces- is not a class, it only contains signatures of methods and properties but has no implementation. class can implement more than one interface as oppose to classes being able to inherit from only one base class or abstract class since C# does not allow multiple inheritance.

Going back to my roots

It's been more than a year since I joined Cognizant as a .Net Developer, though the title says it unfortunately majority of the task were merely support and maintenance, data fixes and mostly analysis, though I've developed numerous tools  using .Net which the team was able to use, I miss really doing coding, I decided to go back to my roots and be a programmer again, welcoming 2015.