Tuesday, April 26, 2011

My StackOverflow Reputation Over 2K

Finally today My SO reputation points are over 2K, 2008. It is a lucky number. Actually, I have not actively raised or answered any questions recently. I just keep checking my SO site and watching some questions there. That's why my points grow very slow.



Another interesting feature of SO is that you can subscribe to some user's feed to see user's activity. It is specially beneficial to following some awesome gurus. For example, I subscribed to user bbum, who is very good in ObjC and iPhone development.

Read More...

Sunday, April 17, 2011

Why is DI important?

In the past week, I was asked by this question. Most people use OO strategy to write their codes. Each class or object has its encapsulated private data member and methods. They do try to separate business logic clear and narrow down only the jobs the class cares. What is Dependency Injection(DI) and why do we use DI?

I asked a similar question to another senior developer, "where do you use DI?" He told me that he only uses it in unit test as mocked objects to avoid real access to database. That's very common in unit tests. However, that's not enough. Actually DI is a very important concept and should be a common practice in our daily coding.

I recalled my previous projects and went through the pattern I used. Here is my further explanation. Take the following codes as example:

public class MyDataReader {
...

public bool ReadData() {
var dbService = new MyDbService();
....
var data = dbService.GetObject();
....
}
}


The question code is the place where dbService object is created. How many times we have seen this type creation in our classes. The class MyDataReader has dependency on MyDbService class. In other words, it knows what db service class to use. Does MyDataReader class need to know that? Isn't that better if separate this specific logic from MyDataReaer?

Now look at the following updated codes:

public class MyDataReader {
...

public bool ReadData() {
var dbService = DIContainer.GetInstance<IDBService>();
....
var data = dbService.GetObject();
....
}
}


The only change is that MyDataReader does not have knowledge of what concrete db service is any more. It passes the job to a container class to get an instance of db service which implements the interface of IDBService. If you make changes in the container, then the instance you want to be used will be able to inject to MyDataReader class. You don't need to change MyDataReader class at all. This is a typical case of DI.

By implement DI pattern, your class will be truly OO. Less coupling is a critical principle of OO. You should always to use DI container as much as possible to separate the relationship between concrete classes. To do DI coding, you do need to create your container class and following your pattern to register the relationship between interface and class.

Fortunately, there are many great DI patterns available. I wrote blogs on Structure Map and MEF. Those are great DI Containers. For sure, you need to learn those patterns and ways they require, but I think it is worthwhile. Write better and clean codes will benefit you and your applications.

Read More...

Sunday, April 10, 2011

Two Useful Excel Functions

I often use Excel as a tool for some tests or data analysis. For example, last week I tried to comparing two columns in data base to see their difference. I found out that I had an Excel as a tool to do that. This file contains some formula for column comparison. Here is summary what I did for my future reference.

First, I copied column from table 1 to a worksheet(table1), and another column from table 2 to another worksheet(table2). Then I added a formula like this in column B:

=MATCH(A2, Table1!A2:A2409, 0)


MATCH function is useful to find first parameter value from the second parameter array, and the third parameter 0 is used to find a match. The result will be the index in the array if item is found, or #NA if nothing is found.

The result will be index number or #NA. I prefer to view the result as true or false. ISNA() is a function for this purpose.







above are snapshots of NeoOffice at my iMac.

Read More...

Saturday, April 02, 2011

Broken MSCOMRT2.OCX Caused Excel Add-in Exception

Last week I spent almost two days to resolve one issue. Excel add-in throw exceptions after an enterprise application update was installed. This happened initially in a product our team supports. The product is from a third party application suite by company C, where one Excel add-in is part of.

The symptom of the issue is that when Excel is opened, an exception is displayed right away. This one is caused by an add-in provided by the company C.



I contacted with the company which provides support the product. What they did was to remove monthview control from the add-in. It did not cause Excel error after that, however, I realized that this was not the right way to resolve the issue. The monthview is a common control from VB 6.0 control. Soon I found this control is MSCOMRT2.OCX, and there are some plains about its broken.

Soon I got calls from other department about the similar exceptions in Excel. They use different add-in and I realized that the root is to fix the control. After many discussions with other team member, I narrowed my focus on this control. I was right on this issue. I found a fix from Microsoft support knowledge base. The fix contains a cab file with only those two files:

mscomct2.inf
mscomct2.ocx


What I did was unregister the ocx file in %systemroot%/system32 first,



Then I copy those files to the path and register the ocx:



The fix seems very simple. However, at first no one knows the root of the issue. The update deployment team was afraid of the update would cause many user apps not working. I think the team work and web search played important role to get down the solution. Based on our communications on the issues and information we got, I finally found the right package as a fix.

Read More...