Показват се публикациите с етикет Software Development. Показване на всички публикации
Показват се публикациите с етикет Software Development. Показване на всички публикации

Java2Days - a brand new event in Eastern Europe!

понеделник, 31 август 2009 г.

Hi guys!

I am very much exited to share with you the news of a coming Java event which we are preparing these days at NASD along with our partners from Insight_technologies – Java2Days!

Java2Days conference is a brand new event in Eastern Europe to present the latest trends in Java development.

Java2Days will be held at the Inter Expo Center on 8-9 October in Sofia, Bulgaria. The conference is hosted by Insight_technologies and the Bulgarian National Academy for Software Development.

The conference is the first of its kind to be held in Eastern Europe, focused to highlight today’s cutting edge trends in building software applications with Java development tools.

Over two days, more than 600 attendees will meet world famous lecturers, engaged all year round in such events as JavaOne, The ServerSide Java Symposium, Jazoon showcasing their latest knowledge in creating more reliable, scalable and secure solutions using Java technologies in more than 20 technical sessions.

The major purpose of the event is to become a place for passionate Java developers to get in touch with the latest technologies, to become a significant part of the global Java community and to learn from the best.

You can see everything about the conference at its official site.

P. S.
If you are going to attend at the Java2Days event, don’t hesitate to contact me – we can have a beer or two talking about everything which bothers you in your work as developers!

How to Send SMS under Windows Using Connected to the PC Phone with Datacable/Bluetooth/IrDA/PCCard/etc. in C#

петък, 24 юли 2009 г.

Intro

Today I had to solve a very interesting task for one of our projects at NASD – sending SMSes trough .NET and C#. In the past I have used different kinds of services for sending SMSes – SOAP web services, Email2SMS services, etc. The usage of the above methods is very straightforward, doesn’t require any special knowledge and the task is practically a trivial. Our application required sending of very small number of SMSes, so we have thought of another approach – to connect in some way (Datacable/Bluetooth/IrDA/ PCCard/etc.) a physical device to the PC and to send messages trough it.
In this post I will show you ready to use approach to send SMSes from a connected to a Windows PC GSM device that supports SMS sending.

The Possibilities
In general there are two possible approaches for solving the task. The first one is by consuming some API or already written library trough C#/Java/etc. I started with the idea of using Java and J2ME for developing the application. However as a C# and .NET fan, I decided firstly to research if there is something .NETish which I could use. A lot of comercial COM libraries could be found on the market, which therefore can be consumed from almost every Windows based language/development platform.
The other main possibility was to find some command line enabled tool, which could be wrapped by a C#/Java/etc.
After a short research my conclusion was that non comercial library which works could hardly be found. So I started looking for the other possibility – a command line tool. But what happened?

Solution
I was very happy to find something called SMSSender provided by Microsoft. SMSSender is an add-on for Microsoft Windows XP that will create and send SMS (short text messages) by using your GSM cellular phone. It also allows the use of all characters from international alphabets. SMSSender could be used under Windows Server 2003 and Windows Vista (with setting to run it in compatibility mode for Windows XP), too. The application has a very simple and userfriendly UI and can be used easily as a standalone application.

To send an SMS firstly you should select your already connected device from which you want to send the SMS. Then you need to input the destination number and of course the content of the message itself. When ready, you can click Send. If everything is OK, you will see the following message box:

On the other hand – if something fails you will see message box, simmilar to this one:

After a minute you will find why I am talking about this message boxes at all.

The Command Arguments
As I said above, the SMSSender application is command argument enabled application. The arguments which we are interested in are the /p:phone_number and /m:”sms_body”. Here we should ask ourselves what happens with the third option – the device selection. Unfortunately the tool doesn’t have argument for device selection. If we don’t provide such the application argues that no device was selected. But how we could select this option? There are two ways possible was to do the default device selection – the first one is to set the registry key HKEY_CURRENT_USER\Software\Microsoft\SMSSender and add new string value called DeviceName with data field equals to the name of your device (in my current case LGE Mobile USB Modem or in other words the value from the dropdown above). The alternative way is just to enable logging (going to Options and check the Enable Logging checkbox).

The SendSMS Wrapper
OK we are ready to implement the wrapper. The task seems very straightforward – something like 10 to 15 minutes – start the process, hide its window, wait the process to end, etc. But is there something which could be a problem for us and make the writing of such application more challenging? If you remember, when clicking the Send button (which is equivalent to start the application with the appropriate command arguments) there was a message box which waits one to click OK(no matter successful or not). Yes.. I know such a behavior seems very developer unfriendly with this pup up messages. When looking for a setting/argument to disable these alerts, I wasn’t able to find such. So I had to put my past Win32 experience in practice again and had to close this message box with code.
I’ll explain this part of the code in short – after starting the process of the SMSSender, I start a thread, which polls for a window with a specific caption (using the native Win32 function FindWindowEx) with a given delay. After finding either the successful or the failed message box, it sends closing message to this window (again with a native Win32 function, this time SendMessage) and prints to the console the whether the message was sent or not. After this the application ends.

The Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

namespace SendSMS
{
class Program
{
#region Win32 API Import

public const int WM_COMMAND = 0x0112;
public const int WM_CLOSE = 0xF060;

[DllImport("user32.dll")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, uint Msg,
int wParam, int lParam);

#endregion


#region Methods

private static void ShowHelp()
{
Console.WriteLine("Vesko Kolev's SendSMS usage:");
Console.WriteLine("SendSMS phone_number \"sms_body\"");
Console.WriteLine("SendSMS 0812345678 \"Test sms body!\"");
}

private static void CloseMessageBox()
{
IntPtr hwndMessageBox;
while (true)
{
hwndMessageBox = FindWindowEx(IntPtr.Zero,
IntPtr.Zero, "#32770", "SMS Sender");
if (hwndMessageBox != IntPtr.Zero)
{
SendMessage(hwndMessageBox.ToInt32(),
WM_COMMAND, WM_CLOSE, 0);
Console.WriteLine("SMS sent successfully!");
break;
}

hwndMessageBox = FindWindowEx(IntPtr.Zero, IntPtr.Zero,
"#32770", "SMS Sender Error");
if (hwndMessageBox != IntPtr.Zero)
{
SendMessage(hwndMessageBox.ToInt32(),
WM_COMMAND, WM_CLOSE, 0);
Console.WriteLine("SMS sending failed!");
break;
}

Thread.Sleep(500);
}
}

private static void SendSMS(string phoneNumber, string smsBody)
{
Process process = new Process();
process.StartInfo.FileName =
Path.GetDirectoryName(Application.ExecutablePath) +
@"\MSSMSSender.exe";
process.StartInfo.Arguments =
string.Format("/p:{0} /m:\"{1}\"", phoneNumber, smsBody);
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();

Thread thread = new Thread(new ThreadStart(CloseMessageBox));
thread.Start();

process.WaitForExit();
}

static void Main(string[] args)
{
switch (args.Length)
{
case 2:
SendSMS(args[0], args[1]);
break;
default:
ShowHelp();
break;
}
}

#endregion
}
}

Conclusion
In this article I have showed you a ready to use source code which sends SMSes from a connected to your PC GSM device. You can consume this app virtually from anywhere – just wrap the wrapper and forget about this task!

I’ll be happy to hear your comments about everything related.

Thanks,
Vesko Kolev

Vesko on Performance – LINQ to SQL

четвъртък, 23 юли 2009 г.

Intro
In general some of the biggest performance problems of the applications seen nowadays are found in the database or the data access layer. This combined with ORM framework such as LINQ to SQL which is new for the development team, which encapsulates all of the queries made against the DB (and “of course” not profiled), which is not researched enough, could lead to very “sexy” results. Here I will try to show you a few common mistakes which I have seen through my everyday work with different developers from all over the world as well as the one that our students here at National Academy for Software Development make. At the end of this article you should know how to avoid such mistakes (not only the one that I’m showing here). I will provide you with the toolset along with the practices which do the work for me every day, and will hopefully do the work for you, too!

The Problems
1. GetAll() and GetCount() methods – this is a classical pair of methods. As you know they can have different variation such as GetAllBy[Something]() and respectively GetCountBy[Something](). I have found a lot of developers do the following:

But where is the performance problem? When somebody calls GetAll() the code just retrieves all the SampleObjects from the DB. This seems to be OK and it actually is. But what about the GetCount() method? It firstly fetches all of the SampleObjects in memory and then counts them. As you know the expected way of solving this task is to use the COUNT function in T-SQL. Of course the idea behind this GetCount() is simple – the developer tried to reuse the query defined in the GetAll() method. One would say – “Hm… I thought that LINQ to SQL executes everything possible in the SQL server. Why here it doesn’t do this?!?” The problem is that when you build the expression (assigned to the result variable) it returns value from IQuerable. Up to now this expression is just an expression – it doesn’t execute anything against the SQL server. However on the next line we have a call of the ToList() method which idea is to return list of SampleObjects. Thus here on this line we have a DB query which fetches the requested list.
Here we should solve two problems. The performance one is obvious – we fetch everything from the DB just to count it, which will impact the memory usage as well as the network traffic. The second problem is that we want to stick up to the Don’t Repeat Yourself principle (DRY, also known as Single Point of Truth and Single Point of Maintenance) – we don’t want to have the query for the GetAll() and the GetCount() twice. In practice it will differ only by the Count() method. Keep in mind that we could have more complex queries with filters, skips, orders, etc. so this could be very bad if we don’t obey the DRY principle here. The above snippet tried to solve this second note, but in a wrong way.
The following code snippet shows sample solution for both of the mentioned problems:

It just uses third method, which is private for the class and returns the common part of the expression as IQueriable. The two public methods just appends the query with a ToList() and Count() calls.

2. SelectMethod(…) and SelectCountMethod(…) for paging – this sample is similar to the above, but again is very common mistake.

This implementation totally breaks one of the main reasons of using paging – loading only a part of the whole content. You have enough knowledge from the point 1 both for finding the problem and for solving it.

3. The N+1 query problem – a nightmare from the past. This is a very performance intensive problem which becomes a classical in the software development folklore. The idea behind this problem is very straightforward – imagine that we have a list of SampleObjects and for each list we want to get which is his owner – SampleObjectOwner. Thus one could write one query for retrieving all of the SampleObjects (let’s say that their count is N) and for each object, we ask which is its owner. This means that we will make 1 + N queries for retrieving the needed data.
I will show you an even worst situation, in which we have to find the first object (if such exists) which have particular SampleObjectOwner as an owner.

The mistake here is similar to the above two, but here it’s the worst situation - the method GetByOwner(…) needs as much quires as the number of the sampleObjects + 1. This means that in the future, while your DB grows the performance will become worst and worst. The problem’s fix is so ironically easy – just compare…

The above code produces only a single query.

General advice
We have seen some problems, which weren’t found during the development process. The question is how to prevent ourselves from such things?
Firstly – one should definitely not use technology or framework in production without a good research. As we just have seen the problems have very straightforward and logical explanation. We should confess that not reading enough is a problem of many developers.
Second – as I mentioned in my previous post – use class like DebuggerWritter for profiling every method. Normally such problems could be found very easily just with one testing. Yes – you will need to have at least a vague idea about what kind of SQL statement should be composed by LINQ to SQL but this is a normal requirement for each software developer.
Third – stick up to the techniques shown in the solutions above. They represent a good pattern for writing DAL logic with LINQ to SQL.

Conclusion
Hopefully, now you should know more about LINQ to SQL and how to avoid some of the problems, which are commonly seen in the developers’ code. It will be very helpful to add your comments to that topic showing problems that you have had/seen in your work.

Thanks,
Vesko Kolev

Algorithms: A real world example

вторник, 12 август 2008 г.

Introduction
Hi guys!
Today we are going to dive into a real world example of using algorithms + data structures. The task is not very hard, but I think it is very useful example how important is one to have an in-depth knowledge of algorithms and data structures.

What is the problem?
These days a friend of mine asked me the following question: “How can we retrieve the different files (in the terms of different names) from given two directories, including the subdirectory files?” In other words the question is how we can get the files from one directory(or in some of its subdirectories) which doesn't exist in the other and the opposite - the files which are in the second directory(or in some of its subdirectories) and does not exist in the first one.
Yes… I know that you can solve this. You can do it even in 5 minutes using whatever data structure you want which has a contains method (or equivalent). But do you think that this approach is going to be the fastest one? Or a more scientific question – is your algorithmic complexity good enough for the needs of your enterprise application?
In general there is uncountable number of algorithmic tasks out there and most of them have more than one good algorithmic solution. I am going to show you a problem with a possible solution.

The algorithm
The approach is based on one classical algorithm. This algorithm merges two given sorted sequences in a new sorted sequence, which contains the elements from both sequences in linear time. On the following picture, you will see two sorted input sequences. The algorithm to get the result sequence is the following:


1)While both sequences are not empty - get the minimum of the two top elements and put it at the end of the result sequence. Remove the element from the input.
2)Now if there are any elements left in the input, they should be in only one of the input arrays. If there are some element left – add them at the end of the new sequence and remove them from the input.

In our case we first have 0 and 1. 0 < 1 therefore we put 0 in the new sequence. The first input sequence will be 3, 3, 15, 19, 76, 89, 99. We are doing this till the moment when the second sequence (which is shorter than the first one) is over. Than we just add the element 76, 89 and 99 to the end of the result sequence.

What is the modification?
You just need to get your both sequence of files in an ascending sorting manner. If the files on the top of the two sequences are equal, then just remove them. If the files are different, then take the one which is name is “less” in the terms of lexicographical order and put it at the end of the result sequence (Note: Here you should check whether the result sequence doesn’t already ends in this filename. Think of a situation in which this is required.). If one of the sequences is not empty, we get its entire elements and put them at the end of the result sequence again taking in mind the above note.

Which are the data structures?
As we saw, we access only the top elements in the input sequences. This is the major feature of the Stack data structure. The result sequence – we add elements at its end and need to peek the last added element. So we can use for example Queue or Linked List depending on the implementation which we are using.

Conclusion
In this post I have tried to show you one problem which can be solved optimally with an elegant algorithm. I have tried not to be bounded to any programming language and not to be clear enough, because want you guys to think of the rest! This is the way in which I believe one should learn to program – with logical thinking and researching.

Hope you learned something new!

Tricks: How to get N random rows from a table in T-SQL

петък, 18 юли 2008 г.

Hi readers!

What is this blog post for?

Today I am going to present you the first post of my second series - Tricks. As I said before I will show "Technology Specific Tricks - short posts about something, which I have researched and which I want to preserve and share with you all."
The first one is about T-SQL


The problem - How to get N random rows from a table?



A little explanation - actually ORDER BY NEWID() sorts the rows in a random way. In our case we just take as much of the first as we need.


Conclusion

You just saw what you can expect from this type of posts.
Hope this helps!

Basics: Why to use accessors and mutators?

сряда, 16 юли 2008 г.

What is this blog post for?

This is the first post from my new series – Basics. In this series I will add things which are (in my opinion) really important and should be perfectly known by every Developer/Senior. I will add topics which I have been asked for or things which I have expected to be known by the mass, but it wasn’t that case. I will try to be as much platform independent as possible. Of course I will add some general knowledge about new things in C# 3.0. I will try to be as much detailed as possible but will appriciate any help/comments on making things broader. No matter – lets go back to basics!
This post is for the so called accessors and mutators / getters and setters / get and set methods / properties in C# / etc. and why we need them at all?


The explanation

public class Person
{
public int age;
}


public class Person
{
private int age;
public int getAge()
{
return age;
}

public int setAge(int value)
{
age = value;
}
}

Firstly, accessors and mutators are used mainly for encapsulating fields of a class (an example for different usage can be given for storing information in the Session or ViewState). It’s not good practice to expose public fields (or why it is better to use getters and setters), because:

- If you use get and set method, you are able to control the access of the property. For example you can give public get and protected/private/etc. set.

- You can put any kind of validation in the set method. If there is a need you can throw an exception.

- The situation may need to set other dependant properties, too, when accessing/setting the current one.

- When you access the get/set method you can rise an event (for example OnValueChanged)

- You can convert the value which you return from the getter to more informationless data (for example you can have a value for a price and you want to return it with 2 digits after the decimal point, but in your calculations it should be as much digits as possible for more correct calculations).

- It is easier to bind against proeprties.

- If you expose public fields, later you cannot change them into properties without recompiling.


Still why?

And yet again – why to use getters and setters when we have just a simple integer field which we do not think will need any special work now? Why should I “loose” my time writing additional code? Because in our everyday life almost every time the specification is changed and almost every time there is a situation which we didn’t take in mind at the time of writing the code. In general in my opinion a good developer is the one that can write as much extensible code as possible, as much reusable as possible and as easier to change as possible nomatter which platform is used. The practice of using getters and setters gives you the flexibility to be the winner in the battle between you and the changing specification. It gives you a very simple rule of a thumb which makes your code keeping tight encapsulation.


Something in addition for C# 3.0

Yes – one of the greatest things in C# (compared to Java for example) is its properties. In Java you do not have such structure (as I have heard this is going to be changed in some of the future releases of Java. I will just ask - where are my get* and set* methods!?!?!). Most of the code one will see in an average Java program is a bunch of getters and setters (hopefully generated).
In C# we have the chance to work with this more clear structure called property. However in C# 1.1 there was a little problem – there was no way in which you can say “I want to have a public getter and a private setter”. The only ability was to say whether you want to have get and/or set with an one and the same access modificator.
In C# 2.0 we were given the access modification functionality for each of the two property parts. This was great. But there was still something which missed me all the time – I wanted to have a way to enforce everybody to use the property and not the field (in general this has sense if you think about the access of the field in the class in which it is defined). Here the C# 3.0 come one day and say “I can fix this!” The magic is called Automatic properties. See the following example which shows an automatic property:
int Age
{
get;
set;
}

As you can see we do not need to explicitly define field for this property. What actually happens is that when the C# 3.0 compiler see an empty get/set property it will automatically generate a private field for you. This means – less code to write and more to generate. Another effect is the fact that you cannot directly access the fields which solves the problem mentioned above.


Conclusion:

Just think of all the mentioned things in the above post. You as developer can only benefit from this. If you didn’t find any sense of using getters/setters, to be honest with you - just find a better field for you.

P.S. If you want to get more information on automatic properties and what happens under the covers you can go to Bart De Smet Blog.

Bulgarian is supported by Google Translate!

понеделник, 19 май 2008 г.

Yeeeeah... it's true! Goolge have recently added translation capabilities for 10 new languages! The new languages include Bulgarian, Croatian, Czech, Danish, Finnish, Hindi, Norwegian, Polish, Romanian and Swedish.

Finally Bulgarian is supported! What does this means to you guys? At least you can read my blog in Bulgarian (using the translator on the right side of my blog).

P.S. Martin - at last there is Bulgarian! :-)
P.P.S. You can find the full blogpost here.

How to create branch under SourceSafe in 5 steps?

сряда, 26 март 2008 г.

Hi guys!

A lot of people (including me of courseJ) had a lot of troubles when they had to create their first branch under SourceSafe. Why? In my opinion this program (talking about version 8.* of SourceSafe) has a lot of problems connected with the friendliness of the UI. Actually I think that it isn't user friendly at all. So here are my bits about solving one of the problems – branching. In the following lines I will provide an algorithm in 5 steps about how to do this activity. I do not know whether there is shorter way to do this but for me this is short enough and the most important thing – it works.

The algorithm is the following. I assume that you are in the database, where is the project you want to branch. Note, that the backward blue diagonals on white background cover some data, which is important only for me.

Step 1: Select the root of your DB, right click on it and select Create Project.




Step 2: Give your new project a name – this will be the name of your new branch. Click OK.




Step 3: Go to the folder you just created – right click on it. Select Share to $/ Your_Branch.




Step 4: Select the project which you want to branch. Check Branch after share (in the bottom) and click Share.




Step 5: You can select new name of the project, which will be branched in your new folder (the one which you created on Step 1). Check Recursive. You can add your Comment in the provided box and then click OK.




After these 5 steps you are ready to wait for your new branch. The speed of actual creating the branch depends on your project size, database, etc.

Hope this helps!

 
Vesko Kolev's Blog : IDeveloper -