C# LINQ Samples and content for Beta 1
VB LINQ Samples for Beta 1
Namoskar!!!
From 4GuysFromRolla
This article is one in a series of articles on ASP.NET 2.0's membership, roles, and profile functionality.
SqlMembershipProvider
aspnet_regsql.exe
SqlProfileProvider
As you know with Orcas Beta 1 we have Linq to SQL template available. There we can drag and drop any database table and the code will be ready for you to use. What if you are not a wizard addicted like me and wants to write your own clean code?
I love the conciseness of the C# 3.0 and the lesser code strategy. Let’s try to write some clean code which will allow us to use the relationship also. I will use Northwind and will use Products and Category table. But I am interested in only few selected columns
Products
++++++
ProductId
ProductName
CategoryID
UnitPrice
Category
CategoryName
I will write my own Linq to SQL code and use my own mapping. You always need to use two major namespaces
using System.Data.Linq;
using System.Data.Linq.Mapping;
I will create a mappings between my class property and SQL columns. For that I need to use couple of attributes. Let’s try that out
#region Products Class
[Table(Name="Products")]
public class Products
{
[Column(IsPrimaryKey=true)]
public int ProductId{get;set;}
[Column]
public string ProductName { get; set; }
public int CategoryID { get; set; }
public decimal UnitPrice { get; set; }
}
#endregion
#region Categories Class
[Table(Name = "Categories")]
public class Categories
[Column(IsPrimaryKey = true)]
public string CategoryName { get; set; }
Now I am ready to go.
Let’s suppose I have one console application, so the code will look like,
static void Main(string[] args)
DataContext db = new DataContext
(@"Data Source=BLR2B03-A\SQLEXPRESS;Initial Catalog=Northwind;");
var products = db.GetTable<Products>();
var categories = db.GetTable<Categories>();
var q = from p in products
join c in categories
on p.CategoryID equals c.CategoryID
select new { p.ProductName, c.CategoryName };
ObjectDumper.Write(q);
That’s all. Isn’t it much cleaner code? Happy weekend.
Why LINQ (or other components of Linq). One of the major reasons is that to address the mismatch between relational data domain and object domain. Database was more comfortable for developers from SQL world and they were no where in object domain.
Please visit Comparing LINQ and Its Contemporaries for more details.
When we consider managed code in our application we also have to think about the performance. .NET has its own way of fine tuning things but just by remembering couple of things we can prevent the unnecessary push backs from our end.
One of the biggest concerns is that why Generics? It is strongly typed, it is type safe. Because of this it also reduces unnecessary Boxing and Unboxing which can be memory intensive. Another thing is that because of snippet feature just by typing ~ we can put destructor in our class and if there is nothing in the body of destructor, the natural life cycle of garbage collector may get hampered.
Such tips and tricks are available in MSDN
Writing Faster Managed Code: Know What Things Cost
Writing High-Performance Managed Applications : A Primer
Garbage Collector Basics and Performance Hints
Performance Tips and Tricks in .NET Applications
Hope this will be helpful.
Project codename "Astoria" is to enable enable applications to expose data as a data service that can be consumed by web clients within a corporate network and across the internet. The data service is reachable over HTTP, and URIs are used to identify the various pieces of information available through the service. Interactions with the data service happens in terms of HTTP verbs such as GET, POST, PUT and DELETE, and the data exchanged in those interactions is represented in simple formats such as XML and JSON. As mentioned in ADO.NET Team's BLOG. You can also visit the entry by Pablo Castro.
Documentation is available for download,
Microsoft Project Codename "Astoria" Overview Document
Microsoft Project Codename "Astoria" - Documentation
Check out the Astoria at http://astoria.mslivelabs.com
Formally known as WPF/E, Silverlight is a cross-browser, cross-platform plug-in for delivering the next generation of Microsoft .NET–based media experiences and rich interactive applications for the Web.
Find out more resources from MIX. To be updated keep on checking Microsoft Silverlight
Microsoft decided to launch ADO.NET Entity Framework and Tools during the first half of 2008 as an update to the Orcas release of the .NET Framework and Visual Studio.
For more in details please refer
ADO.NET Entity Framework Update
Microsoft’s Data Access Strategy
MIX07 sessions are now available for offline viewing. This download is available in both wmv and for Zune or else you can play it using Silverlight (the scale down version of WPF known as WPF/E).
http://sessions.visitmix.com/
Recommended session,
DEV04 - Using LINQ to Dramatically Improve Data Driven Development in Web Applications by Anders Hejlsberg
What is ADO.NET vNext or Entity Framework? It has been a long journey for the application development and data access. Starting from DLib,… DAO, ADO, ADO.NET. Now the world is for ADO.NET vNext or Entity Framework model. This is to increase the level of abstraction and avoid the change mismatch. In Orcas the designer is available for wizard. Let us see how we can leverage that,
Create a Windows Application using Orcas Beta 1. Then from the Solution Explorer add “ADO.NET Entity Data Model”.
Then choose the option “Generate From database”.
Then point to the database you are targeting.
Then choose the Tables
Now add GridView and a Button (to load the data from database). Add the following code in your ButtonClick event. You have to use using NorthwindModel;
private void button1_Click(object sender, EventArgs e)
NorthwindEntities db = new NorthwindEntities();
var q = from c in db.Customers
select c;
dataGridView1.DataSource = q.ToList();
Now you run the apps to see the output.
Let me explain you what it is. It creates couple of files when you use wizard for Entity. In the file Model1.cs it creates a context class with the name NorthwindEntities and we have to initialize this class with the connection string. By default it takes the connection string located in Apps.config (which gets there while choosing the database).
You can add search queries like,
where c.City == txtCity.Text
In txtCity you can pass dynamic input at runtime.
Hope you have enjoyed this.
I though that I must dedicate my first blog entry which I have used for this blog. You can get this from http://get.live.com/betas/writer_betas and enjoy the power.
You might face the same issue as I faced today. I was trying to generate an Entity Data Model using the wizard and the code file was almost empty. So I was not able to implement that model to display the records.
This is a known issue and the fix is available at Microsoft. Please go ahead and download the fix from http://www.microsoft.com/downloads/details.aspx?FamilyID=f69e9eb8-0ebd-4fba-a4cc-2050297ba75b&DisplayLang=en and enjoy the feature.
A nice site at http://www.dofactory.com/Patterns/Patterns.aspx
Creational Patterns
Abstract Factory
Creates an instance of several families of classes
Builder
Separates object construction from its representation
Factory Method
Creates an instance of several derived classes
Prototype
A fully initialized instance to be copied or cloned
Singleton
A class of which only a single instance can exist
Structural Patterns
Adapter
Match interfaces of different classes
Bridge
Separates an object’s interface from its implementation
Composite
A tree structure of simple and composite objects
Decorator
Add responsibilities to objects dynamically
Facade
A single class that represents an entire subsystem
Flyweight
A fine-grained instance used for efficient sharing
Proxy
An object representing another object
Behavioral Patterns
Chain of Resp.
A way of passing a request between a chain of objects
Command
Encapsulate a command request as an object
Interpreter
A way to include language elements in a program
Iterator
Sequentially access the elements of a collection
Mediator
Defines simplified communication between classes
Memento
Capture and restore an object's internal state
Observer
A way of notifying change to a number of classes
State
Alter an object's behavior when its state changes
Strategy
Encapsulates an algorithm inside a class
Template Method
Defer the exact steps of an algorithm to a subclass
Visitor
Defines a new operation to a class without change
Do you know there is another language called Cω? This is still is experimental stage and the future plan is yet to be decided.
This is targeted for concurrent programming. The approach will be more easy and user friendly. Application programmers also will be able to implement on concurrent programming which now only the game for highly efficient group of people who develop OS or Database.
For more visit http://research.microsoft.com/Comega/doc/comega_whatis.htm