Jun 9, 2008

Good code I would like to be used...

Hm, I reallly like this code but it seems that they will never use it. I am a PM, they are developers. Saddly...

public class Actions
{
private delegate void ChangeState(License license, NewLicenseState newState, string reason);
private IDictionary actions;

public Actions()
{
actions = new Dictionary();
actions.Add(
ChangeToExpiredBlockedStateException,
delegate(License license, strin reason)
{
license.ChangeState(5, reason);
});

actions.Add(
ChangeToExpiredInUseStateException,
delegate(License license, strin reason)
{
license.ChangeState(4, reason);
});
}

public ChangeState this[LicenseException exception]
{
get
{
if (this.actions.ContainsKey(exception.GetType())
{
return this.actions[exception.GetType()];
}

Assert.ThrowInvalidOperationException(message, params args);
}
}
}


And then use it as follows:

new Actions()[ex.GetType()](license, ex.Message);

Yeah, I wrote this code as a sample. To bad it seems too complicated. Although I like it, they do not.

Feb 8, 2008

.Net Encoding Simplicity

Well, recently I was asked about
why this function does not work... ok, it works when the browser encoding is Unicode and it does not, when it is Shift JIS
here is the code:

public static string Convert(string input, Encoding source, Encoding destination)
{
if (String.IsNullOrEmpty(input))
{
return String.Empty;
}

return destination.GetString(
source.GetBytes(input)
);
}

"Well..." - I thought, - "It is not supposed to work." In fact, it is a popular mistake to misunderstand how CLR manages string. And the fact that is almost always is missing is that all strings in .Net are Unicode encoded (I suppose that in Java too).

I wonder why actually there are "source" and "destination" encodings...
When we write something myEncoding.GetBytes(meStringHere) we actuall saying the following to myEncoding "hey, give me the real bytes of this string if it were encoded with myEncoding encoding". And what it actually does is trying to map Unicode bytes to myEncoding encoding bytes using the GetBytes(char[], charsQuantity, byte[], bytesQuantity, Encoder) method (this method is actually unsafe and looks differently from what I have written here, but anyway....).

But, the most interesting part here is GetString method. When we call this one (myAnotherEncoding.GetString), we trying to say the following "hey, create me a Unicode string from the following byte array assuming that these bytes are myAnotherEncoding encoding bytes". This is certainly not true - just look at the method... it has two different (or perhaps different) encodings. And what we are doing for example when we try to use the following schema:

input string = "my input string with unicode \u00df or something"
source encoding = Unicode
destination encoding = Shift JIS (I know of two at least...)

We try to do the following:
unicode string -> Unicode bytes -> shift JIS produces Unicode string from Unicode bytes assuming that these bytes are Shift JIS' bytes...

That is not very correct, no, it's wrong.

For the better understanding of what actually happening when you use encoding and strings I suggest looking the following links (they does not give you advices on what-to-do-when, but provide a solid background to work with less issues):
Have a nice day!

Jan 17, 2008

LINQ to WMI provider

Recently, I needed to analyze WMI-related code and now, I really think about to implement\find\improve a LINQ to WMI provider with the following requirements:
  1. 1. it should support both .Net 2.0-3.5 frameworks
  2. 2. it should be extensible - as WMI support providers model LINQ to WMI implementation should allow people to quickly create modules for different WMI providers
  3. should support querying both events and objects
  4. should have Castle-like error handling
So far, I found only one implementation of LINQ to WMI here and here.
Should take a look at what is implemented already...

Jan 15, 2008

70-536 is done! 70-526 / 70-528 next :)

Great!

Today I have passed 70-536 (Application Development Foundation) Microsoft certification exam.
That is really nice and I am happy.

Hope to pass 70-526 and 70-528 soon!

Jan 11, 2008

Thinking about Fluent Interfaces

I am really inspired by the new ideas of DSLs, Language Oriented APIs, an so on... I found today a link about Fluent Interfaces.

And that gave me an idea of using explicitly and implicitly implemented interfaces.
Where, in the following example:

class Order: IOrder, IFluentOrder

IFluentOrder can really be implemented explicitly and user will never have a problem of mixed modes, i.e. the following code will be very easy to implement:

customer.newOrder()
.with(6, "TAL")
.with(5, "HPK").skippable()
.with(3, "LGV")
.priorityRush();

And then, when user will be working with Orders, he will be working with IOrder interface, instead of dealing with methods from both IOrder and IFluentOrder interface.

That requires additional thinking...

Dec 4, 2007

ThoughtWorks first podcast - DSL (part 1)

ThoughtWork'ers have started making a podcasts, and the first on e is about DSL.
I am very excited. You can download it here.

Dec 3, 2007

LINQ to Active Directory

While I was looking into new .Net 3.5 Account Management API (System.DirectoryServices.AccountManagement.dll, former Principal API) I have found an example of implementation of LINQ to Active Directory extension. There is not only the source code but the series of articles as well describing each part of the implementation.

Worth taking a look - here.