November 13, 2009
@ 11:49 AM
I hope everyone in southern California is planning to attend this years SoCal Code Camp in San Diego on 11/21 - 11/22. It's always a great time and lots of free training!

I will be presenting the following sessions and I hope you will attend. Also, check out my new .NET discussion site called DotNet Army!

Building nTier Applications with Entity Framework Services

Learn how to build real world nTier applications with the new Entity Framework and related services introduced in .NET 3.5 SP1. With this new technology built into .NET, you can easily wrap an object model around your database and have all the data access automatically generated or use your own stored procedures and views. Then learn how to easily and securely expose your object model using WCF with just a few line of code using ADO.NET Data Services. The session will demonstrate how to create and consume these new technologies from the ground up. Lots of code!

Slides: Building nTier Applications with Entity Framework Services.pdf (2.88 MB)
Demo Code: EntityFramework.zip (859.84 KB)


dotNetDave's Favorite Programming Tools

This session will focus on my favorite Visual Studio add-ins and other tools that makes programming faster and easier. I will focus on tools that are either free or very affordable. Tool categories include Writing Better Code (easier, faster and correct the first time!), Code Helpers, Documentation (helper and creation), General Utilities and more. These tools are designed to impress your boss and get you home at a reasonable time. Packed full of demonstrations and very few PowerPoint slides! Licenses for some of the 3rd party products I will be demonstrating will be given away (over $1,100 worth), so be sure to attend and bring a business card!

Slides: dotNetDave's Favorite Programming Tools.pdf (1.82 MB)

Building Rich & Interactive Web Applications with ASP.NET AJAX

Learn how to build rich web application interfaces using ASP.NET AJAX and the ASP.NET AJAX Control Toolkit. This new technology makes programming JavaScript into your ASP.NET pages easy, increasing the power and functionality of your applications, reducing round trips to the server, and making it easy to consume web services for dynamic content. In this session you will be introduced to the new client and server controls for ASP.NET and Java Script to learn how to build a rich Web 2.0 experience for your users.

Slides: Building Rich & Interactive Web Applications with ASP.NET AJAX - 2009.pdf (2.36 MB)
Demo Code: AdventureWorksAjax.zip (803.65 KB)

Why You Need .NET Coding Standards (2009)

This session will guide any level of programmer to greater productivity by providing the information needed to write consistent, maintainable code. Learn about project setup, assembly layout, code style, defensive programming and much, much more. We will even go over some real in production code and see what the programmer did wrong in "What's Wrong With this Code?". Code tips are included to help you write better, error free applications. Lots of code examples in C# and VB.NET.

Slides: Why You Need .NET Coding Standards-2009.pdf (3.8 MB)
Demo Code: CodingStandards.zip (245.54 KB)


Pictures and Video

SoCal CodeCamp Fullerton - 2009

Pictures & Video from This Years Code Camp:

Pictures from past SoCal Code Camps:

Video from past Code Camps:


 
Categories: ADO.NET | AJAX | ASP.NET | Code Camp | Csharp | Defensive Programming | Development | dotNetDave | Entity Framework | Generics | LINQ | VB.NET | VS.NET | WCF

I hope everyone in Arizona and southern California is planning to attend this years Desert Code Camp on 6/13 and SoCal Code Camp in San Diego on 6/27 - 6/28. It's always a great time and lots of free training! I will also be selling a limited number of my latest book "David McCarter's .NET Coding Standards" at my sessions for $12, cheaper than the web site (no tax and shipping), please bring exact change or check.

I will be presenting the following sessions and I hope you will attend. Also, check out my new .NET discussion site called DotNet Army!

Building nTier Applications with Entity Framework Services

Learn how to build real world nTier applications with the new Entity Framework and related services introduced in .NET 3.5 SP1. With this new technology built into .NET, you can easily wrap an object model around your database and have all the data access automatically generated or use your own stored procedures and views. Then learn how to easily and securely expose your object model using WCF with just a few line of code using ADO.NET Data Services. The session will demonstrate how to create and consume these new technologies from the ground up. Lots of code!

Slides: Building nTier Applications with Entity Framework Services.pdf (2.88 MB)
Demo Code: EntityFramework.zip (859.84 KB)


dotNetDave's Favorite Programming Tools

This session will focus on my favorite Visual Studio add-ins and other tools that makes programming faster and easier. I will focus on tools that are either free or very affordable. Tool categories include Writing Better Code (easier, faster and correct the first time!), Code Helpers, Documentation (helper and creation), General Utilities and more. These tools are designed to impress your boss and get you home at a reasonable time. Packed full of demonstrations and very few PowerPoint slides! Licenses for some of the 3rd party products I will be demonstrating will be given away, so be sure to attend and bring a business card!

Slides: dotNetDave's Favorite Programming Tools.pdf (1.82 MB)

Building Rich & Interactive Web Applications with ASP.NET AJAX

Learn how to build rich web application interfaces using ASP.NET AJAX and the ASP.NET AJAX Control Toolkit. This new technology makes programming JavaScript into your ASP.NET pages easy, increasing the power and functionality of your applications, reducing round trips to the server, and making it easy to consume web services for dynamic content. In this session you will be introduced to the new client and server controls for ASP.NET and Java Script to learn how to build a rich Web 2.0 experience for your users.

Slides: Building Rich & Interactive Web Applications with ASP.NET AJAX - 2009.pdf (2.36 MB)
Demo Code: AdventureWorksAjax.zip (803.65 KB)

Why You Need .NET Coding Standards (2009)

This session will guide any level of programmer to greater productivity by providing the information needed to write consistent, maintainable code. Learn about project setup, assembly layout, code style, defensive programming and much, much more. We will even go over some real in production code and see what the programmer did wrong in "What's Wrong With this Code?". Code tips are included to help you write better, error free applications. Lots of code examples in C# and VB.NET.

Slides: Why You Need .NET Coding Standards-2009.pdf (3.8 MB)
Demo Code: CodingStandards.zip (245.54 KB)


Pictures and Video

SoCal CodeCamp Fullerton - 2009

Pictures from This Years Code Camp:

Pictures from past SoCal Code Camps:

Video from past Code Camps:


 
Categories: .NET | ADO.NET | AJAX | ASP.NET | C# | Code Camp | Defensive Programming | dotNetDave | Entity Framework | Generics | LINQ | VB.NET | VS.NET | WCF | Web Services

January 9, 2009
@ 03:19 PM
I hope everyone in southern California is planning to attend this years SoCal Code Camp at Cal State Fullerton on 1/24 - 1/25. It's always a great time and lots of free training! I will also be selling a limited number of my latest book "David McCarter's .NET Coding Standards" at my sessions for $11, cheaper than the web site (no tax and shipping), please bring exact change.

I will be presenting the following sessions and I hope you will attend. Also, check out my new .NET discussion site called DotNet Army!

Building nTier Applications with Entity Framework Services

1:30PM Saturday - Room: UH 246

Learn how to build real world nTier applications with the new Entity Framework and related services introduced in .NET 3.5 SP1. With this new technology built into .NET, you can easily wrap an object model around your database and have all the data access automatically generated or use your own stored procedures and views. Then learn how to easily and securely expose your object model using WCF with just a few line of code using ADO.NET Data Services. The session will demonstrate how to create and consume these new technologies from the ground up. Lots of code!

Slides: Building nTier Applications with Entity Framework Services.pdf (2.79 MB)
Demo Code: Building nTier Applications with Entity Framework Services.zip (849.73 KB)


dotNetDave's Favorite Programming Tools

1:15PM Saturday - Room: UH 250

This session will focus on my favorite Visual Studio add-ins and other tools that makes programming faster and easier. I will focus on tools that are either free or very affordable. Tool categories include Writing Code (easier, faster and correct the first time!), Code Helpers, Documentation (helper and creation), General Utilities and more. These tools are designed to impress your boss and get you home at a reasonable time. Packed full of demonstrations and very few PowerPoint slides! Licenses for some of the 3rd party products I will be demonstrating will be given away, so be sure to attend and bring a business card!

Slides: dotNetDave's Favorite Programming Tools.pdf (1.8 MB)

Building Rich & Interactive Web Applications with ASP.NET AJAX

11:30AM Saturday - Room: MH 121

Learn how to build rich web application interfaces using ASP.NET AJAX and the ASP.NET AJAX Control Toolkit. This new technology makes programming JavaScript into your ASP.NET pages easy, increasing the power and functionality of your applications, reducing round trips to the server, and making it easy to consume web services for dynamic content. In this session you will be introduced to the new client and server controls for ASP.NET and Java Script to learn how to build a rich Web 2.0 experience for your users.

Slides: Building Rich & Interactive Web Applications with ASP.NET AJAX - 2009.pdf (2.36 MB)
Demo Code: Building Rich & Interactive Web Applications with ASP.NET AJAX - 20091.zip (702.2 KB) UPDATED!

Why You Need .NET Coding Standards (2009)

2:30PM Saturday - Room: UH 250

This session will guide any level of programmer to greater productivity by providing the information needed to write consistent, maintainable code. Learn about project setup, assembly layout, code style, defensive programming and much, much more. We will even go over some real in production code and see what the programmer did wrong in "What's Wrong With this Code?". Code tips are included to help you write better, error free applications. Lots of code examples in C# and VB.NET.

Slides: Why You Need .NET Coding Standards-2009.pdf (3.46 MB)
Demo Code: Why You Need .NET Coding Standards-2009.zip (94.46 KB)


Pictures and Video

SoCal CodeCamp Fullerton - 2009

Pictures from This Years Code Camp:

Pictures from past SoCal Code Camps:

Video from past Code Camps:


 

Two new technologies, the Entity Framework (EF) and ADO.NET Data Services were released with .NET 3.5 SP1 in August 2008. These two major editions to the framework finally provide true data modeling and an easy way to send the data across a WCF service. Making programming the data tier much easier (kind of). Since these technologies are so new there is not a lot of good information out there and a lot of what you can find is a lot of "fluff"... not much "real world" solutions. Since I have been actively been using this where I work since it came out in a "real" project, I thought I would share what I have learned. This will be a living blog post, meaning that as I find things I will update it. So check back often.

One thing I should mention is that the project I am working on is converting VB6 code to C#. We use SQL Server 2005 with most all of the data access going through stored procedures. Most of the sp's I have seen so far are simple selects and inserts or selects based on a parameter. So these are ideal candidates for just letting the EF create the SQL dynamically.

Entity Framework

Design Considerations

When starting this project I chose to create an assembly just for the Entity Data Model's. This way, down the road it could easily be shared with another applications. I chose the naming scheme for the assembly of: MyCompay.Project.Data.Entities.dll (of course replace "MyCompany" and "Project" with your company name and project name). This works great since by default entity type access is public.

There is one caveat with this, when you configure your entities, connections are created in the app.config file that look like this:

<configuration>

  <connectionStrings><add name="MyEntities" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=MyServer;Initial Catalog=MyApplicationDB;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

</configuration>

This issue is when you reference this assembly in your WCF project (see more below), you will need to move the connection strings information to the web.config file under the <system.web> section.

Creating Your Entity Data Model

When I first started going though the VB6 project, I searched for sp calls or dynamic sql. As I saw these statements I added the appropriate tables to my data model. This worked great until I found the last table that was actually "root" table for the application. For example a Customer table that has a primary key that most if not all other tables rely on. While the designer correctly created the relationships, they never seemed to work correctly. I kept getting errors. So always add the "root" table first when creating your data model.

ADO.NET Data Services

Design Considerations

With the project I'm working on, there are multiple assemblies that will end up needing to connect to the Data Service to retrieve data. Because of this and the need to pass around the same "proxy" object, I decided to wrap the Data Service proxy in a separate assembly. The naming scheme I used for the assembly is: MyCompay.MyProduct.Data.ServiceProxy.dll  (of course replace "MyCompany" and "Project" with your company name and project name). By default the proxy object created to the Data Service is public so it's easily used by any other assembly. Also, configuration is easy since the location of the service is defined in one place in this assemblies app.config file.

LINQ

With the Data Services, LINQ can be used to call the service. This is great for the programmer since there is no need to master REST. Your LINQ statement will be automagically turned into REST. But be forewarned that at this point LINQ does not support all of REST. So you can write your LINQ statement, it will seem correct, but nothing will come back from the service. Most of the time there will be no warning or Exception either. So, if you are not getting data back, look at your LINQ query.

Loading Data

Retrieving data from a data service using LINQ is really simple. In my class I declare:

MyEntities _serviceProxy = new MyEntities();

Client _client;

Then to load, simply do the following LINQ statement:

_client = (from cl in _serviceProxy.Clients where cl.ClientId == this.ClientId select cl).Single<Client>();

This performs a call to the Data Service and returns a single Client object using a REST query simular to this:

http://localhost:4437/Services/MyDataService.svc/Clients('1')

To load child objects simply code the following:

_serviceProxy.LoadProperty(_client, "Accounts");

 

_serviceProxy.LoadProperty(_client, "Categories");

Each of the LoadProperty methods above will create a call to the Data Service. These calls can be batched to reduce network traffic, but you will basically have to write the REST queries manually... something I have not tackled yet.

Data Joins

Joins are not supported in Data Services because they are not supported in REST. But if you need to join data to display it in a grid etc, you can do it like you would do it in EF. For it to work the data will have to be preloaded into memory from the Data Service. The join would look similar to this:

var data = (from a in _client.Accounts

            join ac in _client.Categories on a.CatagoryId equals ac.Id into cat

            from c in cat.DefaultIfEmpty()

            orderby a.Type, a.Number

            select new

            {

                a.Id,

                a.Department,

                a.Number,

                a.Description,

                a.Type,

                a.IsTaxable,

                a.IsActive,

                Code = c == null ? string.Empty : c.Code

            }).Distinct();

 

accountGridView.DataSource = data.ToArray();

In the code above, cat.DefaultIfEmpty() is very important because if there is no match between Account and Category, then no data at all will be returned! Then in the last line of the select, it's also important to check for null, since "c" could be null and will throw an exception if you try to access one of its properties.

Now that you have your data into the grid control (DataGridView) you are going to run into another problem... retrieving the data from the DataSource! Since the data has been loaded as an anonymous type it can't be converted back. Also, since I chose not to show the Id property in the grid, I simply can not look at the first column.

So in my case, I only want the Id value of the row the user clicked on, from there I can use LINQ to look up the Account and what ever else I need to display more detailed data etc. For this we are going to have to use reflection.

private void accountGridView_SelectionChanged(object sender, EventArgs e)

{

     var data = accountGridView.Rows[accountGridView.SelectedRows[0].Index].DataBoundItem;

     var propInfo = data.GetType().GetProperty("Id");

     var propValue = (Int32)propInfo.GetValue(data, null);

 

     LoadAccountDetail((from acc in _client.Accounts where acc.Id == propValue select acc).Single());

}

As you can see in the code above we are using reflection and GetProperty to retrieve the value for Id from the grid DataBoundItem which I then use in the LINQ statement to retrieve the Account object.

Updating Data

One of the great things about the Entity Framework is the built in state tracking. This make is really simple to update, insert and delete object (data) with a single method call. Unfortunately, as soon as you serialize an EF object across the wire using any service including ADO.NET Data Service, this tracking is gone for the most part. Though Microsoft has promised to make this better in upcoming releases, for now we have to do it a little more manually, but still is pretty easy. You just need to remember this when using objects coming from a Data Service.

To update an object, simply change it's properties and then do the following:

_serviceProxy.UpdateObject(currentAccount);

 

_serviceProxy.SaveChanges();

Calling SaveChanges will make a call to the DataService. You can make as many calls to UpdateObject, AddObject and DeleteObject as needed before the SaveChanges call.

Service Methods

What's Not Supported
  • Enum parameters: I wanted to do this to send back different data based on an enum parameter (just like I do in normal programming). This is a perfect job for service methods. Unfortunately, I was told by Microsoft that it's not supported in "this release". They did not indicate if it will be supported in a future release.

"Good" Resources


 
Categories: ADO.NET | Csharp | Entity Framework | LINQ | WCF

If you are interested in using LINQ to read Xml instead of the older way of the XmlDocument and SelectNodes, the code below is a pretty good example. The code takes in the ISO standard file for country names and codes (see sample below) and turn it into a list of Country objects for use in an application including ComboBoxes.

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<ISO_3166-1_List_en xml:lang="en">

   <ISO_3166-1_Entry>

      <ISO_3166-1_Country_name>AFGHANISTAN</ISO_3166-1_Country_name>

      <ISO_3166-1_Alpha-2_Code_element>AF</ISO_3166-1_Alpha-2_Code_element>

   </ISO_3166-1_Entry>

   <ISO_3166-1_Entry>

      <ISO_3166-1_Country_name>ÅLAND ISLANDS</ISO_3166-1_Country_name>

      <ISO_3166-1_Alpha-2_Code_element>AX</ISO_3166-1_Alpha-2_Code_element>

   </ISO_3166-1_Entry>

</ISO_3166-1_List_en>

Before

Here is the original code that has been in use for a few years. It uses the "easy" way of loading and selecting nodes using the XmlDocument object.

Dim countriesXml As New System.Xml.XmlDocument

countriesXml.LoadXml(My.Resources.CountriesXML)

 

Dim list As New System.Collections.Generic.List(Of Country)

 

For Each country As System.Xml.XmlNode In countriesXml.SelectNodes("//ISO_3166-1_Entry")

 

   Dim tempCountry As New Country

 

   tempCountry.Name = country.SelectSingleNode("ISO_3166-1_Country_name").InnerText

   tempCountry.Code = country.SelectSingleNode("ISO_3166-1_Alpha-2_Code_element").InnerText

 

   list.Add(tempCountry)

 

Next

 

After

This code uses LINQ and the new XElement to make it easy to load the xml from the assemblies resources.

Dim reader = XElement.Parse(My.Resources.Countries)

 

Dim list As New System.Collections.Generic.List(Of Country)

 

Dim data = From c In reader.Elements(XName.Get("ISO_3166-1_Entry")) _

           Order By c.Element(XName.Get("ISO_3166-1_Country_name")).Value _

           Select New Country With {.Name = c.Element(XName.Get("ISO_3166-1_Country_name")).Value, _

                            .Code = c.Element(XName.Get("ISO_3166-1_Alpha-2_Code_element")).Value.ToUpper}

 

As you can see this is much cleaner, fewer lines of code and actually faster (1 millisecond). Also you can see in the Select line that I am actually filling the Country object during the query... pretty cool!

Tip Submitted By: David McCarter

This code can be found in the open source dotNetTips.Utility assembly


 
Categories: LINQ | VB.NET | XML

If you are coming to the San Diego .NET Developers Group meeting tonight I hope you will be their early for my talk titled "What’s New In VS 2008 SP1". Lots of new additions to this SP, not just bug fixes. Below is a link to the presentation.

VS2008Sp1.pdf (715.88 KB)
 
Categories: .NET | ADO.NET | AJAX | ASP.NET | Csharp | dotNetDave | Entity Framework | LINQ | MVC | Silverlight | VB.NET | VS.NET | WCF | WinForms | WPF

Everyone should check this out. Lots of info on the new version:


http://go.microsoft.com/?linkid=9369515


The .NET Framework 3.5 Enhancements Training Kit includes presentations,
hands-on labs, demos, and event materials.


 
Categories: .NET | ADO.NET | Entity Framework | LINQ | MVC | News

I found out the hard way in the last two days that an SQL Server CE database does not work properly when creating a typed Dataset with a ASP.NET 3.5 application. Sure, you can create one in the IDE, but the code-on-the-fly will not be generated therefor you can't use it in your code. Actually causes an error if you try to build.

Rec'd an confirmation from Microsoft today that they did not implement this for ASP.NET. Just wanted to warn you so you don't waste an hour plus like I did. If you still want to use SQL Server CE and typed DataSets in ASP.NET you will need to create a separate assembly (which should be done anyway). Oh, won't work with LINQ either.


 
Categories: ADO.NET | ASP.NET | Compact Framework | SQL Server | LINQ