Tuesday, July 24, 2007

Wix and Team Build (TFS) integration

At our company, I have struggled for hours to automate the setup package in a Team Build Type in Team Foundation Server.
I found several articles on how to integrate Wix with MSBuild, but none were about using Wix with Team Build. In most of the cases, the msbuild looked like this:


<propertygroup>
<toolpath>C:\Program Files\Wix\</toolpath>
<outputname>MyProjectSetup</outputname>
<outputtype>package</outputtype>
</propertygroup>

<itemgroup>
<compile include="MyProject.wxs">
</compile>

<import project="$(ToolPath)wix.targets">



I added MyProject.wxs (the wix file) in the build type directory and modified TFSBulid.proj and added the above text. The build was not successful of course :) .
After a carefully examining the log files, I noticed that the wix.targets project overrode some tasks and some properties defined in Microsoft.TeamFoundation.Build.targets file. The solution was to change all properties and targets from wix.targets file like this:
from <target name="Compile"> to <target name="WixCompile">, from BuildDependsOn property to WixBuildDependsOn, and so on...
The final step was to call the WixBuild target from AfterDropBuild overriden target.
The final msbuild script looked like this:

<target name="AfterDropBuild">
...
<calltarget targets="WixBuild">
</calltarget>

<propertygroup>
<toolpath>C:\Program Files\Wix\</toolpath>
<outputname>MyProjectSetup</outputname>
<outputtype>package</outputtype>
</propertygroup>

<Itemgroup>
<Wixcompile include="MyProject.wxs">
</ItemGroup>

<import project="$(ToolPath)wix.targets">

Everything works just fine now, and we are able to include in the continuous integration process the deployment and setup packages consruction.

Tuesday, February 13, 2007

Dynamic ASP.NET web service proxy generation

For many times I have been annoyed by the automatic proxy generation of the ASP.NET web services in Visual Studio. I wanted to work in a more “WCF” style (Windows Communication Foundation), as in using “operation contracts” and “data contracts”. Unfortunatelly, there was no way of doing this in asp.net web services. You had to deal with the duplicated code done by the generator, or you had to manually modify and update the proxy. I considered developing a library for dynamic generation of the web service using only the service contract and the address of the service. With WCF, the asp.net web services are somehow obsolete, but I thought it wouldn’t be so bad to publish my solution, since I havent’ found any way to do dynamic web proxy generation.

Here is how you should use the web service:

  1. First, write the service contract (the web service interface, and the data transfer

    objects). Example:

    public interface IWebServiceInterface
    {
    string HelloWorld();
    int GetSum(int a, int b);
    UserDetails GetDetails(string firstName,string lastName,int age);
    }


  2. Then write the service implementation using ASP.NET web services:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    public class Service : System.Web.Services.WebService, IWebServiceInterface
    {

    #region IWebServiceInterface Members

    [WebMethod]
    public string HelloWorld()
    {
    return "Hello World";
    }

    [WebMethod]
    public int GetSum(int a, int b)
    {
    return a + b;
    }
    [WebMethod]
    public UserDetails GetDetails(string firstName, string lastName, int age)
    {
    UserDetails ret= new UserDetails(firstName,lastName,age);
    return ret;
    }

    #endregion


Publish the web service, and in order to use it, create a IWebServiceInterface instance:

IWebServiceInterface dynamicService =
DynamicProxy
.GetProxy<IWebServiceInterface>("http://localhost/TestService1/Service.asmx");

And there you have it – the dynamic generated asp.net web proxy. You don’t have to worry about duplicated code or manual updates anymore. But pay attention, if you intend to use this code, you shouln’t modify the method or the types namespaces, or other details provided by the attributes, because the library uses only the contract to generate the proxy, and not the information from the WSDL. So the proxy presumes that the web service is in the "http://tempuri.org/" namespace, and all methods are decorated with the [WebMethod] attribute, with no parameters, just as they are generated by the visual studio IDE.

The code generation is done using the System.CodeDom namespace. If you have questions don’t hesitate to write me, or post a comment.


Download source code

Monday, January 08, 2007

Castle project - MicroKernel

In the "Team Work” post I have been talking about an architecture based on contract, in other words – an interface. The presenter for example was using the ILocalDataAccessService and the IOnlineService services.
What happens if the current presenter needs another service? We should change the presenter constructor, and all initialization. We could have the same problem as the one I talked about in the previous post, with the Object Mother – we could have to change the code in a lot of places and maybe hundreds of tests. This is one of the many problems which could appear because of a tight coupled architecture. The dependency using constructor injection isn’t enough sometimes, for example a presenter can use a service only in a specific moment, or it can use a service which isn’t available all the time, so the service shouldn’t necessarily be given as a constructor parameter.
The ideal way is to have an infrastructure, a framework which solves all these problems automatically for us, so we shouldn’t worry about a complex service dependency, or service creation.

The first idea is to have a collection of services, which can be retrieved at any time using their names, and/or interface they implement. These would solve our problem with the service we don’t use all the time, or they could be unavailable at a specific time.

IService service = (IService) Kernel[“My service”];

The second idea is to automate the construction of services, if a service A needs to be constructed and it depends on another service - B, the framework should automatically retrieve the service B from the collection and construct the service A. So the services should be instantiated automatically without having to call explicitly the constructors.
Kernel.AddService(“Service A”, typeof(AService));
Kernel.AddService(“Service B”, typeof(BService));

If the BService class has a constructor with a AService parameter, the framework should automatically call the constructor with the service “A” added in the first step.

Another problem would be the service “style”: is the service a singleton? Or should a different instance be created for every call of Kernel[“My Service”] ? Or maybe create a service for each thread? These details should also be handled by the framework.

So, we shouldn’t be worried when changing the constructor of a service. The infrastructure should have to worry now. We could also use this framework for a plugin architecture: until now, I have worked at a few applications which used plug-ins, and most of the time, a plugin defined in fact a service. I had to do every time my own manager for these plugins. Some plugins were singletons, some would had to be instantiated for every call, but I had to think how to do all this by myself. I didn’t use a framework which could do all the work for me.

Some days ago I have searched for such a framework and I found exactly what I was looking for. It is called the Castle Project – MicroKernel. I started using this framework, and – until now, I am very pleased with the results.

Thursday, January 04, 2007

TDD problems - Object Mother

I started working on a new project some time ago, and decided to work in a TDD style as much as I could. The idea was that there were very vague specifications for the project, resulting in a very “agile” project. So what methodology other than XP Programming and TDD should I adopt, I thought.(If you don't know about TDD (Test Driven Development) read about it on wiki.)

I have had some experience with TDD before, but I usually stopped writing tests when I started to develop the user interface. Although I used the MVP design pattern, so I should have been able to test the presenter using a mock as a view, I think this is a very time consuming process, and sometimes maybe not very useful, especially when the presenter logic is not very complex – it takes data from the object and passes it to the view.

The project I was talking about is a monitoring engine. Used to monitor computers, software applications and other stuff… The business was pretty complex, and there was no user interface, so the TDD was almost a must. I started to work in almost in a perfect TDD style, but after some time I found it very difficult to maintain the unit tests.

The problem was that a small change in the business code determined a lot of changes in the unit tests. It was pretty upsetting when seeing 100 compile errors when changing just the number of parameters of a constructor. And very time consuming. At some point, 50% of my work consisted in maintaining the unit tests. Most of my tests were constructing business objects in order to test them. A small change in the constructor or in the manner the business object was constructed determined as I said a lot of errors in the tests. So my idea was to separate the object construction from the unit tests, and make some kind of object factory, but a smart factory, which could create very complex objects with just a function call, or let me customize the object creation.

I found that this method is in fact a pattern, called “Object Mother”:

“Through a handful of simple method calls, this utility provided a complete, valid, and customizable structure of business objects (think of an invoice: its lines, all related charges, remit to, bill to).

Object Mother starts with the factory pattern, by delivering prefabricated test-ready objects via a simple method call. It moves beyond the realm of the factory by

  1. Facilitating the customization of created objects,
  2. Providing methods to update the objects during the tests, and
  3. If necessary, deleting the object from the database at the completion of the test.

Now, when I change the number of parameters in a constructor, I have to change only the object mother not all my 1000 test units, so I consider this method very helpful.

Tuesday, November 28, 2006

Team work

As I started working at my new job, I noticed that the biggest problem was team work. The problem consisted in the fact that the project was pretty big and the team was also pretty big, and distributed across the entire floor. From the programming point of view, the problem was that the modules were tightly coupled, for example in order to develop my module, I had to use a library for a hardware external device, and a library for web service calls.
Almost all programmers were complaining that they can’t work because some module which they had to use isn’t working properly. Sometimes, I saw some developers just waiting the whole day, just because some web service which they used wasn’t working, or they needed for someone to write a web service so they could call it in their application.

And they thought it’s something normal that they should be provided with the perfect libraries and perfect working code and working conditions so they could work. When the project deadline approached, they started to work till midnight.

So what I have learned from this, is that you shouldn’t expect a provided module to work perfectly, and you shouldn’t wait for some problem to be resolved in order to continue your work. Your application architecture should provide a method so that the “integration” with other modules could be delayed to an indefinite moment in the development cycle. Using this architecture, if you develop for example a windows application, you don’t have to wait until the database architect designs the tables, or some specific web service is created for you.

I developed my own architecture to prevent all these issues, and I must say I’m pretty pleased with it until now. In fact I can’t call it “my own” because it is based on a series of design patterns, like MVP (Model – View – Presenter), and Dependency Injection.

I’m not going to discuss about the MVP in details here (maybe in some future post). The main idea is that MVP is a modified MVC, more loosely coupled. The “design by contract” is the main technique that stands at the base of MVP, and also helps us to defer the integration with other modules.

The presenter is the main business entity, and it is usually concrete (not abstract/interface). All other entities can be interfaces. The presenter is given a view and a model. It should take the data from the model and “give” it to the view, and also it should listen to events coming from the view or the model. Usually, the presenter works with abstract views and sometimes with abstract models. So, “design by contract” means that we have a “view contract” – an interface which the presenter will work with. The same pattern applies to the services which are used by the presenter business.

Let me give you an example: a simple windows forms application which calls a web service to give a list of persons, displays them, and saves some of them in a local database/file. How should we develop our application, considering that the web service isn’t developed yet, and your boss hasn’t decided yet whether to use a local database, or some xml files. If you don’t like programming more than 10 hours a day or in weekends, then don’t wait for your boss to make the decision, or the other person to develop the web service, in order to start programming.

Presenter First, a variant of MVP, means developing the presenter first (doh…), and then the services and views.

So, we start with our presenter:

public class PersonsPresenter
{
private ILocalDataAccessService localDAL;
private IPersonsView personsView;

public PersonsPresenter (IPersonsView personsView,

IOnlineService onlineService, ILocalDataAccessService localDAL)

{

this.personsView = personsView;

personsView.Persons = onlineService.GetPersons();

personsView.SaveButtonPressed +=

new EventHandler<EventArgs>(personsView_SaveButtonPressed);

this.localDAL = localDAL;

}

void personsView_SaveButtonPressed(object sender, EventArgs e)
{
localDAL.SavePersons(personsView.SelectedPersons);
}

}

The presenter should be provided with a view (IPersonsView), an online service (IOnlineService) and a local data access service (ILocalDataAccessService). The business is very simple, the presenter takes a person list from the online service and puts them in the view. When the save button is pressed, the selected persons from the view are saved in the local database. Now we can implement our services and view. The view is a windows form which implements the IPersonsView interface, the online service implementation could return a default list of 3-4 persons for example, and the local data access service could save the persons in a in-memory list until our boss decides between xml files, isolated storage, and sql server database. So we can work with mocks for an undefined period of time.

The manner of constructing the presenter is based on dependency injection – constructor injection.

We could also make a mock for the view, and make automated tests for the presenter (This is a major advantage of using MVP, perhaps in a later post I will talk more about TDD – Test Driven Development).

In conclusion, we succeeded in developing our main application business and interface, without knowing the exact implementation of the local data access layer and the web service.

Now, if the programmer responsible for the online service gives us a library representing the service proxy, we just have to implement an adapter for our interface of that proxy. Don’t forget to catch and rethrow all possible exceptions from that library, belive me it will help you. Especially when you are developing a module which has a visual interface, it will spare you of a lot of problems if you display “Web service proxy error: Object reference not set…” rather then just “Object reference not set…”. When you will be asked by your boss about this error, you can respond “Can’t you see??? It’s a web service proxy error, it’s not my fault!” : )

Friday, October 13, 2006

Hello everybody!

Hello.

Starting from today, this is my new blog, where I'm going to try to post interesting stuff related to .NET programming as often I can. I don't have extraordinary experience as a software developer, but I think the newcomers in the software development industry would benefit from this, because I can still "speak their language", the language of beginners let's say. All interesting stuff that I learn I will try to present it as simple as it could be.
My main point of interest when writing software is to benefit from this process as much as I can, learning new design patterns, learning new architecture related stuff, and always wanting to improve myself professionally.

I am sure that many of you are not pleased of what you are currently developing, probably you think you don't learn anything at your job lately, but here is still hope : ) . If you do it your way. When developing, you should also try to think about you, about what you are learning, about how you are improving yourself, don’t limit yourself to do something functionally correct and as fast as you can do it.

In the future posts, I’m going to try to present how I’m trying to benefit from my work. I’m hoping that by doing this, somebody would benefit from it. I’m also open for suggestions, because I’m not a know-it-all person. So please don’t hesitate to comment/argue with me.

I will also include a lot of technical details of course. This is not a philosophical blog (as it would have appeared : ) ), or one of those blogs written by somebody who has nothing better to do than writing his opinion about life or something, so expect to find code and very technical stuff. This blog is for developers, so if you’re not a developer, I think I’m going to bore you, sorry : ).


See you soon.