Category: C#

Articles and concepts related to C# language

0

Simple C# Application to Remove/Clean Unused/Orphaned Resource file entries

In most of the present day code development, Application Localization is being achieved using RESX files. As time progress, we might not be able to keep track of which key-value pairs of a RESX file are being consumed by the application and which are orphaned. At present, it is very difficult to remove the orphan entries through automation and most of us stick to manual clean up. There are some tools which are capable of cleaning RESX files, but they come with price tag. In this quick tutorial, I am going to show how to clean a resource file through regular C# code.

1

JumpStart # 17–Difference between IEnumerable<T> and IQueryable<T> in EntityFramework SQL

IQueryable basically generates the complete expression tree and then executes it to get results, where are it is not true in IEnumerable scenario. But still IQueryable is inherited from IEnumerable. IQueryable’s extension methods take expressions based on which expression trees are build, while for IEnumerable’s extensions methods takes functions based on which results are enumerated and published. From afore said reasons, IEnumerable is better on Collections stored in memory, where as IQueryable can optimize performance of Remote Data Sources like Databases.

0

JumpStart # 16–Lazy Loading using Lazy<T> in C# 4.0

From C# 4.0, .Net Framework supports Lazy initialization of instances. Lazy initialization helps in loading instances (especially useful for costly properties of objects) only when they are accessed at runtime. That means lazy properties are not loaded when actual object initialization is done.

2

JumpStart # 15–Unobstructive Validation in ASP.Net 4.5

ASP.Net 4.5 supports UnObstructive JavaScript validation on client side with UnobtrusiveValidationMode=”WebForms” attribute on page level. With this advancement, page size will reduce drastically for a page which contains large number of validation controls.

Unobstructive Validation removes inline JavaScript which is generated by regular ASP.Net Validators, instead it uses HTML5 Data attributes to perform validation.

0

Parallel Programming in C#

1. Use Parallel.For (introduced in .Net 4.0) for parallel processing on multiple threads.
2. When a iterative long running operation needs to be performed, then using Parallel.For will help in spanning these iterations on multiple threads and on final stage combining all successive inputs.
3. There is also a provision for breaking/stopping these iteration by setting up LoopState.
4. We can also set the initial value (as a param) for all threads which are spanning across.

1

C# Design Pattern–Factory Method

Factory Method Pattern provides an interface for creating objects. Sub classes under Factory Class are going to decide which object type needs to be instantiated.

0

JumpStart # 14–Strongly typed Data Controls in ASP.Net 4.5

ASP.NET 4.5 provides an ability to declare the data type of the data that a control is bound to throught ItemType property. When we set this property, two new typed variables are available in the scope of data-binding expressions: Item (One Way binding) and BindItem (Two Way Binding).

In previous versions, Data binding expressions are evaluated at runtime using reflection and bind data, which is not only developer nightmare but also encourages more data shaping problems.

0

JumpStart # 13–Async HttpHanlders in ASP.Net 4.5

ASP.NEt 4.5 introduces Async processing of Asynchronous HttpHandler using “HttpTaskAsyncHandler” abstract class and then override “ProcessRequestAsync”. HttpTaskAsyncHandler implements both IHttpAsyncHandler, IHttpHandler. In this new way, ASP.Net takes most of the overload of managing threads in continuing async tasks by itself.

0

C# Design Pattern–Facade

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

5

Dropdownlist Validation in ASP.Net MVC 3–Razor

In this short tutorial, I am going to show how to validate a Dropdownlist in MVC3 using Razor Syntax. Our Model Class – using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using...

4

JumpStart # 11 – Xml Comments for code using GhostDoc Extension

Documentation of code, arrrgh!!! :-) Most of us hate it, but believe me its an important part in SDLC. Especially when it comes to maintenance of legacy code, Documentation is a life savior.

Writing Xml Comments in Visual Studio is not going to be a easy going task (for large projects it’s a nightmare too). So I started using GhostDoc, an extension of Visual Studio, which helps and makes Xml commenting comparatively easier. There are many other extensions available which give the same functionality in writing comments, but in this small JumpStart I would like to show how to get started with GhostDoc.

11

ASP.Net MVC–Simple Application Security using Password Hashing before storing it to Database

The intended application got some secured products in the Sql DB (here the table is Products). Users can login to the DB and can only retrieve products thorough MVC Application. So User first needs to Register to the application, then he can login to the application to retrieves the secured information of the products.

The main attention of the complete process flow is “Registration”, especially Password field. Storing Password as a string into DB is considered as not a good practice, so first we generate a Password Salt using Cryptography, then we use the original Password String along with Password Salt to hash it into a more secured format using a Hashing algorithm. and finally it is stored in the Users table of Sql DB.

While Authenticating User, we get the Password String from User login, then check the DB for Username, get the User Entity from the DB, fetch the Password Salt associated with the User, Combine the Password string and Password Salt to generate the Password Hash, then finally cross check the generated Hash with that of the DB version. If both matches, Authenticate the user or else notify him as Invalid Login.

3

Implementing MVP Pattern using UserControls with WCF Database Operations in Entity Framework and LINQ – Part III

In this part of the tutorial, we carry on with the construction of MVP Pattern. First we start with the creation of View interfaces for the UserControls (which we created in PART II). Then we create the presenter classes, which will take the corresponding instances of views and then manipulate the view using Model classes (there by achieving code portability). Once we are done with the presenter classes, we implement the View interfaces in the codebehind of the UserControls. Finally we create a sample ASP.Net web page (typically webform), and use the UserControls for database transactions.

4

Implementing MVP Pattern using UserControls with WCF Database Operations in Entity Framework and LINQ – Part II

In this part of the tutorial – We first code for the CustomerService.cs by implementing ICustomerService.cs interface. We use LINQ queries to code database transactions. Then we create a host application (here it is a Console application) which operates the WCF service. Then we create a sample website for implementing MVP Pattern to access WCF. In the website, we implement some UserControls for fetching/updating/deleting records from the database.