My custom Visual Studio’s environment settings



By ganton ~ November 19th, 2008. Filed under: Other.

I’m using non-standard Visual Studio environment settings and I’ve decided to share them with other developers on the web. I hope that everybody is familiar with how to import and export VS settings but for those who do not know I’ll write few words.

First of all you need to start your VS :). Then you should go to Main menu\Tools\Import and Export Settings…

Visual Studio Import and Export Settings Wizard will appear. You can follow wizard instruction and you will be able to import, export or restore defualt settings.

And now let me show you some print screens with different VS’ text editors and their look. Below you can see my XML editor settings.

The next screen shot depicts the settings of my HTML editor.

And below you can look at my settings of C# language text editor.

I also use the same line of colors and fonts in order to configure VS immediate window and find result window. Below is the find result window.

And now if you like my VS environment settings you can download them from here and use them. This package contains only my editors settings and will not change your toolbox or other windows position. It also will not change your text editor formatting settings. But I strongly RECOMMEND you to export your current settings before to import my settings in order to be able to restore your preferred VS state in the case you do not like my settings when you touch them.

Enjoy!

I have new theme applied to my blog



By ganton ~ November 18th, 2008. Filed under: Wordpress.

Some time ago, I saw that my blog doesn’t look well on browsers different from Mozilla. As it was clear it was because of the theme used.

Yesterday, I looked for a theme that I’ll like and that it will look more or less good at least on IE6, IE7, Mozilla and Chrome. And I choose Bytetips 1.6 by Jim. I changed the body background color and make it to have a fixed size. I also remove the image from the header for now. Few simple thingsĀ  but I like the theme like it looks on my blog. May be I should center it but I didn’t decide it yet.

Thanks to Jim for the nice theme. Well done Jim!

Enterprise Library 3.1 installation problem



By ganton ~ November 17th, 2008. Filed under: Other.

I needed to install Web Client Software Factory (WCSF) on one old machine which I decided to use for some tests. As far as WCSF needs Enterprise Library 3.1 I’ve decided to install it. And I ran into a problem.

Firstly, let me say what I have installed on the machine. OS - Windows XP SP, VS 2008, Guidance Automation Extensions (GAX) and Guidance Automation Toolkit (GAT) for VS 2008.

So, before to install WCSF I’ve tried to install Enterprise Library 3.1 and I got the error below.

The error says that some types cannot be loaded. That’s why I’ve started fuslogvw.exe from the SDK and tried to see what went wrong.

The last log record has information about the error that has occurred during installation. It was clear from the log that for some reason Microsoft.Practises.RecipeFramework.dll cannot be loaded.

Than what came into my mind was that it is somehow connected with GAT and GAX. Logically, I thought that I have installed newer version of both on my machine and may be it can be the reason. And I’ve tried to uninstall both GAT and GAX. After uninstalling them I’ve tried to install Enterprise Library 3.1 again. And it was installed without any problems.

Have fun!

The power of unit tests



By ganton ~ November 10th, 2008. Filed under: Unit testing.

What i’d like to share with you is that the unit tests are really power and they make developers life much easier. I saw a lot of developers that said that unit testing is just a spent time in not right direction (developing code). As I remember in the beginning I also was in this group of wrong believers. But after a while I became a developer who thinks that without unit testing the code is hard to test and it is most likely not well formed. I hope everybody agrees that the use of TDD makes the code much more structured and well formed.

I’d just like to give you an example of how unit tests help us to not miss trivial errors. Last week in our team we saw that we need to extend one of our external modules with a new functionality. Before to extend it I realized that the module needs to be re-factored in order to extend the one abstract base class and to remove some common functionality from its descendants. It was done. Unit tests for the new functionality were written and the new functionality was implemented. And I ran the tests. What happen? More then 60% of our module unit tests doesn’t pass well. And the reason was a very trivial error which without unit tests I’ll found after integration of the module.

Conclusion, the unit testing is a part of software development that nobody should ignore. It will save time and efforts when you test your code and it will improve code quality. It is for sure.

I’m annoyed of how developers used to organize loops…



By ganton ~ November 4th, 2008. Filed under: Other.

Today, I browse the code of one of our projects in order to look through a problem I discussed with another developer and I saw a loop which irritated me. The sample of what I’m talking about is below.

object obj = null;
foreach (int item in array)
{
// initialize the object if the first pass
if (obj == null)
{
obj = new object();
}
// do some work
}

I really do not like when I found a loop where we have a check and after the check we initialize an object. I became even more irritated of the comment. But why to write such code? I didn’t found any reason for organizing the loop such a way. Why I do not like it? First of all because it is not well formed and if you have a more then 20 lines of code in the loop and another 5 in the if statement it becomes not exactly clear. The second and not the least thing is that it is slower with the if statement then without it.

I can offer several variants of how to do the same and it will work faster and the logic will be the same as it is in the cycle. The first variant is just to initialize the object before to go to the loop. The disadvantage of this variant is that if we initialize some heavy object it will take some time may be more then the time of all loop checks.


obj = new object();
foreach (int item in array)
{
// do some work
}

In the case we have a heavy object for initialization we can use the variant below.


if (array.Count > 0)
{
obj = new object();
foreach (int item in array)
{
// do some work
}
}

Of course we also can use a variant with IEnumerator. It is a very elegant snippet (see below) but it will get much more time then others because of the call of a function.


IEnumerator enumerator = array.GetEnumerator();
if (enumerator.MoveNext())
{
obj = new object();
DoWork();
}
while(enumerator.MoveNext())
{
DoWork();
}

I test the performance of all variants with an array of 10000000 entries and the results are below.

I know somebody can say come on man it is not so much time difference. Yes, I know that but in any case I do not like the initial variant because it is also not elegant and doesn’t look well. Of course, it is my opinion and everybody can write code in his/her way.

Execute unit tests against DB and keep it unchanged.



By ganton ~ November 3rd, 2008. Filed under: Unit testing.

In my old post I explained how you can create a NUnit add-in for reverting changes in database made by unit tests. In this add-in I use TransactionScope class to open a MSDTC transaction and rollback changes made by the unit test. I found this approach time consuming and unpredictable (sometimes my tests doesn’t work with an exception connected with DTC connection). I wasn’t able to find why it crashes.

Today, I’d like to write about two classes I have created in order to support the same functionality but without extensions and without TransactionScope class.

First of all let me say that I use NHibernate in the project for accessing DB and .Net Framework 3.5 for application development.

Firstly I’ve created a helper class which accepts a session of type ISession and provides a single method Execute. Execute method accepts an instance of Action delegate and invokes the action into a transaction which is rallbacked. This way all changes made in database by the test will be rollbacked.


public class RollbackHelper
{
private ISession session;

public RollbackHelper(ISession sessionToUse)
{
session = sessionToUse;
}

public void Execute(Action action)
{
using (ITransaction transaction = session.BeginTransaction())
{
action();
transaction.Rollback();
}
}
}

Then created a base class for all my test fixtures which test functionality against database. i named it BaseRepositoryFixture class. It contains an instance of RollbackHelper, ISessionFactory and ISession. It contains two methods used to set up and tear down needed data for current text fixture. They are decorated with TestFixtureSetUp and TestFixtureTearDown attributes. You can note that they are virtual methods. It is done in order to allow child classes to override them.

public class BaseRepositoryFixture
{
protected ISessionFactory sessionFactory;
protected ISession session;
protected RollbackHelper rollbackHelper;

[TestFixtureSetUp]
public virtual void TestFixtureSetUp()
{
Configuration configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(Module).Assembly);
sessionFactory = configuration.BuildSessionFactory();
session = sessionFactory.OpenSession();
rollbackHelper = new RollbackHelper(session);
}

[TestFixtureTearDown]
public virtual void TestFixtureTearDown()
{
session.Dispose();
sessionFactory.Dispose();
}
}

Here is a sample how to use these classes in unit tests.


[TestFixture]
public class ViewRepositoryFixture : BaseRepositoryFixture
{
Module module;

private View CreateTestView()
{
View view = new View()
{
Guid = Guid.NewGuid(),
Name = "Default",
RelativePath = @"\Default.aspx",
RowVersion = 1,
Module = module,
Description = "some description"
};
return view;
}

[TestFixtureSetUp]
public override void TestFixtureSetUp()
{
base.TestFixtureSetUp();
module = new Module()
{
Name = "Base module",
RowVersion = 1,
IsActivated = true,
Href = "http://basemodule",
Guid = Guid.NewGuid(),
DisplayName = "Base module",
Description = "Some Base module description"
};

session.Save(module);
session.Flush();
}

[TestFixtureTearDown]
public override void TestFixtureTearDown()
{
session.Delete(module);
session.Flush();
base.TestFixtureTearDown();
}
[Test]
public void AddNewView()
{
rollbackHelper.Execute(delegate()
{
View view = CreateTestView();

ViewRepository repository = new ViewRepository(session);
repository.Add(view);

View result = session
.CreateCriteria(typeof(View))
.Add(Expression.Eq("Id", view.Id))
.UniqueResult<View>();

Assert.IsNotNull(result);
});
}
}

In this test fixture I override both set up and tear down methods in order to create a module and to delete it after the tests. Note that in TestFixtureSetUp we call base class method before child implementation in order to use its protected variables. Note that in TestFixtureTearDown we call base class after deleting the module. In the test method AddNewView we create a delegate and provide it to Execute method of the RollbackHelper.

I have in my ViewRepository a method which retrieves a specific View but note that I don’t use it. Instead of using it I create a ICriteria and execute it in order to check if a view was added into database. I do that in order to be sure that all is correct. It is clear that if I use ViewRepository method for retrieving added view I can run into an error because of the ViewRepository method.