|
Become the worlds fastest developer |
|
|
|
|
Written by J. Ingolfsson
|
|
Monday, 16 November 2009 21:59 |
|
Has this ever happened to you? You need a function that takes as a parameter a Foo and outputs a Bar. Being the intelligent and gung ho programmer you are you immediately see a solution to the problem. You start programming and after 6 hours of work you emerge from your office with a TransformFooToBar (Foo FooInstance) function.
When you prodly show the function to a colleague instead of being duly impressed he blurts rudely out "Why didn't you use the function System.Foo.Foo2Bar function?" It turns out that there is already a function that does more or less exactly what you just spent 6 fun but ultimately wasted hours on.
|
|
Last Updated on Friday, 18 December 2009 15:59 |
|
Increase application performance using multiple resultsets |
|
|
|
|
Written by C.M. Mortensen
|
|
Sunday, 15 November 2009 13:52 |
|
It is quite common for any non trivial Web application page to require data from more than one table.
The "natural" thing then is to write code that makes more than one connection to the database server and requests data more than once, even several times. While easier to program, this style does not scale very well connecting and fetching data is a costly operation and decreases the maximum number of requests per second you application can handle. This was the bad news. The good news however is that we can return multiple resultsets in a single database request and thus minimize the time and resources we use in communication with the database.
I consider the best way to return multiple resultsets to be using stored procedures, there are other ways such as using dynamic SQL but for securit, performance and maintenance reasons I prefer stored procedures. Using stored procedures also limits the amount of data we have to send over the wire resulting in lower memory requirements on the web/application server.
The following simple example uses SqlCommand and its corresponding ExecuteReader method to fetch data. To get the data for the next control we call simply call NextResult.
// sqlcommand is an instance of SqlCommand // get the first resultset datareader = sqlcommand.ExecuteReader();
// read the data from that resultset // GetCustomerOrderDataFromReader is a function // mapping datareader data to business class
while (datareader.Read()) { CustomerOrders.Add(GetCustomerOrdersFromDataReader( datareader)); } // now we fetch the next resultset datareader.NextResult(); // read the data from that second resultset while (datareader.Read()) { OrderData.Add(PopulateOrderLinesFromDataReader( datareader)); } // and so on
|
|
Use the power of partial classes |
|
|
|
|
Written by P. Singh
|
|
Saturday, 14 November 2009 20:38 |
|
Among the many improvements Microsoft introduced in .Net 2.0 are partial classes.
What this basically means is that you can use more than one physical file for the source code of a class, you can have a nearly unlimited number of files but must of course have at least one. This feature has several uses. We can separate different sections of the class for example Interface code and business logic code. This also allows us to extend classes that are generated automatically.
In this article we are going to explore this feature with a closer look on how Visual studio helps and leverages partial classes
|
|
Last Updated on Saturday, 14 November 2009 21:34 |
|
Databound controls and BindingContext |
|
|
|
|
Written by P. Singh
|
|
Friday, 13 November 2009 23:23 |
|
I recently came across this problem. I had two controls on the same form a ListBox and a Combobox that needed to use the same datasource. This had the effect that when an item is selected in one control also selects the same item in the other control which is not what I had in mind at all.
The solution is to change the BindingContext of at least one of the controls.
|
|
Last Updated on Tuesday, 17 November 2009 11:27 |
|
Reduce code duplication in constructors by calling Base Class Constructors and Constructor Chaining |
|
|
|
|
Written by P. Singh
|
|
Thursday, 12 November 2009 19:20 |
|
As we have looked at earlier, constructors are functions used for creating instances of classes for example a constructor for the class Car might be
public Car { string Manufacturer , string Model, DateTime CreationDate, int Doors, string Color) { /* Initialization code */}
When creating a class instance C# automatically calls the base class constructors, C# also allows you to explicitly call the constructor(s) of the base class. It is assured that the call to the base class constructor(s) takes place before any initialization in the derived constructor. This makes sure that the derived constructor doesn't try to use members that haven't yet been initialized. Therefore you do not need to duplicate any initializing functionality that is already in place in the parent class while allowing you to easily override the parts you want.
Even more usefully C# provides a way to call a constructor from another which allows one to reduce the amount of code duplication that can occur in constructors. This practice is typically called constructor chaining.
In the article we are going to look at ways to reuse constructor code both within a class and between different classes.
|
|
Last Updated on Saturday, 14 November 2009 22:18 |
|
Improve your classes with Copy Constructors |
|
|
|
|
Written by J. Ingolfsson
|
|
Tuesday, 10 November 2009 23:18 |
|
As you may know Constructors are class functions that are used to create new instances of classes.
Constructors have the same name as the class and have no return value.
You can have more than one constructor with different parameters and do all kinds of initialization in them.
One common and useful type of constructor is the so called “Copy constructor” it takes its parameter an instance of the class and uses that to create another “identical” instance. The new instance is a “real” copy with no reference to the original.
|
|
Last Updated on Thursday, 12 November 2009 19:35 |
|
Decrease .net verbosity with the "using" keyword |
|
|
|
|
Written by J. Ingolfsson
|
|
Wednesday, 11 November 2009 19:55 |
|
Most applications and systems aren't just written and then forgotten about. They go through regular cycles of design, programming, bug fixing and maintenance. To ease this process it is important that program readability is high. Program readability varies from language and even varies within the same language depending on the approach taken.
While it is important to use descriptive names for namespaces, classes, functions and variables the combination of all this can lead to what may to some people seem like extreme verbosity, especially if you are used to terse syntax.
|
|
Last Updated on Thursday, 12 November 2009 22:49 |
|