Search This Blog

20 January 2012

Deal with multiple .vsmdi-files in sourcecontrol

If you ever have worked in a large project you have probably seen that there are multiple .vsmdi-files under sourcecontrol after a while. These files contains information about the testruns in the project and if the current file is checked out to another developer you will create a new such file if you change the testrun.

There is a good reason for not having these files under sourcecontrol.

Almost all developers have their own way of viewing files and especilly testruns and if this file is checked in and somebody makes a change that change will affect all other developers when they perform a "get latest"

The below blogpost is very informativ in specifying in how you can remove these files from sourcecontrol and make it possible for every developer to create their own testruns.

http://seriouscodage.blogspot.com/2010/03/multiple-vsmdi-files-with-vs2008-path.html

15 December 2010

Use Outlook as interface against Lotus Notes

During my work as consultant I often find myself in positions where I get a new mail from a customer during a project. Since I already have a couple of email addresses, at least one from my company and one private. I'm using Office Outlook to gather all these mail accounts in one place to avoid having to look through multiple places and get access to all my mails easy and fast.

With my latest customer I was required to get a Lotus Notes mail address and when looking over the web to find a method to use Outlook as interface with this mail too I started to think that this wasn't possbile without the use of DAMO (Domino Access for Microsoft Outlook) which is a program that IBM stands for that makes this possible.

I don't really fancy using an extra tool for making this possible and continues to look around and eventuelly I found a solution that is blatant obvious. All you need is to know that adress of your Domino server and your account information.

Add a new account and choose to create manually then select an internet mail account of type IMAP. Add the correct information with using your domino server as incomming and the correct server as outgoing. Your technicians should know what settings you need here.

After that leave all other settings as default and Voilá you can start using your outlook as interface against Lotus notes.

  

08 November 2010

Using reflection to obtain type information in runtime (C#)

Usually you know which type that you are working with but once in a while you only know what basetype you are working with but doesn't know what subtype you currently have.

During these times reflection is the way to go. Using reflection you are able to find out what type you are working with and create actions based on the fields, events and properties of the type.

I'll start with a short example to show the usage of reflection:

If you send information about events that happens by a webservice and these events are prone to change and it is possible that new events can be added too. If then the webservice usages is so that a presentation of theses events should be created you need to have some way to create a default view of the properties of the different events.

We have three classes defined, one base class and two classes that inherites from this one and defines some properties of themself.

public abstract class MyBaseObject 
{
   public string Message;
   public string Felmeddelande;
}


public class MyDateTimeObject : MyBaseObject
{
   public DateTime StartTime { get; set; }
}


public class MyOccurenceObject : MyBaseObject
{
   public int NumberOfTimes{ get; set; }
   public string[] Values { get; set; }
   public string[] Headers { get; set; }
}



If we now have a method that accepts a parameter of MyBaseObject and want to create functionallity of examining the content of the object we can do as follows:

public class ObjectInspector
{
   public static InspectObject(MyBaseObject obj)
   {
      var typ = obj.GetType();
      var fields = typ.GetFields();
      var properties = typ.GetProperties();

      foreach(var field in fields)
         Console.WriteLine(string.Format("{0} : {1}", field.Name, field.GetValue(obj));      

      foreach(var prop in properties)
        if(prop.CanRead())
           Console.WriteLine(string.Format("{0} : {1}", prop.Name, prop.GetValue(obj, null));      
   }
}

A warning, ifyou are to use GetValue of properties then you need to make sure that the method CanRead returns true. This method examines the accessor methods for the different properties and makes sure that the property is readable. Likewise the method CanWrite returns information is the property have a setter.

More information about this can be found in these two articles:



15 September 2010

Avoding hardcoded propertynames in C# .NET

Using strings that denote the name of fields and properties of objects in code are always a bad thing. The reasons are many, its hard to refactor code that contains hardcoded strings with properties names here and there in the code and its easy to make a typo and write some of the property wrong.

Wouldn't it be better if it was possible to avoid this and gather the property from the object directly instead and thereby avoid having to make the typo in the first place?

Luckily this is quite possible with a bit of functionallity added into the project. The below code picks out the name of the property or field of and object and returns the string name for this item. I got this idé from David Morgantini blogpost http://davidmorgantini.blogspot.com/2009/07/nhibernate-disjunction.html

public static string GetFieldName<TObject>(Expression<Func<TObject, Object> exp)
{
     return Regex.Match(exp.ToString(), @"\.(.*)")
               .Groups[1].Value.TrimEnd(new[] { ')' });
}

The code can be used as this

GetFieldName<Book>(b => b.Title)

This will give the string "Title" which can be presumed to be a property of an object named Book.

A more informativ place to use this code is when using NHibernates criteria API which can be cluttered by string constants.

public IList<Book> GetBooksByAuthor(string author)
{
    return Session.CreateCriteria(typeof(Book))
             .Add(Restrictions.Eq("Author", author)
             .List<Book>();
}

Can instead be written as

public IList<Book> GetBooksByAuthor(string author)
{
    return Session.CreateCriteria(typeof(Book))
             .Add(Restrictions.Eq(GetFieldName(b =>
                                              b.Author), author)
             .List<Book>();
}

Hopefully this can help you out with getting more managed code which is easier to maintain in a good state.






27 August 2010

Productivity Power Tools for Visual Studio

Began using Power Tools just recently and already I'm finding that I depend on this tool more and more. handling large solutions with many files and where people conduct refactoring on a daily basis.

Is much easier to get a good organisation if you are able to overlook what you have changed without having to search all around for changes that have been made. Another good thing with this tool is that its relativitly lightweight.

Last but not least this tool helps you out when files and classes are being written with different styles with regards to tab or spaces for example.

13 August 2010

Cold not load type '...' from assembly '...' during builds on different targets

A hard to track down error occured during my current project that required several hours of work before finally getting solved by a very simple solution so I feel I should give some information about my problem and the solution found to save others from doing the same work.

The problem is that we are using multiple build configurations with substitution files for Test, Development and Production environment.When building for some environment and then switching configuraton and adding some classes, or when other classes are checked in, in some circumstances a problem will occur when different assemblies cannot not be found.

The thing that makes this even harder to figure out is the fact that while using MS Test to run the test the tests will fail but if you enter debug mode the tests will pass without any problems.

The solution is to make a clean of the entire solution not just the current configuration, either by switching between the configurations and cleaning or running a batch build and cleaning all different configurations and cleaning them up and rebuilding.

The reason while this is happening is that some files cannot be deleted for some reason and the build actually fails to rebuild the assemblies needed.

11 August 2010

Mocking of objects

When testing an application that interacts with other external systems or that access a database you need some handling for this to make this work correctly.

Either you need some test versions of the systems and setup the database similar as the production database which is cubersome and lot of work to make each test work. A slight change in the object model would require that the tests also are changed.

A better method is to use some mocking framework to simulate that certain calls are being made and check that this really happens in the test. There exists several such mocking frameworks that have different advantages and disadvantages.

In the projects I'm currently working with we use a mocking framework developed by Mikael Waltersson which is called Simple Mock. This mock framework is as the name specifies very simple to use but don't be fooled the easy of use doesn't imply that the mock framework lacks functionallity, instead it has the functionallity that is required for a mock framework.

Try it, the time you spend learning how to setup the framework for your project will be earn quick enough once you start writing more and more tests.

Link to the development page on codeproject: http://www.codeproject.com/tips/45376/Simple-Mocking-new-mocking-framework-for-NET-3-5.aspx