Feb 9, 2008

sealed class usage

We recently started an internal academy in our company to improve our software development skills. The first meeting was about OOP fundamentals, it was really interesting (thanks Vesko for your efforts!). This question appeared as interesting for me - when should we use the sealed class.

Some collegues guessed it should be used in the most concrete classes, as they will not be used as a base classses, but I don't think so. After all - why should you say : "Hey, don't use this thing as a base class" explicitly after you know it will not be used anyway.

I did soem research on the net about the sealed class usage, it appeared it's common use is for classes with static methods. The example I've found was about Brush and Pen .NET
objects. They are sealed as they have only static fields like : .Blue and so on.

So when you are doing software you may keep in mind that you have that sealed thing to help you limit the inheritance of your classes.

Feb 6, 2008

Is Microsoft the source of all evil or what?

It is very strange, I was searching for a migration tools, just to see how this market is going. I accidently found this thing here:




What's so strange? Some people choose weird domains to run business under. The strange thing happened after I clicked on the link to see what's behind:



Finally I decided to type the url address in my browser to see actual site behind thesource.ofallevil.com and see where it redirected me:

>

Feb 4, 2008

5 minutes C# code to load CSV file into a DataSet


Step 1 : add button for browsing files, a textbox to hold the file name and paste the followin code in the browse button click handler (this code will basically popup a OpenFileDialog, set up to only open files with .csv extension):

             OpenFileDialog ofd = new OpenFileDialog ();
            ofd.Filter =
"CSV Files|*.csv" ;
            
if (ofd.ShowDialog() == DialogResult .OK)
            {
               
this .textBox2.Text = ofd.FileName;
                GetData();
            }

Then we need to get the file from the textbox2 and load it into data set:

      
private void GetData()
       {
            
FileInfo fi = new FileInfo (textBox2.Text);
            
OleDbConnection con = new OleDbConnection ( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fi.Directory.FullName + ";Extended Properties='text;HDR=Yes;FMT=Delimited'" );
            con.Open();

            
DataSet dsThings = new DataSet ();
            
OleDbCommand cmd = new OleDbCommand ( "SELECT * FROM " + fi.Name, con);
            
OleDbDataAdapter adapt = new OleDbDataAdapter (cmd);
            adapt.Fill(dsThings);
            
this .dataGridView1.DataSource = dsThings.Tables[0];
            
       }

As you can see we use OleDB provider to load the CSV file into the dataset.

Jan 28, 2008

Job Done!

I think I made it finally. The osCommerce 2 vShop import was made this afternoon. Just waiting QA test now.
>

Jan 22, 2008

.NET C# to parse MySQL queries? Impossible? Pavel can make it ;) (with some ifs and buts offcourse ;)

The most interesting task in my career came the end of the last week. As a Vizibility employee I was asked if I can port osCommerce live site to out vShop solution (http://www.vizibility.co.uk/tmenu/vshop.asp) The interesting thing is that I had no access to osCommerce nor did I ever worked with it. What's even more chalenging - osCommerce database was given to me as SQL queries, something like :

drop table if exists table ;
create table table (
-- Some things here ...
);

insert into table () values ( );

As a normal software developer so there were 3 big chalanges:

1. How to parse this SQL thing (it is MySQL) ?
2. How to figure out
the osCommerce Logic ?
3. How to port all those things as a script so it can be applied to our live servers by the support team?


It's really chalanging don't you think? First I thought it will be a good step to install fresh copy of osCommerce, import the products and try to port them as CSV or something. But then I understood it will take too much time, so I decided to parse the SQL queries on my own and store them in a predefined objects like tables, rows and so on and so on. Then I could easilly transfer them as a text files in the format we needed.

And I almost made it - the parsing methods are done (I was able to parse this thing in a day but some bugs still appear).

Seems the more I work on our shopping solution the more easier I can understand other's shopping solutions.

Great task in a great company!

Jan 21, 2008

5 minutes C# code to open Web.Config from Windows Forms application.

In my previous post about IIS, Virtual Directories and Directory Services you saw how can you obtain the sites per IIS instance as well sa all the root virtual directories. Now it is time to show you how can you obtain the Web.Config file for a root directory.

At first glance I thought it will be easy, I thought I will simply need declare the following:

using System.Web.Configuration;

And / or :

using System.Configuration;

Well it's not quite this way. In order to load web.config file from a windows forms application you need to have some kind of context to be used or, in other words, file mapping.

The following method takes two parameters : path to the root directory and DirectoryEntry, representing the root directory
object (you can see my previous post ( Get IIS sites and virtual directories with .NET / C# / Directory Services ) learn how to obtain DirectoryEntry for a web root directory.
And here is the method:

      
List < string []> GetWebConfigAppSettings( string Path, DirectoryEntry entry)
       {
            
List < string []> items = new List < string []>();
            
if (entry.Properties[ "Path" ].Value != null )
            {
               
FileInfo fiWebConfig = new FileInfo (entry.Properties[ "Path" ].Value + @"\web.config" );
               
if (fiWebConfig.Exists)
                {
                  
WebConfigurationFileMap fm = new WebConfigurationFileMap ();
                   System.Collections.
Hashtable props = new System.Collections. Hashtable ();
                  
foreach (System.DirectoryServices. PropertyValueCollection property in entry.Properties)
                   {
                        
string propertyName = property.PropertyName;
                        props.Add(propertyName, property.Value);
                   }
                  
VirtualDirectoryMapping mapping = new VirtualDirectoryMapping (fiWebConfig.Directory.FullName, true );
                   fm.VirtualDirectories.Add(entry.Properties[
"AppRoot" ].Value.ToString(), mapping);
                   System.Configuration.
Configuration cfg = System.Web.Configuration. WebConfigurationManager .OpenMappedWebConfiguration(fm, entry.Properties[ "AppRoot" ].Value.ToString());
                  
foreach ( KeyValueConfigurationElement elmt in cfg.AppSettings.Settings)
                   {
                        items.Add(
new string [] { elmt.Key, elmt.Value });
                   }
                }
            }
            
return items;
       }



The method above will return a List of all in the web.config file (the appSettings section), you can easilly adjust it to fit your needs.

As you can see the magic begins in :

> System.Configuration. Configuration cfg = System.Web.Configuration. WebConfigurationManager .OpenMappedWebConfiguration(fm, entry.Properties[ "AppRoot" ].Value.ToString());


method. You simply need to make some preparations befor ask it to open the web.config.

Note : this code again is written in few minutes and does not pretend to be optimized, you can again use it at your own will.

Jan 17, 2008

Interesting Apple Ad

Recently I came across this link. It's amazing how creative the Apple marketing team is and how they exploit the Apple vs. MS war.

Have fun :)
.

Jan 13, 2008

How much does your site costs?

Recently I cam accross this site : http://www.smartpagerank.com. What impressed me was that this site can estimate (who knows how exactly) how much does your site costs.
Unfortunatelly my blog was estimated to 0$ but that's subject to change :P. I will also put this code into the left side of my site. You can check it out either.
>

Another 5 minutes with C# to post to Google Blogs (Blogger.com) using Google Blogs SDK (2)

I revised the tool a bit, here is how it looks now:





Also I made some refactoring to the code and it looks better now (no more richTextBox1 names ;).
I plan to release this tool in order to help developers blog code to their blogger accounts easier. The next secret I am going to reveal is that I started a toolkit to help you create blog related programs with C# / .NET / Google Blogs API.

For now I am planning to release the program for free, as a Lite version and I plan to spend some more time to create a better looking version with SEO features in it and distribute it for a small fee (something like 10 EU).
The Toolkit will released for free now.

Well that's it. I am looking forward when I will have some more time to develop those
controls and programs to help the Blogger community grow ;)

Jan 10, 2008

5 minutes with C# - Get your blog in TreeView Control

I played a bit more with the Google Blogs Api and was able to create a custom TreeView control to help you get your blog in Hierarchical View.

Here is a screenshot of the TreeView:



The control will be soon released to my site and new blog post will be published to let you know
.

Another 5 minutes with C# to post to Google Blogs (Blogger.com) using Google Blogs SDK

             // Create service to be used for queries
             Service service = new Service ( "blogger" , "blogger-sample" );
            
AtomEntry createdEntry = new AtomEntry ();
            
// Create credentials you should add your blog username
             // and password here
            service.Credentials = new GDataCredentials ( "your_username" , "your_password" );

            
// As I only have one blog to speed up - I am taking it,
             // You can extend here in order to allow the user to
             // select from mutliple
blogs if he / she has.
             Uri firstBlogUri = GetFirstBlogUri(service);
            
            
// If we have the Uri - it's time to actually post.
             if (firstBlogUri != null )
            {
               
// Create FeedQuery and assign Uri.
                FeedQuery query = new FeedQuery ();
                query.Uri = firstBlogUri;

               
// Create Atom Feed from that query.
                AtomFeed feed = service.Query(query);

               
// Create atom entry for the new post
                AtomEntry newPost = new AtomEntry ();

               
// Fill in the post information
                // You need to have a control named richTextBox2 which will
                // contain the post body and a textBox1 control which will
                // contain the post title.

                newPost.Title.Text = this .textBox1.Text;
                newPost.Content =
new AtomContent ();
                newPost.Content.Content = richTextBox2.Text;
                newPost.Content.Type =
"html" ;
                newPost.Authors.Add(
new AtomPerson ());
                newPost.Authors[0].Name =
"Pavel Donchev" ;

               
// Try to publish and report success / fail:
                try
                {
                   createdEntry = service.Insert(query.Uri, newPost);
                }
               
catch ( Exception ex)
                {
                  
MessageBox .Show(ex.Message);
                }
               
if (createdEntry != null )
                {
                  
MessageBox .Show(newPost.Title.Text + " posted successfully!" );
                }
            }

There is also a helper function to retrieve the blog Uri:

      
Uri GetFirstBlogUri( Service service)
       {
            
FeedQuery query = new FeedQuery ();
            
// Retrieving a list of blogs
            query.Uri = new Uri ( "http://www.blogger.com/feeds/default/blogs" );

            
AtomFeed feed = null ;
            feed = service.Query(query);
            
AtomEntry entry = feed.Entries[0];
            
for ( int i = 0; i < entry.Links.Count; i++)
            {
               
if (entry.Links[i].Rel.Equals( "http://schemas.google.com/g/2005#post" ))
                {
                  
return new Uri (entry.Links[i].HRef.ToString());
                }
            }
            
return null ;
       }

Jan 8, 2008

5 minutes C# code to help you blog source ...

As I was unabled to find a reliable editor to convert my code samples to html, I decided to write one. You need two richTextBox components, named richTextBox1 and richTextBox2 and a Button, named button1.

Here is the code you need to put into the button1 click event handler:
       private void button1_Click(object sender, EventArgs e)
       {
            
string oldColor = "-";
            
string newColor = string.Empty;

            
StringBuilder strHtml = new StringBuilder();

            
for (int i = 0; i < this.richTextBox1.Text.Length; i++)
            {
                richTextBox1.Select(i, 1);
               
newColor = System.Drawing.ColorTranslator.ToHtml(richTextBox1.SelectionColor);
               
if ((newColor != oldColor) && (newColor != string.Empty))
                {
                  
if (!oldColor.Equals("-"))
                   {
                        strHtml.Append(
"");
                   }
                   strHtml.Append(
" + newColor + ";\">");
                   oldColor = newColor;
                }
                strHtml.Append(richTextBox1.SelectedText.Replace(
"<", "<").Replace(">", ">"));
               
Application.DoEvents();
            }
            strHtml.Append(
""
);
            
StringBuilder sbNew = new StringBuilder();
            sbNew.Append(strHtml.ToString().Replace(
"   ", "   "));
            richTextBox2.AppendText(sbNew.ToString());
       }


PLEASE NOTE: this code was written in 5 or 10 minutes, it doesn't pretend to be either clean or readable, it simply works. If you want - you can take it, optimize it, use it or whatever, just have in mind it wasn't written carefully ;).

Dec 28, 2007

Get IIS sites and virtual directories with .NET / C# / Directory Services

       public List GetSites(string serverName)
       {
            List Nodes = new List();
            CustomTreeNode tnResult = new CustomTreeNode();
            try
            {
                DirectoryEntry w3svc = new DirectoryEntry("IIS://" + serverName + "/w3svc");
                foreach (DirectoryEntry entry in w3svc.Children)
                {
                   if (entry.SchemaClassName == "IIsWebServer")
                   {
                        CustomTreeNode Site = new CustomTreeNode(entry.Properties["ServerComment"].Value.ToString());
                        this.ServerLoadStart(entry.Properties["ServerComment"].Value.ToString());
/>                        Site.CurrentEntry = entry;
                        Site.ImageIndex = 1;
                        Site.SelectedImageIndex = 1;
                        Site = GetVirtualDirectories(entry, Site);
                        Site.ImageIndex = 0;
                        Site.SelectedImageIndex = 0;
                        Nodes.Add(Site);
                   }
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
            return Nodes;
       }
       private CustomTreeNode GetVirtualDirectories(DirectoryEntry site, CustomTreeNode Parent)
       {
            string VirDirSchemaName = "IIsWebVirtualDir";
            DirectoryEntry folderRoot = site.Children.Find("Root", VirDirSchemaName);
            foreach (DirectoryEntry deChild in folderRoot.Children)
            {
                CustomTreeNode tnFolder = new CustomTreeNode(deChild.Name);
                this.VirtualDirectoryStart(deChild.Name);
                tnFolder.ImageIndex = 0;
                tnFolder.SelectedImageIndex = 0;
                tnFolder.CurrentEntry = deChild;
                tnFolder.ImageIndex = 1;
                tnFolder.SelectedImageIndex = 1;
                Parent.Nodes.Add(tnFolder);
            }
            return Parent;
       }

Here is the result I get after few more hours of work:


Aug 28, 2007

The silent catch

Another of my interesting phrases (or at least I don't know if someboddy used this phrase). What is silent catch?

Silent catch is a term I use for try .. catch block, where the catch does nothing or at least you don't see it to be doing anything.

The first example:
try
{
some unsafe code here ...
}
catch {}

Another example:
try
{
some unsafe code here ...
}
catch (Exception ex)
{
hide something (for example grid or some other control) ...
return;
}

The second example is as bad as the first. It will be underlined by the Microsoft Visual Studio code editor to warn you that the ex object isn't used anywhere within the body of the catch statement.

Today I spent about two hours to investigate why a page doesn't display
one Grid. It appeared to be beried deep inside the system (I mean it was in an ascx control). The problem appeared to be missing view in the database. Unfortunatelly after I couldn't see the exception I was forced to start from the web user interface, first I checked the settings in the application to see if it is not forbidden or something, then I started to debug, to see where this part of the page comes from. After I found it, I needed to see what's going on in the control. So it took me two hours to trace the problem. What if there were an exception with stack trace? 15 minutes of work would have been enough for me to see that the view is missing.

So:
"Try to avoid silent catch statements as they may cost you a lot of efforts to find the problem later (even if you wrote the code on your own)."

Aug 23, 2007

Settings, they are supposed to help ?!?

Yesterday I had a tricky problem. I client of our company complained he can't upload files on our system. I started troubleshooting the problem. Each time I clicked the "Continue" button exception was thrown as if an assembly can not be found. It was pain in the neck. The assembly was something like "System.Web.UI.MobileControls.Adapters" (I can't remember the exact name now but it doesn't matter as we didn't use that assembly at all). I asked a collegue of mine for help (thanks Vesko!). The problem was in Machine.config file so we decided to back it up and comment that provider to see what will happen. It threw an exception, but this time more reasonable. It said there is no disk space. I logged again onto the server to free some more space
and gues what? There were 60GB of free disk space! I started to think where the problem might be and tried to find solution in Google. It finally appeared that the problem is a setting within the OS. Short explanation below:
Windows 2003 Server uses NTFS file system. This file system is capable of maintaining disk quotas for each user. In other words - the administrator may define how much disk space each user can use. It was set to 1 GB for the user, running the service responsible for the upload. So I set it to 3 GB, tested to see if it works and it worked. Then I left the server and asked the administrator to decide how much disk space will be available to the user running the process.

After this space is used again I will know what to do ;).

So I learned very useful lesson - first check the settings and then try to debug or touch the code. In most cases if the code worked before and you know nothing has changed - this means that some resources available to the application before aren't available now OR someboddy changed some setting which was important for your code to run smoothly.

Aug 9, 2007

Surface Fixing



What is surface fixing?


Surface fixing is a phrase I use for a fix, which is implemented in a layer above the layer where it occurs. If we imagine the standart 3-tier architecture and we have a bug in the Data Access Layer, surface fix will be to implement the fix in the Business layer or the Presentation Layer. It is common for junior developers or companies where the things should happen in a matter of hours or days (companies with short deadlines).



Example of a surface fixing:





Let's say we have a page for registering users (Presentation Layer). On user registration this page creates the User object (Business Layer). Sets its properties as necessary, and calls it's Save() method. The Save() method creates

an instance of the UserDAL object (Data Access Layer) and decides whether the task is to create new user or update existing one. Let's say we will create new user. The User Business Object -> Save() method will probably call something like : UserDAL -> InsertUser(long UserID, string UserLogon ....);

Now let's say that the Data Access Layer doesn't escape the single apostrophe in the UserLogonID. Passing parameter as O'Conner or something like this causes exceptions. The question is where should this be fixed?

I see most of you answering : "In the Data Acess Layer offcourse!". That's the best place but if you are working on a small project.

Now ask yourself what will happen if we fix this in a big enterprise system with hundreds of modules / pages referring to that particular routine? After all the other modules / pages are working they will stop working or at least stop working correctly. With other words - your fix will require the whole system to be redeveloped. And what's worse - the QA team will have a lot of work. They will love you for sure :). And to make the things even worse we will imagine that we are near the deadline (which is almost sure after we are testing already). In such cases the Surface Fixing may help.

Below I am giving the advantages and disadvantages of the Surface Fixing.

Advantages:

  • Your fix will not broke anything in the existing modules
  • The fix will only be tested, not the modules which may be affected
  • Ideal to use near deadlines

  • Disadvantages:

  • The fix isn't applied at the right place
  • Conflicts may appear in the future with modules which are working correctly
  • Against the official software development standarts


  • When to use this approach ?

  • When we are developing module in a big system with lot's of other modules which depend on the bug
  • When we are near deadline
  • When we are sure that there will be new version.


  • Conclusion:

    This approach is perfect to fix some bugs in the current version, but consideration to completely fix it in the next versions should be made. It simply is not right to use this approach as a regular fixing guidance. In the software development and the 3 or more tier models the above layes should only see the previous and depend on it. And what's more important - the above layer should get the data as expected.

    Jul 8, 2007

    New company - new luck :)

    Yep, only in few hours I will be on my new desk in my new company :). I am very excitet about this. The new company is Vizibility and it is developing web modules. At first glance it seemed very very interesting with all the young people there. They were very hospitable on the interview and managed to earn my complete confidence. It seems that I did that too. After I met their requirements they met mine too.

    So it is a big day today :). Wish me luck
    !

    Jun 30, 2007

    Skype API (Skype4COM) + Google MAPS API = ? [3]

    As a part of my Google Maps and Skype API I wanted to add custom icons to represent different objects on the map (currently I made it for the bus stations).

    The picture bellow is the result :



    It was again easy - we simply create GIcon object. Then we assign the URL of the image to be used, size of the icon and point, where the info window will appear :


    Code


    icon = new GIcon();
    icon.image = 'BusFine.gif';
    icon.iconSize = new GSize(39, 60);
    icon.iconAnchor = new GPoint(5, 34);
    icon.infoWindowAnchor = new GPoint(5, 2);




    Then we only need to add this object as a second parameter in the GMarker object constructor.



    Code


    // point is a GPoint object, representing the location the marker
    should be
    // displayed.
    marker = new GMarker(point, icon);



    Note : Some browsers may need some more settings in order to display the custom icon correctly, the example above is for IE.

    Jun 18, 2007

    Skype API (Skype4COM) + Google MAPS API = ? [2]

    The last few days I played a bit more with this little thing and made it a bit more stable.  First of all there were some strange silver rectangles in the map (which I suspect is becouse I use IE) another struggle was to force the map not to resize (pretty strange, huh?). I solved both issues at once by adding overflow="hidden"  style attribute to the div, containing the map. It is quite strange.  I also added the stnadard map types controls (Map, Satellite and Hybrid), that wasn't hard but took a lot of search in the net, I had the feeling that I need to define my layer, I don't know why but in the end I understood I only need to call : map.addControl(new GMapTypeControl()); it simply cannot be more easier. After the lesson with
    the map type control it was a line of code to add the large zoom control : map.addControl(new GLargeMapControl()); the guys from Google really rule the world :). They made it as easy as possible. After I got the map types and the zoom control I wanted some challenge. I decided to add a tabbed info window, only for the test the first tab I wanted was : "User Info" and I wanted the second to be "Date of birth" that was again as easy as possible ! Nice work, googlers !

    Finally here is what I have so far :


    I will try to add custom layers and lines these days only to test how it works and is it hard to implement.

    Jun 10, 2007

    What happened with the Data mining?

    Well, I simply stopped the project for a while, since I need to see the legal aspects of it. I am not quite sure if it is legal to simply take some catalogs from somewhere, reformat them and use them as your own. I think I can do such thing with some free public catalogs, but I will not act, until I have the written permission from the authors. Here are two screenshots of the spider in action :



    The above screenshot is when the spider is gathering data. It displays how much data is gathered and what the spider is doing in the given moment.



    The above screenshot displays the results of the crawl as a treeview. It uses MS Access database to store the data (since it is a small project).

    Technologies used for the spider :
    />.NET 1.1 (Windows Forms), MS Access, Internet Explorer Automation COM Libraries (AxWebBrowser, ShDocVW COM mshtml).