Friday, April 29, 2016

String or binary data would be truncated. EPiServer

I had a stressful story 2 days ago. Well, we were at the end of a sprint and like many other projects enjoyed our peaceful time solving a few minor issues that our customer has reported when suddenly we faced the error:
String or binary data would be truncated

I know what you are thinking:
"Yup! OK, Someone used a name longer than 50 char..."

But that was not the story!

How did I check

Fortunately, we were at the end of sprint, so I could check all new properties very fast but there was nothing wrong. I've checked my DB with a query like the one that I used for finding my string in here and checked for strings with len>50. And I've shocked since there was nothing wrong.

Then I've asked my good friends in EPiServer to help meQuan Mai and Daniel Ovaska guided me how to find the cause of the problem.

Faithfully, I had a back up from the last night so I could compare it with my working copy easily using "Visual Studio SQL server database comparison tool" that I've described in here :) and found the problem.

The Problem

As Quan Mai mentioned, when you have mismatch between your code and your DB, you will have this error. Of course I hopped that the error message were more obvious but just keep that in mind :)

Anyways, I've found the problem! Yayyyyy!
We have 2 totally different branches that we are working on for some time and we have a separated DB for each of them. Then I've found the properties of the other one inside my DB....

Well if you switch your working branch, it will change the web config, but what you don't keep your dlls inside your source control, right? So keep in mind to rebuild your code before browsing your site :)

How to compare 2 Sql Server Databases with VS 2015


There are some times that you need to check your DB, either the data or schema. Sometimes you have a back up and you want to see what has been added updated or removed from your data, like when you have an error that you cannot find the cause.
Sometimes, you want to see if someone has changed the schema and you want to know what is the change.

It is an old problem right? But in the old days you had to pay lots of money to be able to do that.
Take a look at Red Gate SQL data compare or SQL Data Examiner . Yup you have to pay at least 300$ to do that. :|
But the good news is that your Visual Studio has the ability to Do a lot of Data and Schema comparison which is free and more importantly it is inside our great tool Visual Studio. :)

How to do it?
Inside Visual Studio 2015 (mine is professional) Go to Tools >SQL Server. you will see 2 options for comparing Data, or Schema.

Insert your connection Data, click compare and you are good to go :)

Tuesday, April 26, 2016

Agile, Why shall I spend lots of time on brainstorming, pair-working, stand-up meeting


If you have every worked with Agile processes like SCRUM, you will see that there are lots of time for brainstorming/ stand-up meetings and pair working. Considering all the time that will be spent some will decide to ignore some of those rules and it may have dramatic effects on the quality of the solution/ total time that has been spent on the project.

General Idea
Back in the old days, there were people who analyze the needs, then some others had to design a system based on those analysis and at last there were some programmers who would implement the code. Generally speaking, the analyst was the only one who had to be the expert on the field of the project and based on his vision, the designers (some people with university degree in software engineering)  would design a model and test it based on the analysis and the programmers just have to implement the model.

What happened in practice
* It was very hard to implement. Software teams were not mature enough to implement everything as the idea said so in practice, they spent a lot of time on design and analysis, but at the end the implemented result was far from what the customer needed. (because of the gray zone/ incomplete data/ low-depth analysis/ lack of software knowledge, etc)

*Around 60% of the time had been used for creating documents that no one would read or understand in the end. Add this time to maintenance time since the documents also have to be the updated.

*There was a big gap between analysis and implementation, so when they finished the implementation the customer might not need it anymore. (We consider it as a failure)

What About Agile

With agile we took out the over head of analysis and design. But it is not magic! nor miracle!
It simply says, choose some programmers who know basics of software design and can solve problems (we call them developers), give them authority to decide what shall they do and let them talk to the customer.

What If we remove the brainstorming session?

In Agile, Brain storming session is a meeting that your team decide about the time that they will need on each task. On SCRUM for instance, there is a session on the start of each sprint which can take up to 1 day. So many people will say: OH! the whole team for 1 day waste their time to decide about the time that they need?!!! I will ask one of them who has enough experience to do this instead, so I will win lots of time!
Well, then you will end up with a team that has no vision about the whole tasks, and therefore the code quality will decrease and you might end up with spending lots of time on merging/refactoring/maintenance instead.
Worst than that, if your expert don't describe what he had in his mind, the one that has to implement it might implement the task with another way, and he is not to blame since he have a limited vision and with that vision, his solution works fine.

What if we remove the pair working
Pair working is the process of 2 people working together on the same machine. Well, one is working and the other is sitting behind his hand to prevent mistakes.

Yup, you might say that it will double the time that you need to spend on each task, so why to work as pairs?
Well, it will prevent people from implementing a very complicated code that no one would understand, the maintenance cost will decrease since 2 people checked it line by line. And you have 2 people to handle the changes because both have the knowledge of the project.

* My experience: don't work as pairs when the task is simple, otherwise you will get board and you've wasted your time.

What if we remove the stand-up meeting

Stand-up meeting is for your team to understand what is going on while helping each other. Some would say that when we have a problem, we will ask each other, we are good friends, etc. but in practice I saw these scenarios:
*The one who is drawn in the problem: Software developers like google very much. They will try to find the answer to all problems there. While it is a good way to solving your problems, it is a wast of time if you spend 1 week to solve a problem that your teammate already knew,

*The one with lack of experience: It is really hard to ask a question for people who don't have
enough experience, because for them it is like admitting that they don't know. Also, other people are mostly busy in the day so they might be interrupted when the others ask too much questions. Therefore, it will be late when you see their code and understand what a mess they have made up.

*The one who is not Taciturn: In every team, there are some people who enjoy their inside world, more than the outside world and that is why they choose this job. If there is no stand-up, no body will understand what are they doing, their achievements and their mess up things.

*The one with complicated solutions: Software developers like to write codes that can be applied to a general issue and normally it is good, but not when they change a simple subject to a twisted and a lot harder problem that is hard to read. Of course, code review is one of the ways to prevent this problem, but it is simpler this way. Like when you see someone wants to spend 2-3 days to implement a simple service wrapper.

*The one with rondevu point:
Stand-up meetings is a rondevu point. It help the team to manage their time (developers don't like to wake up early in the morning so you might see someone who wants to start his day at 1 a.m :) )
It helps people to maintain their focus since no one will ask you a question when you are deeply in your task

Software development is not a concrete block that you cannot change. You need to understand pros and cons and the application on your team in order to get the best result. So if you want to do Agile, but you want to get rid of any of the above, make sure that you know the consequences.

Cherry Pick! What an amazing command


Well, I've used cherry pick several times. It is a very useful tool that you will use it a lot when you understand how it works. 

Basically speaking, you will need it when you have 2 different branches that some people are developing concurrently and you want some of the changes to be implemented in both. Some might say that, we will copy the code. Well, then you will have merge errors, and if you are the one who is responsible for merges you will know that how painful it is when you want to understand which side to choose and how to merge, specially when it is not your code.

Using cherry pick, you are actually taking some code from a branch and applying it to yours, but at the same time, you are telling to your source control that you are applying one of its changes to your branch, so when you are going to merge 2 branches together, it will understand that you've already applied the change on your branch.

When you realize that you love it!

OK, I have 2 separate branches now and I want one of them to have all of the changes in the other branch, but I have some changes that I don't want to merge for 1 day or 2. Well, I it was the time that I realize how much I love this command. It is very fast and easy to use, specially with source tree. :)

Steps On Source-Tree
It can be done with these steps:

1- Select your branch (the one that you want to apply the change) as your current branch

2- On log/history window, there is a drop-down on top left, select "All Branches" instead of "Current Branch" so you can see all changes in all branches.

3- Select changes that you want using ctrl+left-click or Shift-left-click. then right click on selection and choose cherry-pick! 

Congratulations, you have done your first cherry pick! :) very easy! Right?! 

Tuesday, April 19, 2016

Search for a string in all tables, rows and columns of a DB

Several days ago, I needed to search my database for special string. Then I saw a very good query in here, but since it wasn't an article, I thought why not write a small post about it.

    @search_string  VARCHAR(100),
    @table_name     SYSNAME,
    @table_schema   SYSNAME,
    @column_name    SYSNAME,
    @sql_string     VARCHAR(2000)

SET @search_string = 'string to search'


OPEN tables_cur

FETCH NEXT FROM tables_cur INTO @table_schema, @table_name

    DECLARE columns_cur CURSOR FOR SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @table_schema AND TABLE_NAME = @table_name AND COLLATION_NAME IS NOT NULL  -- Only strings have this and they always have it

    OPEN columns_cur

    FETCH NEXT FROM columns_cur INTO @column_name
        SET @sql_string = 'IF EXISTS (SELECT * FROM ' + QUOTENAME(@table_schema) + '.' + QUOTENAME(@table_name) + ' WHERE ' + QUOTENAME(@column_name) + ' LIKE ''%' + @search_string + '%'') PRINT ''' + QUOTENAME(@table_schema) + '.' + QUOTENAME(@table_name) + ', ' + QUOTENAME(@column_name) + ''''


        FETCH NEXT FROM columns_cur INTO @column_name

    CLOSE columns_cur

    DEALLOCATE columns_cur

    FETCH NEXT FROM tables_cur INTO @table_schema, @table_name

CLOSE tables_cur

DEALLOCATE tables_cur

This query is very useful, specially when you don't know about the structure of a project. For instance, you need to check something in a CMS (lets say for debugging your code) like EpiServer but you know nothing about its structure, then finding your values will be a real pain.

Tuesday, April 12, 2016

Advanced Programming: An easy way to manage critical zones by locking

So, sometimes people might ask what is the critical zone, or why shall we manage it?
Well, on multi-thread apps like web applications, it is possible that a thread leaves a method and go to sleep and another thread change something that has an impact on those values.
The simplest example that I can come up is an "IncreaseAndSave" method like this:

void IncreaseAndSave()
     var value= ReadValueFromDB();
     // The thread1 goes to sleep

Simple scenario:
Lets say we have a value "10" in our DB and 2 threads that call our method almost at the same time. The first one will try to run the code, it reads the value from DB and increase it so Value will be "11" in our memory. Then the thread will go to sleep and thread2 will run the process. It will read value "10" again from DB since T1 didn't save it and will increase it to "11". Now both threads will call save method and we will end up with "11" instead of "12".
So the zone that we call a critical zone is from reading to saving Data in the DB.

Simplest Solution
Well there is a reserved word in C# (and almost all other programming languages) for lock. The functionality is very simple. it is like a safe box. When you lock it, no one else can enter or lock it until you leave the safe box and then someone else can take over.
The only important note is that you have to lock the same object :D (safe box)
void IncreaseAndSave()
      var value= ReadValueFromDB();
      // The thread1 goes to sleep

Make it a bit more complex
So, we saw the problem when 2 threads want to access a critical section in a method but what if our critical zone is inside 2 different methods?
For instance, if we have a decrease method, one thread may decrease the value in DB while the other one still didn't save its value.
Of course the solution is to lock both stuff with the single object (the same safe-box)

Make even more Complex
We saw the code for locking some part of codes, but the drawback of that code is that the lock area cannot be accessed by multiple threads even though they might not have an impact on each other.

For instance, take updated function below as an example. The thread thr1 will try to increase the value for key =1 so it will lock the process and goes to sleep, then thr2 will try to increase the value for key=2 but since the area is already locked by thr2, it has to wait.
It is pretty clear that in a simple web application, there will be a long queue for just saving stuff and etc, and it will be slow like if it is running on a computer with only 1 CPU and 1 thread.
So what can we do to make it efficient?

void IncreaseAndSave(int key)
       var value= ReadValueForKeyFromDB(key);
       // The thread1 goes to sleep

We need a class to return the lock for each key. The idea is to have a bank of  safe boxes instead of only 1. But we have to pay attention that the bank is the one who is responsible for finding the mutual interest. In the other word, on runtime the thread will ask the LockContainer(banker) about the safe-box with special key and then it will try to lock the safe-box (lock).
It can simply be done by code below:

  public class LockContainer
        private BlockingCollection<object> lockItems { get; set; }

        private object LocalLockItem { get; set; }

        public LockContainer()
            lockItems = new BlockingCollection<object>();
            LocalLockItem = new object();

        public object GetLockItem(string str)
            lock (LocalLockItem)
                if (!lockItems.Any(li => (string)li == str))
                return lockItems.First(li => (string)li == str);

* Implementing the process required a list, but at the same time, adding items to list and reading them would make another critical zone, but fortunately, .net has a thread safe list called BlockingCollection which you can use with multiple threads.

How to use the code
You will need to create the container somewhere. If your critical zone is only inside a class, use it as a property of that class and make it static.
If you need to lock different areas in different classes for a key, implement a singleton design pattern (which can be found in here) and return the lock container. Then you can use it easily:
  LockContainer locks = new LockContainer();

void IncreaseAndSave(int key)
       var value= ReadValueForKeyFromDB(key);
       // The thread1 goes to sleep


Friday, April 8, 2016

Using Exceptions, good or bad?!

Last year when I started to discuss this matter with my friends, I was almost sure that they all love exceptions, but I've found out that I was wrong! So lets could be the drawback of the exceptions and shall we use them or not.

First, lets say an exception is like a sword. You can use it when something happens to protect your code from unwanted results, but at the same time cut you if someone throws it and no one catch it.
Also, no one will use a sword to peel an apple! will you?! :D

Well, I myself cannot imagine how hard it would be to write a clean code without exceptions. :) You will prevent unwanted effects with only one line of code. There is no need to return message, value, etc. it could be logged very easily and your log can contain every detail that you may need. specially the call stack! By using typed exceptions (who won't?!) you can choose different reactions and by using inheritance, you can have even more control.

handling lots of exceptions is hard. You have to have a good idea about what is going on in your project. As I said, it is a sword, if you use it in a small code, like throwing an exception and handling it inside a class (or even worst - inside a method :| )
If you don't know what you are doing, you will most probably end up with a very ugly code that will be very hard to maintain

Where to throw an exception

An exception has to be thrown in these cases:
1- When there is a special situation in your code.
Think of normal scenarios that you've seen until now, like when memory is not accessible, network is down or etc. These situations have a meaning in their criteria, but at the same time they will tell make the user (code) to handle them properly.It is important to notice that it is not only connected to a low level code, when your code is about bank accounts you can have exceptions like "the balance cannot be negative" or "insufficient funds", etc. or for a method that handles shipment it can be "Product already shipped" or "The stock is empty", etc.

2- Where your domain ends as a wrapper
Basically speaking, you may need to have lots of different events in your code, but in many cases you don't want to return the complete details to the one who consumes your methods/services/package. For instance, you may have different rules in a banking system that you need to manage internally, but when there is a relation to other system, it just needs to know that an exception from "Specific Type" happened, and maybe a message and a code for the reference, there is no need for additional data.

Where to handle Exceptions
Well this one is obvious when you know the first part,
Of course on any outer edge of your domain/service you can decide if you want to take an action on your specific exceptions. If you have created an exception to stop the whole system, then you have to let it go, otherwise you have to handle the exception and take a suitable action.
A long sword can only handled by a strong man while even a girl can handle a short and light sword.

Also, if you are interacting with a human, you have to make sure that no exception reaches him. You just need some messages as a hint/error for the customer, and nothing more.

EPiServer: Your changes in code won't take effect?! Check the F@#king Version in your branches!

Yesterday I had a lot to do and therefore I had to switch between a lot of different branches. When I was working on one of my tasks I've found out that my changes doesn't take effect on EpiServer. To be more specific, I had a field with a UIHint before, then my UIHint didn't cause any behavior in the UI! It has been changed to a simple short string field.

To find the problem I tried adding a new property, but surprisingly my new field didn't even show up. So I thought maybe something is wrong with my web-server, temp files or etc. I tried restarting my IIS, deleting my temp files everywhere and then restarting IIS again, and after all restarting my windows! But none of them worked!

But again I've got surprised since even restarting the windows didn't work for me. So I post a question on EpiServer and thanks to Per Gunsarfs I understand what was the problem.

It was pretty simple! Your changes won't take effect if there is a newer assembly version available!

I just needed to change my assembly version to the newest that we have. (right click on project> properties> application tab> assembly information button) or as I did, update your branch with the parent branch (or master) if it has been set to the latest.

Easy? Right?! But just if you know it!