Skip to main content

Posts

Showing posts from 2012

Compressing and DeCompressing a File in C#

Methods to compress file and decompress file using System.IO.Compression Method to compress a file. public void CompressFile ( string sourceFile, string destinationFile ) { // Check File exists in the source path if ( File .Exists ( sourceFile ) == false ) throw new FileNotFoundException ( ); // Create the streams and byte arrays byte [] buffer = null ; FileStream sourceStream = null ; FileStream destinationStream = null ; GZipStream compressedStream = null ; try { // Read the bytes from the source file into a byte array sourceStream = new FileStream ( sourceFile, FileMode .Open, FileAccess .Read, FileShare .Read ); // Read the source stream values into the buffer buffer = new byte [sourceStream.Length]; int checkCounter = sourceStream.Read ( buffer, 0, buffer.Length ); if ( checkCounter != buffer.Length ) { throw new ApplicationException ( ); } // Open the FileStream to write destinationStream = new FileStream ( destinationFile, FileMode ...

Inside Entity Framework: Lazy Loading, Explicit Loading and Eager Loading

When working with Entity Framework, it is important to understand how to hit the database and get the data especially to avoid the performance issues with the applications. That is exactly what I’m going to explain here that there are several ways to hit the database and load the related entities to retrieve the data. Also it is purely developer’s choice which one to use depending on the context to improve the performance. Lazy Loading: With lazy loading enabled, related objects are loaded when they are accessed through a navigation property. The default value of LazyLoadingEnabled is false. However, if we use the EF tools to create a new model and the corresponding generated classes, LazyLoadingEnabled is set to true in the object context's constructor by default. In this type of loading, each navigation property that we access causes a separate query to be executed against the data source. Here is the simple example which is based on the assumption that there are three tables O r...

Disposing WCF service after invoking in .NET

There are so many ways available in .NET to invoke WCF services. In this article I will explain how to dispose after consuming or invoking WCF services. There are three most commonly used ways to consume a WCF Service in a Service Oriented Application built on the WCF Infrastructure which are as follows: a) Referencing WCF Service from VS.NET Studio 2010. b) Executing ServiceModel Metadata Utility Tool (Svcutil.exe) and retrieve metadata from a WCF service and use it to create a WCF proxy that can access the service. c) Using ChannelFactory class used in advanced scenarios that can be used to create more than one channel(to create multiple endpoint listeners). There are other approaches which are more advanced and less common to all. One such approach is generating proxy using the Mex service endpoints at runtime and another, worth mentioning is using the discovery API. No matter what approach is being used, the code block must always be executed the Dispose method so that it improves...

Code Contracts - .NET Features

How do you make sure your method guarantees to operate correctly under defined expected input states? Well Code Contracts may help you. One of the fine features provided by .net 4.0 is Code Contracts. First it’s worth thinking a little about what contract means to you. They indicate the expected input states under which the method guarantees to operate correctly. Contracts act as checked documentation of your external and internal APIs to be shipped. The code contracts are used to improve testing via runtime-checking, enable static contract verification. One of the interesting features of Code Contracts is that it includes a MSIL rewriter (ccrewrite.exe) that post-processes an assembly to change the intermediate language instructions emitted by the compiler. Another great feature of Code Contracts is that you can turn static analysis on and off on a per project basis. I believe this will be important to anyone practicing TDD and BDD. A code contract follows design principle of Design B...