Thursday 4 November 2010

Partial Classes, Interfaces & Generics


Partial classes can be used to split definitions of classes, structs or interfaces over two or more source files. Each source file contains a section of the class definition which are combined at when the application is compiled.


Partial classes can be beneficial in the following situations:

  • Working on large projects, allowing developers to work on different sections of a class without both having to access the same physical file. 
  • Working with generated source code, as code can be added to a class without touching the original source file. 
  • Improve maintainability by logically grouping code.

An interface is a contract between classes. An interface is particularly powerful in C# as it allows multiple inheritance.

Interfaces are abstract types that contain no actual data but simply expose behaviours to the classes that implements them.

Generics use type parameters, which allow classes and methods that can defer specification until they are instantiated.
  • Maximises code reuse, type safety and performance. 
  • Most common use in collections
Read rest of entry

Friday 29 October 2010

C# System Types

The type system in .NET or Common Type System (CTS) divides types into 2 categories:
  • Values Types
  • Reference Types
Value Types

These are simple data types that directly contain their data, for example:
  • Integers
  • Floats
  • Enums
  • Structs
Reference Types

These types store references to objects.  Reference type variables have the ability to refer to the same data, so two variables can point to same object and the object be manipulated by performing operations of both variables.  Examples include:
  • Classes
  • Interfaces
  • Delegates
  • Arrays
Read rest of entry

Thursday 28 October 2010

SQL: Format Dates

Formatting dates in T-SQL is generally a nuisance as no format date function exists. In the past I have seen many developers (including myself) use the DATEPART() functions to split the dates and then manually build up a date string.



Alternatively I have seen developers CAST() the date to format.



Recently however, I discovered the different date formats that come standard in SQL Server as part of the CONVERT() function.


For an excellent thorough list of date formats please visit  http://www.sql-server-helper.com/tips/date-formats.aspx
Read rest of entry

Wednesday 27 October 2010

SQL: Get the length of text, ntext & image


Ever needed to find the lengh of a ntext field in SQL only to be met with the following error message, whilst using the LEN() function?







This can be achieved by simply using the DATALENGHT() function.  DATALENGTH is especially useful with varcharvarbinarytextimagenvarchar, and ntext data types because these data types can store variable-length data.


Note: Compatibility levels can affect return values. For more information about compatibility levels
Read rest of entry

Wednesday 29 September 2010

Getting Started with LINQ to Entities

Introducing the object context, basics queries, eager loading, lazy loading and projection.
In this video, Julie Lerman will show you how to query your database with LINQ to Entities queries against an Entity Data Model. This video uses Entity Framework 4.0 and Visual Studio 2010.


Presented by Julie Lerman.




Context object
var context = new myModel.MyEntities

Basic query
var query = from c in context.Customers select c;
var customers = query.ToList();

Basic filter query
var query = from c in context.Customers where c.customerID==5 select c;
var customer = query.FirstOrDefault();

Eager loading
var query = from c in context.Customers.Include("SalesOrdersHeaders") where c.customerID==5 select c;
var customer = query.FirstOrDefault();

Lazy loading (.NET 4 only)
var query = from c in context.Customers where c.customerID==5 select c;
var customer = query.FirstOrDefault();
lblTest.Text = customer.SalesOrdersHeader.Count

Projection
var query = from c in context.Customers where c.customerID==5
select new { c.CustomerID, c.Firstname, c.Lastname };
Read rest of entry

Tuesday 21 September 2010

Creating an Entity Data Model from a Database

The entity framework is an object-relational mapping (ORM) framework for the .NET Framework. The entity framework separates the conceptual and logical data models, allowing developers to program against the conceptual model rather than the logical model.

This video walks you through the basics of creating an entity data model from an existing database using Entity Framework 4.0 and Visual Studio 2010. You’ll learn to run the Entity Data Model wizard, connect to a database, and select database objects for your model. Once the model has been created, you’ll then learn about the basic features of the model such as the entities, associations and properties.


Presented by Julie Lerman.




Read rest of entry

Saturday 7 August 2010

JSON - An Express Overview

Whilst building a new reporting engine this week I was advised by a friend to consider using JSON as a simple and effective way to store the business rules. Having not used JSON before, I decided to take a quick look and found it to be a very straightforward yet elegant way of storing data.

What is JSON?

JSON (JavaScript Object Notation) is a neat and structured text-based data format that is an accepted alternative to XML.

JSON comprises of two structures:
  1. A set of name/values pairs, which is basically an object. Both the name and value mus be in double quotes.
  2. An ordered list of values, which is basically an array. Values in turn can store strings, numbers, objects, arrays, Booleans and nulls. 
Basic Objects

var jString = { "Firstname": "Joe", "Lastname": "Bloggs" }

document.writeln(jString.Firstname); // Output Joe

Basic Array

var jString = { "Firstname": "Joe", "Lastname": "Bloggs", "Sports": ["Football", "Tennis", "Jujitsu"] }

for (var i in jString.Sports) {
document.writeln(jString.Sports[i]);
}

// Ouput: Football Tennis Jujitsu

Read rest of entry
 

Popular Posts

developer-express Copyright © 2009 Black Nero Blogspot Templatesis Designed by Ipietoon Sponsored by Online Business Journal