Database BLOB Compression using C# and SharpZipLib
Posted @ Sep 05, 2008 10:40 AM | Permalink | 0 Comment(s)
Sometimes you just have to save files in the database. The reason? Because "Simon" said so. So without further ado, here is a simple example on how to compress files using C# and SharpZipLib:

1
2 using System;
3 using System.IO;
4 using ICSharpCode.SharpZipLib.Zip.Compression;
5 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
6
7 namespace YourNamespace {
8 public class GenericCompressor {
9 public static byte[] Compress(byte[] buffer) {
10 MemoryStream ms = new MemoryStream();
11
12 Stream stream = new DeflaterOutputStream(
13 ms,
14 new Deflater(Deflater.BEST_COMPRESSION),
15 buffer.Length
16 );
17
18 try {
19 stream.Write(buffer, 0, buffer.Length);
20 }
21 finally {
22 stream.Close();
23 stream.Dispose();
24 }
25
26 return ms.ToArray();
27 }
28
29 public static byte[] Decompress(byte[] buffer) {
30 MemoryStream ms = new MemoryStream();
31 byte[] data = new byte[4096];
32
33 Stream stream = new InflaterInputStream(
34 new MemoryStream(buffer)
35 );
36
37 try {
38 while (true) {
39 int bytes = stream.Read(data, 0, data.Length);
40
41 if (bytes < 1) {
42 break;
43 }
44
45 ms.Write(data, 0, bytes);
46 }
47 }
48 finally {
49 stream.Close();
50 stream.Dispose();
51 }
52
53 return ms.ToArray();
54 }
55 }
56 }


To compress files in ASP.Net, the code would be something like this:

1
2 byte[] data = new byte[fileUpload.PostedFile.ContentLength];
3
4 fileUpload.PostedFile.InputStream.Read(data, 0, data.Length);
5
6 byte[] compressedData = GenericCompressor.Compress(data);
7
8 // Then save to your favorite database...


Conclusion:


I prefer having this utility over other solutions because I don't have to be tied up to a specific database vendor and of course, no database component or assembly to install. No hassle.

LiteSqlHelper - yet another .Net data access helper
Posted @ Sep 03, 2008 08:02 PM | Permalink | 0 Comment(s)
LiteSqlHelper is a lightweight data access helper that I developed sometime ago. It's a good replacement to SqlHelper or DAAB. It's simple to use, extensible and free (without limitations).

Source code and samples can be found here.



Cheers,

Chris
C# 3.5: Extending System.String
Posted @ Sep 01, 2008 01:36 AM | Permalink | 0 Comment(s)
With the new extension method feature of C# 3.5 it's now easy to extend your classes for additional functionality. Here are some useful functionalities to add to your favorite System.String:

1
2 // Sample namespace
3 namespace CompanyName.ProjectName.Common {
4 public static class StringExtension {
5 public static string Left(
6 this string text,
7 int length
8 ) {
9
10 if (text.Length < length) {
11 return text;
12 }
13
14 return text.Substring(0, length);
15 }
16
17 public static string Right(
18 this string text,
19 int length
20 ) {
21
22 if (text.Length < length) {
23 return text;
24 }
25
26 return text.Substring(
27 text.Length - length,
28 length
29 );
30 }
31
32 public static string Mid(
33 this string text,
34 int start
35 ) {
36
37 return Mid(
38 text,
39 start,
40 text.Length - (start - 1)
41 );
42 }
43
44 public static string Mid(
45 this string text,
46 int start, int length
47 ) {
48
49 if (text.Length < length + 1) {
50 length = text.Length - 1;
51 }
52
53 return text.Substring(start - 1, length);
54 }
55 }
56 }
57


Sample usage would be like this:

1
2 string text = "ABCDE";
3
4 Console.WriteLine(text.Left(1));
5 Console.WriteLine(text.Right(2));
6 Console.WriteLine(text.Mid(3));
7 Console.WriteLine(text.Mid(3, 1));
8
9 // Output:
10 A
11 DE
12 CDE
13 C
14

ASP.NET: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack
Posted @ Aug 29, 2008 01:52 AM | Permalink | 0 Comment(s)
ThreadAbortException:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.


If you are using Response.End, Response.Redirect or Server.Transfer somewhere in your code and you encounter this error , please check this solution from Microsoft.
C#: "finally" block vs StackOverflowException
Posted @ Aug 26, 2008 06:59 PM | Permalink | 4 Comment(s)
Taken from the MSDN documentation:

The finally block is useful for cleaning up any resources allocated in the try block as well as running any code that must execute even if there is an exception.

So in other words whatever code you have in your "finally" block will always execute when an exception occurs, right? Wrong. The correct answer is - it depends on the error.

Let me give you a simple example:

1
2 static void Main(string[] args) {
3 try {
4 Console.WriteLine("Try block");
5
6 Recurse();
7 }
8 catch {
9 Console.WriteLine("Catch block");
10 }
11 finally {
12 Console.WriteLine("Finally block");
13
14 Console.ReadLine();
15 }
16 }
17
18 static void Recurse() {
19 Recurse();
20 }
21


The System.StackOverflowException is an exception that will NOT execute codes in your catch and finally blocks.

Hope this helps.
Blog changes
Posted @ Aug 26, 2008 10:13 AM | Permalink | 0 Comment(s)
I am re-organizing things on my blog so there might be some changes in the tags and links or maybe the layout.

I am also adding a new "Project" section where I will be posting some of the projects I created as open source. Feel free to use them without any limitations.
My Philippine Job Site Listings
Posted @ Aug 04, 2008 12:03 AM | Permalink | 1 Comment(s)
My bookmark of job sites and recruitment firms in the Philippines:

Philippine Job Sites


Philippine Recruitment Firms


Apologies to those firm/sites that are not on the list. If you want to be listed please send me an email containing your company name and website here.
ADO.NET Entity Framework Vote of No Confidence
Posted @ Jun 27, 2008 06:20 AM | Permalink | 0 Comment(s)
Over the past year, Microsoft and the Entity Framework team have received a tremendous amount of feedback from experts in entity-based applications and software architectures on the .NET platform. While Microsoft’s announcement of its intention to provide framework support for entity architectures was received with enthusiasm, the Entity Framework itself has consistently proved to be cause for significant concern.


Read the petition here or discuss it in Tim Mallalieu's Blog.
May 16, 2008: XDA Friday Session
Posted @ May 20, 2008 06:25 AM | Permalink | 2 Comment(s)
A little gathering took place last May 16, 2008 in Rod's condo. 'Twas fun!!! Here are some pictures of the gang.



Charm, girl in red, on her 10th cup of Bacardi :-P





Knights of the rectangle table





Some random pose with the Ryan's better half - "The Doc"





Hey, Olem's drunk and Jen's crying :-P





4 alcoholic bastards watching Angel Locsin's Lobo on TV, darn!





PeeGees, my friends...;-)


SOA vs BPM: Why compare?
Posted @ May 15, 2008 08:05 AM | Permalink | 0 Comment(s)
I just read the news in InfoQ about "Should your architecture focus on SOA or BPM?" and found the title misleading. I don't know if this is just a hang over from my vacation or if it's the excitement of tomorrow's Friday session but it seemed that I really can't understand why SOA was being compared to BPM? Can someone shed a light?

SOA is about software architecture and BPM is Business Process Management. From the definition alone you can already understand that the two are different. And by the way, the same goes with OOP vs SOA...

Anyway, I will let this pass so that I can enjoy tomorrow's session. Peace!