Carlos Ble

Carlos Ble

I am a professional software developer, I solve problems.

I also teach and mentor developers to build better software.

Developing software since 2001.

Can I help you?

  • Do you need high quality tailor-made software?
  • Need training on TDD, clean code or refactoring?
  • Is it risky? do you need advise?
  • May I pair with you to write better code?

Events

Upcoming public training courses:

  1. [Online - en Español] 25 y 26 de Junio
    Test Doubles con JavaScript - Online
  2. [in English] July 7, 8 & 9
    TDD (open to the public) - Tenerife, Canary Islands
  3. [en Español] 14 y 15 Julio
    TDD (en abierto) - Gran Canaria, Canary Islands
  4. [in English] October 13, 14 & 15
    TDD (open to the public) - London, UK
  5. [en Español] 29, 30 y 31 de Octubre.
    TDD (en abierto) - Madrid, Spain

Conferences:

  1. I'll be at SocratesUK 2014
  2. I'll be at the London Test Gathering Workshops.

Archive for August, 2007



I think my first scripting language was something called ARexx in my old Amiga 1200. I used to write ARexx script for environment tunning.
After that the Bourne Again Shell (BASH) has been REALLY handy in the past years as I was working always in unix environments (mainly Linux) and BASH along with AWK and Sed, Grep, etc.. are extremly powerful tools, I'd say a must. In fact, they are still my favorite tools in Linux for system scripts.
Along with BASH I used to do some scripting in Python and Perl but I don't like Perl at all, is is too weird and difficult to maintain :-) .
I think scripts are a key to boost productivity sometimes, i.e, everytime you need to generate code, use regular expressions, or just to write something quick. They are good even to write a complex applications or systems, it just depends on your problem. Said that, I'm afraid a lot of C# developers do not use scripting languages at all and this is a mistake in my opinion.
Now that I'm working on Windows I felt a bit lost without my bash and all the GNU tools so I installed them thanks to the
GNUWin32 project and the Win-Bash but unfortunately I miss a lot of features (the bash is not the same actually). I've concluded that it is better to use Python on Windows (for all the scripting) rather than Bash.

Here you go a nice script to find regular expressions on .cs files and replace them with other regular expressions.
You only have to run the script from your solution root folder in order to go into every csharp file recursively.
You can do things like that:

C:\>python recursiveReplace.py 1 "(\S+)\.(\S+)\.Key\s+" "\1.BusinessDefinition.\2"
The former expression change this: Something.AnotherThing.Key
For this: Something.BusinessDefinition.AnotherThing

C:\>python recursiveReplace.py 1 "SomeString[0-9]" "SomeStringWithoutNumbers"
The former expression change this: Something9
For this: Something

I'd like to think that you were not changing your code manually in the places in which you can just use a regular expression to replace :-)

Don't worry if you don't know Python and regular expressions: http://www.amk.ca/python/howto/regex/regex.html

On productivity

Most of the people I've met think that being at work a lot of hours means more productivity. I mean in IT, in software development. For some reason they believe that the more hours you sit in front of the computer the better. I find this absolutely wrong, anti-productive 100%. Even when I was addressing my own company it was a mistake to work more than 6 hours a day because I don't think the human beings are made to do the same activity that long.
If you think of something you like to do, whatever it be, you'll most likely agree with me that you would not like to do so for more than 4 hours. I love software development and I like to write code every day but, honestly, I don't feel like working more than 5 hours a day, no matter how exciting the project is. When I have only a few hours to solve a problem I focus on it much more than when I have to be in the office late hours. When I start the day in the office having rested properly, having a tight deadline for the items in my to-do list I'm definitively more productive and design the software better.
When I was in colleague I used to focus much more as the exams were getting closer and I did pass the colleague successfully understanding almost everything and remembering only the important things.
The ideal for me is to work 28 hours a week, investing some of that time researching and other activities (will post about that someday). I hope companies realize that in the future to have more happy and efficient developers, focusing on the quality and not in the quantity (usually the quantity of wasted time)

I've discovered this cool site: DotNetKicks.com which has been developed using the popular SubSonic framework. It seems to be like a Digg clone but regarding .Net development. What is very nice is that they consider Mono, CastleProject and other open source projects. I hope to get some posts kicked by this new site ;-)

There is a lot of nice documentation at Microsoft's website along with some other good sites but I'm going to add some details and my working configuration for my service. For me, coming from Linux environments it has been a bit tedious to figure out how Windows manages the SSL certificates and all the stuff.
The definition of the service is something like this:

[ServiceContract()]
public interface IPaymentService
{
...
public class PaymentService : IPaymentService
{

I have it hosted on IIS as a virtual directory within the Default Web Site so the folder contains a web.config:

<system.serviceModel>
<services>
<service name="Services.Mail.MailService" behaviorConfiguration="KPMembershipProviderOverHttps">
<endpoint contract="Services.Mail.IMailService"
binding="wsHttpBinding" bindingConfiguration="MembershipBinding"/>
</service>
<service name="Services.Payment.PaymentService" behaviorConfiguration="KPMembershipProviderOverHttps">
<endpoint contract="Services.Payment.IPaymentService"
binding="wsHttpBinding" bindingConfiguration="MembershipBinding"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="MembershipBinding">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="KPMembershipProviderOverHttps">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="MembershipProvider"
membershipProviderName="KPMembershipProvider" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

<system.web>
<authentication>
<forms requireSSL="true" />
</authentication>
<compilation debug="true">
<assemblies/>
</compilation>

<membership defaultProvider="KPMembershipProvider">
<providers>
<add name="KPMembershipProvider" type="Services.KPMembershipProvider, Services" />
</providers>
</membership>
</system.web>

I had to implement my MembershipProvider class within the same Services assembly:

public class KPMembershipProvider : MembershipProvider
{
public override bool ValidateUser(string username, string password)
{
#region Business logic to validate an user
...

#endregion
}

#region the rest of the MembershipProvider overrided methods just throw a not implemented exception
...

#endregion
}

And this is the web.config in the client side:

<system.serviceModel>
<client>
<endpoint address="https://localhost/Services.Demo2/Mail.svc" contract="Services.Client.IMailService" name="MailService"
binding="wsHttpBinding" bindingConfiguration="MembershipBinding">
</endpoint>
<endpoint address="https://localhost/Services.Demo2/Payment.svc" contract="Services.Client.IPaymentService" name="PaymentService"
binding="wsHttpBinding" bindingConfiguration="MembershipBinding">
</endpoint>
</client>

<bindings>
<wsHttpBinding>
<!-- Set up a binding that uses UserName as the client credential type -->
<binding name="MembershipBinding">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>

<behaviors>
<serviceBehaviors>
<behavior name="KPMembershipProviderOverHttps">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<!-- UsernameToken over Transport Security -->
<userNameAuthentication userNamePasswordValidationMode="MembershipProvider"
membershipProviderName="KPMembershipProvider" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

Now in the C# code, everytime you use the client, you have to send the credentials as well:

PaymentServiceClient client = new PaymentServiceClient();
client.ChannelFactory.Credentials.UserName.UserName = "carlos";
client.ChannelFactory.Credentials.UserName.Password = "12341234";
client.CallWhatever(parameters);

In order to get a web server running on HTTPS, we need to create a SSL certificate. And also to get the client accepting the certificate it has to trust the server (which is a requirement for the service client to work) so that the it has to trust the certification authority which signed the certificate. I created the request using IIS and signed it using OpenSSL following these links:

I had to change few things regarding the steps described in thouse documents but they were tiny changes like renaming the serial.txt to serial or creating the index.txt file empty or removing the "-config openssl.config" option from the command line.
Once IIS has the signed certificate it enables to port 443 for ssl automatically and you have to run the certmgr tool (from then run item in the start menu) and add the ca.cer and iisx509.cer into the Trusted Root Certificate Authorities in order for the client to trust the certificate (I guess the ca.cer is enough). If you open IE and access the service in https://localhost/Services...../Service.pvc and you get the page without any certificate warnings or errors, your service client have to work as well.
Eventually, if you need to export the certificates you need the private key and you can do so generating the .pfx files from the certmgr tool or mmc: http://www.digicert.com/wildcard-export-import.htm
Now, ir order to avoid setting the credentials everytime and read them from the web.config, there are a few options:
http://msdn2.microsoft.com/en-us/library/ms730868.aspx
But I've implemented a quick hack to solve the problem too:

clientWrapper.jpg

Here you have the copy-paste version:

public class ClientWrapper
where T : System.ServiceModel.ClientBase
where Z : class
{
private T _service;
private static string _username = null;
private static string _password = null;

public string Username
{
get { return _username; }
set { _username = value; }
}

public string Password
{
get { return _password; }
set { _password = value; }
}

public T Service
{
get { return _service; }
set { _service = value; }
}

public ClientWrapper(T service)
{
_service = service;
readCredentialsFromConfig();
_service.ChannelFactory.Credentials.UserName.UserName = Username;
_service.ChannelFactory.Credentials.UserName.Password = Password;
}

private void readCredentialsFromConfig()
{
if (_username == null)
{
_username = ConfigurationManager.AppSettings["servicesUsername"];
_password = ConfigurationManager.AppSettings["servicesPassword"];
}
}
}

And the required entry in the web.config

appSettings
add key="servicesUsername" value="carlos"
add key="servicesPassword" value="12341234"
appSettings

This is a hack more than an elegant solution but given the WPF architecture it has been the only way I've found to
make a ComboBox strongly typed. The problem I had is that I use to access the SelectedItem ComboBox's
property casting the object or using the as operator expecting a certain type of object and it could
be ambiguous. At least it is tired to find out whether there is no SelectedItem or you are casting to the wrong type.
I mean, if you populate the combobox with an object of type A and access the SelectedItem expecting type A,
then a change in the combobox items type will break the application during runtime. It could be difficult to detect.
By using the Boxerp.Controls.GenericComboBox template you get a strongly type combo to avoid that situation.

The idea is to wrap the ComboBox within the GenericComboBox keeping track of the items inserted in the combo
and casting the SelectedItem property. The problem is that XAML doesn't allow for generics so you cannot put the
GenericComboBox in the XAML but you can put a Grid or a Panel as a placeholder for the combobox.
You must place the combo insde the grid programmatically:

  • GenericComboBox<int> _myCombo = new GenericComboBox<int>();
  • _myComboGrid.Children.Add(_myCombo);
  • _myCombo.AdjustDimensions();




The classes are available to download in the Boxerp svn:
InterceptedList.cs

GenericComboBox.xaml

GenericComboBox.xaml.cs