Tuesday, January 17, 2012

Simple.Data, is it really a good idea?

I've been keeping an eye on micro ORMs for a while. Yesterday I decided to give Simple.Data a try. From the introduction, it looks brilliant, the fluent API, the briefness, the simplicity,..., everything seems to be perfect... well, until I started coding.

Once I'm in VS, I immediately realized that Simple.Data might not be the one I wanted. Its magical syntax heavily depends on .NET's new dynamic feature (yes, it's .NET 4 and above only). That means you loose the static language's best capability: compile time checking. And also you loose the productivity from your tool, there is almost no intellisense provided by VS. You might say, what's the big deal, the syntax is cool. Yes, it's cool. But the problem is, if you make an accident typing or a normal typo (who doesn't?), then compiler won't be there for you and you'll only get the null at runtime. Maybe through unit test coverage may resolve this. But from where I stand, it seems to be just a better alternative of HQL dressed with a new cloth named fluent API.

But I did find some part that really make me happy. It seems Simple.Data recognizes fields separated by underscore. I've got a table with columns like CUSTOMER_ID, and Simple.Data can happily map it to a property named CustomerId. If I rename CUSTOMERID, then Simple.Data still works fine. This is clever!

There is a tool, Simple.Data.Pad, to address this issue. So far I haven't been able to make it work with sqlite yet.

In general, I'm not so convinced yet, but I'll continue on the project to work with it, let's see where I'm going to end with.

Sunday, January 15, 2012

.Net code contracts

I knew there is something new in .NET 4 called code contracts and I also saw some simple code snippets about it. Without digging in, I though it was just some fluent code sugar to help you replace assertions like
if (input_int > 0) throw new ArgumentException("some msg");
with
Contracts.Requires<argumentexception>(input_int > 0);
and come on, this fluent sucks, requires an exception? I would have written a better one!

But I was wrong!

I had some free time today and I opened a long bookmarked url and took a look at it. After reading through the first section, I knew I was wrong. There was post compile code weaving and checking. Without it, there is not much virtual inside, but with the post process, everything can happen. And it turns out to be a very much valuable technology than I thought. Kudos!

Some simple googling led me to this blog that provides some analysis I would also do, using Reflector on the generated code, to see the magic there. Also in their home page, there is a video at the bottom(?!) of the page, after some random comments!

Wednesday, January 11, 2012

Differences between event and delegate in C#

I did not realized some of them until I came across this article. In short,

  1. event is a modifier and delegate is a normal type declaration
  2. thus event can be used in interfaces and delegates can not
  3. event can only be accessed by the class defines it, not even the child classes (an in-direct access through protected method is possible), while delegate is more like a member
  4. normally event follows a convention on signature: foo(object source, EventArgs e)

Sunday, January 8, 2012

Start on Python

It's being quite a while since my last blog and I would like to have something really new to start the new year, Python.

Recently I started teaching my son Python. I don't know Python before. So I have to learn it while teaching him. I intended to learn Python for quite some time, but time has always been my biggest enemy, together with my memory: it's getting worse every year. If I learn something and don't use it immediately, then it's quite possible after 3 months, I'm new to it again. So I start blogging it, trying to make my memory last longer.

Python is an elegant language, it's very expressive and has very brief syntax - though this can be a double side sword - to a experienced programmer with long time Java/C#/C++/C experience, it takes some time to be used to it.

Some points about Python I'd like to note down:

  1. Python started at 1989, and first public appeared at 1991
  2. CPython is the C implemented version of Python, then standard Python
  3. Python generally has 2 kinds of primary types: numbers and strings, though array/list and hashtable/dictionary is first class member of the language.
  4. Python use space indention to indicate sub-scope. The indent does not have to be unified within program, but must be the same for the same scope
  5. like most script languages, variables do not need to be pre-claimed before being used
  6. Python can be compiled into byte codes, like Java class files, with extension .pyo or .pyc
  7. using '''/""", you can have multiple line strings
  8. raw string starts with a 'r': r"c:\a\path\file"
  9. unicode string starts with a 'u': u"this is unicode"
  10. raw_input() will return anything you input as a string, while input() will try to evaluate your input as Python code! For example:
    >>> a = 3
    >>> b = input()
    a+11
    >>> b
    14
  11. Python as +=, -=, *=, /=, similar to other languages, but no i++, ++i, i-- or --i. But ++i or --i won't give you any syntax error, since the +/- are treated two time sign prefix!

Tuesday, October 4, 2011

substring in unix shell scripts

Recently I need to handle quite some unix shell scripts and find some tricks about strings that is less known.

1. substring
Most of my searches resulted using awk. I don't mind using it, but it make the script less readable. Luckily there is a easy way:
one_str="this is a string"
sub_str=${one_str:5:2} # ${org_str_var:0_based_index:length}
# now sub_str contains "is"
2. string search and replacement
Again, most search results say you should use "sed" or other external commands. But below line will also do the trick:
alpha="This is a test string in which the word \"test\" is replaced." beta="${alpha/test/replace}" #replace only the 1st insance
beta="${alpha//test/replace}" #replace all insances

Friday, September 23, 2011

.NET/windows Debugger list.

It's been quite a while since my last post. Anyway, I don't have any followers and this is just for myself to keep some ideas here.

This link gives a description of several debuggers/tools and their best usage scenarios. Though it seems to be quite powerful, I really hope some tools that can do debugger record and play like the Java world has now.

Friday, July 15, 2011

Git as SVN client: rollback to previous version

Git is a very power content based DCVS. I use it as a SVN client, since my company is still using SVN. I've been quite happy until yesterday: I made a mistake in a commit and need to roll it back. The problem is I already checked in to SVN and it broke some unit tests I did not cover on my local run, but the CI server did. I'm used to local reset to revert some bad changes, but this will only help when you have not pushed changes to remote sites:
git reset HEAD~
Above command will reset your master pointing to the previous commit. From Git's point of view, it's done. The current HEAD (master) now points to the desired copy of the codes you want to have. But the problem is once you do a synchronization with SVN, it's going to be reset back to the copy you want to get rid of. This is because Git is treating master tag as a pointer. When you do the above reset, it just move the pointer back without doing any real commit. So here what we really need is a "real" commit, which "git revert" provides.
git revert HEAD
After this, if you look at the history, you'll find a commit with the new result. Now synchronize with SVN and you'll find a new commit in it and the changes you made has been rolled back.

Note, git revert should only use when you have pushed your changes to remote sites. Then it gives you a chance to fix the problem. If your change is still local, git reset is recommended.

Maybe this is stupid, but it took me a while to figure this out, hope this helps some one out there who is also bothered by this.