Mihai Bejenariu Blog

<November 2008>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456


Navigation

Subscriptions

Post Categories



.NET stuff (RSS)

Design Patterns
Amazing how graceful Martin Fowlers can talk about patterns. I rediscovered with pleasure one of its books:

http://www.martinfowler.com/books.html.

 

Also, I would like remember here my favorite site about patterns:

http://www.dofactory.com/Patterns/Patterns.aspx

 

Happy design patterns :)

 

posted Monday, March 27, 2006 3:18 PM by Mihai Bejenariu with 741 Comments

NUnitForms - automate your testing

NUnitForms is an NUnit extension for unit and acceptance testing of Windows Forms applications.

Your NUnit tests can open a window and interact with the controls. Your tests will automatically manipulate and verify the properties of the GUI. NUnitForms takes care of cleaning up your forms between tests, detecting and handling modal dialog boxes, and verifying that your expectations for the test are fulfilled.

See below the first NUnitForms test. It shows a form, write a test in textbox control and then it clicks a button.

[TestFixture]
public class BatchSecurityTester: NUnitFormTest
{
            [Test]
            public void AddNewGroup()
            {
                        //Show the form
                        MyForm form = new MyForm();
                        form.Show();

                        //Enter text 'NewGroup' in txtGroupName control
                        TextBoxTester txtGroupName = new TextBoxTester("txtGroupName");
                        txtGroupName.Enter("NewGroup");

                        //Press btnOK control
                        ButtonTester btnOK = new ButtonTester("btnOK");
                        btnOK.Click();         
           
}
}

Automate testing is a powerful tool complementary to unit tests.
Happy (extreme programming) coding!

P.S. NUnitAsp is the correspondent for ASP.NET application.

posted Friday, September 02, 2005 8:41 AM by Mihai Bejenariu with 636 Comments

Database backup programatically (using .NET)

Today my boss asked me to add a new feature to the application I work on: to make a database backup every time the application is closed.

I was very surprised to do this in less then 1 hour using SQLDMO library which is shipped with SQL Server 2000.  The dll itself is a COM object and you must reference it from your .net project as such.  The IDE will create the necessary COM wrappers needed to use the library. 

 

                    using SQLDMO;

                      //Necessary declarations
                     SQLServer2Class server = new SQLServer2Class();
                     Database2 database;
                     Backup2Class backup = new Backup2Class();

                     //Connect to server
                     server.LoginSecure = true;
                     server.Connect(“server”, “user”, “pass”); 

                     //Select database you want to backup
                     database = (Database2) databases.Item(“database_name”, null);
             
       backup.Database = database.Name;               

                    //Specify the place where backup to be saved.
                     backup.Files = “C:\mydb.bak”;

                     //Effective backup of the database
                     backup.SQLBackup(server);

                     //Disconnect, don’t forget it
                     server.DisConnect();


As you can see, this is a much easier alternative when SQL information or control is needed.

posted Friday, August 12, 2005 3:05 AM by Mihai Bejenariu with 593 Comments

Get list of SQL Server databases
In this post I’ll present you 2 ways to get existing databases on a SQL Server.

First solution is to use sp_databases system procedure. Calling this procedure is made like calling any other stored procedure. Please find below the code snippet.

//Create and open the connection
SqlConnection cn = new SqlConnection(CONNECTION_STRING);
cn.Open();

//Create and execute the command
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_databases";
SqlDataReader myReader = cmd.ExecuteReader();

//Shows all databases
while(reader.Read())
{
    Console.WriteLine(reader.GetString(0));
}


The second solution is to use SQL-DMO library. As you may have been maybe expecting, the code is much simpler. You just need to connection to SQL Server, and use SQLServerClass.Databases property. Be aware to add reference to SQLDMO.dll. It might be found at the following path:

C:\Program Files\Microsoft SQL Server\80\Tools\Binn\SQLDMO.dll

SQLDMO.SQLServer server = new SQLDMO.SQLServerClass();
server.Connect(".","sa","sa");

foreach(SQLDMO.Database db in server.Databases)
{
        Console.WriteLine(db.Name);
}

posted Wednesday, July 27, 2005 3:46 AM by Mihai Bejenariu with 753 Comments

Comparing files
One way to compare files is to use hashing. A hash value (also called a message digest) is a number generated from a text. The hash is substantially smaller than the text itself, and is generated by a formula in such a way that it is extremely unlikely that some other text will produce the same hash value.

Please have a look to the following code snippet to see implementation of files comparing with hash mechanism.


using System.IO;
using System.Security.Cryptography; 
...
private bool AreFilesIdentical(string source, string target)
{
            //create the hashing object.
            HashAlgorithm alg = HashAlgorithm.Create();

            //Calculate the hash for the first file
            FileStream fsA = new FileStream(source, FileMode.Open);
            byte[] hashA = alg.ComputeHash(fsA);
            fsA.Close();
 
            //calculate the hash for the second file
            FileStream fsB = new FileStream(target, FileMode.Open);
            byte[] hashB = alg.ComputeHash(fsB);
            fsB.Close();
 
            //compare the hashes
            return BitConverter.ToString(hashA) == BitConverter.ToString(hashB);
}

Another solution would be to use file metadata information. But you can’t count on it every time. E.g. if you based on file time modification stamp, you might have surprises in case of file originate on system with different time zones.

I’m waiting your suggestions.

posted Thursday, July 14, 2005 3:53 AM by Mihai Bejenariu with 893 Comments

Refactoring
Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a 'refactoring') does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it's less likely to go wrong. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring.

Here it is a list of possible refactorings in Alphabetical Order. Read this carefully and don’t hesitate to refactor your code when you have the opportunity. http://www.refactoring.com/catalog/

ReSharper is a refactoring plugin for Visual Studio to support C# development. It's developed by the same people who built IntelliJ Idea for Java and brings much of its development style to C#. http://www.jetbrains.com/resharper/

posted Thursday, July 14, 2005 3:35 AM by Mihai Bejenariu with 754 Comments




Powered by Dot Net Junkies, by Telligent Systems