Skip to main content

Posts

C# Generic Factory

Implement Factory pattern using generics     public interface IDoWork   {       string DoWork();   }     Declare an Interface first by abstracting the common  functionality    Here I am taking the example of DoWork     public class Manager : IDoWork   {     public string DoWork()     {         return "Manager Manages School" ;     }   }     Implement the IDoWork in concrete classes as shown      public class Teacher : IDoWork     {         public string DoWork()         {             return "Teacher teaches student in school" ;         ...

JQuery Mobile - Dialog demo

Dialog is used in JQuery Mobile to show user information or to get user response as similar to confirm in javascript. The JQuery Mobile code sample to achieve the same feature as Confirm in javascript is as below <!DOCTYPE html> <HTML>        <HEAD>         <TITLE>Dialog Demo</TITLE>         <meta name="viewport" content="width=device-width, initial-scale=1">         <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css"/>         <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>         <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>     </HEAD>     <BODY>         <div id=...

Enable Mobile Browser Debugging

.   1. Download Node.JS from http://nodejs.org/ 2. Install Node.JS 3. Along with Node.JS H:/> is also installed. 4. Go to H:/> 5. Type in H:/> npm install weinre 6. Type in H:/> cd node_modules 7. Type in H:/ node_modules > cd .bin 8. Type in H:/ node_modules / .bin > weinre –boundHost <IP Address> 9. Go to Firewall 10. Select Inbound Rules 11. Click on “New Rule” on right side 12. Select Port and click on Next> 13. Add port 8080, 8090 in “Specific local Ports” text box and click Next> 14. Enter name and click on Finish 15. Open chrome and enter address http://<IP Address> : 8080

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...