Category Archives: Software Development

Software Development Topics

Code-First Development Workflow

By Tim Daniels
Over the past few weeks I’ve continued using the code-first workflow. At this point, I’m ready to share what I’ve learned and what I’ve developed so far. The code-first workflow enabled me to use the convention over configuration approach in the design and development of a solution. With this approach, I found myself focusing, almost exclusively, on the application of a solution to a business problem. Some thought and effort was given to architecture and application structure, but I did’nt concern myself with database design, or object model design, other than verifying that what was generated by the Entity Framework matched my requirements and expectations. The result was a solution, and code, that is very simple and concise. A complete, normalized relational database, and a complete object model were automatically generated by the Entity Framework.

Here is a brief description of the solution I’ve been developing. I call the application “Action Manager”. It’s a basic project management application written in C#.NET. The application will record and track the number of hours worked on a specific task, by a specific person, on a specific project, for a specific customer. The data is stored in a SQL Server database. For development purposes, I’m using SQL Server 2012, the express edition. The entire application is build on the .NET Framework 4.0.

In designing and developing an application, I first get a thorough understanding of the business domain the application will operate in. Understanding a business domain requires identification, and definition of the entities existing in the domain, and how they interact with each other. These entities are then abstracted into individual object-oriented classes in code. Here is a list of the entities currently identified and defined in the project management domain;

  • Customers
  • Projects
  • People (who work on the projects)
  • Titles (job titles)
  • Tasks
  • Rates
  • Timesheets
  • Timesheet entries

Following are the C# classes representing each of the entities in the project management domain. These classes are completely independent of a database source, the object relationship model (ORM), and any third-party attributes. They are also independent of any base class inheritance. This type of class is sometimes refered to as a plain old common language runtime object, or POCO class. I created one class library project called “Entities” to store these classes.

Convention over configuration
Examine each entity class closely. You will notice that the entities have properties which define relationships with other entities in the domain. For example, The customer class has a property called projects. This property is defined as a generic list of project objects. This establishes the conventional relationship between customers and projects, that is customers can have one to many projects. In the same way, the person class has a property called time sheets, establishing the convention that a person can submit one to many time sheets for work performed on a project. These properties are called navigation properties in the object model.

Notice also, that each class has an ID property defined with an object type of integer. The property is named after the class with a suffix of Id (e.g., CustomerId and PersonId). This naming convention is used by the entity framework to create a unique key column when generating tables in a database. This also facilitates the creation of primary key and foreign key relationships, in the object model and the database.

Entity Framework
The Entity Framework can use the conventional relationships established in these POCO classes to automatically generate an object model, and a relational database schema. The DbContext class is where this can be accomplished. The DbContext class is in the System.Data.Entity namespace. Let’s look at some code to see how this is done.

In the above code snippet I created a class called ProjectManagementContext which is derived from DbContext. I then added a property for each of the classes in my entity class library. The type of each property is DbSet. When developing with the Code First workflow you can define a derived DbContext that represents your session with the database and exposes a DbSet for each type in your model. The DbSet Class represents a typed entity set that is used to perform create, read, update and delete operations for each of the entities defined in the object model of the context.

Here is an illustration of the object model generated by the Entity Framework, based on the conventions defined in the POCO classes of the Entity class library. Notice the Navigation Properties, and how they form relationships between the objects in the model. For example, the one to many relationship between the customer object and the project object, is clearly illustrated here.

Object model generated by the Entity Framework in Code-First Workflow

Object model generated by the Entity Framework in Code-First Workflow


Here is an illustration of the database and database model generated by the Entity Framework.
Database generated by the Entity Framework in Code-First Workflow

Database generated by the Entity Framework in Code-First Workflow

In the near future I’ll be writing about the Entity Framework’s fluent API. I’ll also explore the implementation of the create, read, update and delete methods in the DbSet of entity class.

Here are several helpful links to help you get familiar with Entity Framework Development Workflows and the code-first workflow.

Code-First Development with Entity Framework 5.0

By Tim Daniels
As you know from previous posts, I prefer using an Object Relational Mapping (ORM) framework, as part of a domain driven approach to design and development of a business solution. The Microsoft ADO.NET Entity Framework is an Object Relational Mapping framework that enables developers to work with relational data as domain-specific objects, eliminating the need for most of the data access plumbing code that developers usually need to write. Developers can focus on their application-specific business logic rather than the data access fundamentals.

Recent development in the ADO.NET Entity Framework provide further support for two other software development techniques, which I find very appealing and effective, in design and development of new solutions.

  • Code-First Development Workflow
  • Convention over configuration

The code first approach offers several advantages to the application developer. It allows the developer to focus on domain specific entity classes and how these classes relate to each other in the business model. These classes are completely independent of a database source, the ORM and any third-party attributes. They are also independent of any base class inheritance. These classes are sometimes refered to as plain old common language runtime objects or POCO classes.

The entity classes form a set of conventions which the ADO.Net Entity Framework uses to automatically generate an object model and a SQL Server relational database.

Here is an example of two POCO entity classes. A time sheet object and a time sheet entry object. There is a one to many relationship between the time sheet object and the time sheet entry objects. This relationship is expressed by the TimeSheetEntries property of the TimeSheet class. This is known as a navigational property.

In the near future, I’ll be writing a series of posts on the Code-First development workflow, and then follow-up with a post on how code first ties into the convention over configuration development technique.

Here are several helpful links to help you get familiar with Entity Framework Development Workflows and the code-first workflow.

Windows 8 – Developing Windows Store Apps

By Tim Daniels
I recently upgraded by desktop to Windows 8. I also started learning about developing Windows 8 style apps. I downloaded a great introductory book called “Beginning Windows 8 Application Development” by István Novak, György Balássy, Zoltán Arvai and Dávid Fülöp.

I also downloaded the express versions of Visual Studio 2012. Yes, that’s right, I said versions (plural). There is an express edition for each different kind of application you can develop. Here is the breakdown;

  • Visual Studio Express 2012 for Web
  • Visual Studio Express 2012 for Windows 8
  • Visual Studio Express 2012 for Windows Desktop
  • Visual Studio Express 2012 for Windows Phone
  • Visual Studio Express 2012 Team Foundation Server Express 2012

If you buy the professional edition of Visual Studio 2012, all of these different types of apps can be developed from a single IDE.

My focus is on developing apps for the Windows Store. This is the type of application development covered in “Beginning Windows 8 Application Development”. For this purpose I’m using Visual Studio Express 2012 for Windows 8. As you would expect, Visual Studio is already loaded up with an adequate set of templates to jump start development. The express edition even comes with a Windows 8 start screen simulator, so you can test things like screen orientation on your desktop.

Microsoft has done a great deal of work to extend Windows 8 style development to a very broad developer community. Windows 8 Style app development supports the following programming languages;

  • C++
  • C#
  • Visual Basic
  • JavaScript

User interfaces can be developed with the following languages;

  • HTML5, CSS, and JavaScript
  • XAML (Extensible Markup Language)

XAML based user interfaces can be designed and developed using another free development tool called Blend for Visual Studio 2012 (Windows Store apps). Blend is fully integrated with Visual Studio 2012. This makes it very easy for the programmer to seamlessly collaborate with the user interface designer.

At this time I have mostly focused on developing some very simple Windows 8 Style applications using JavaScript, HTML5 and CSS.  In support of rapid application development, Microsoft has developed a robust set of pre-written JavaScript functions specifically for development of Windows Store apps.

Here is a helpful link on learning to build Windows Store apps.

Visual Studio 2012 and MVC 4

By Tim Daniels
In my previous post, I laid out a set of objectives for pursuing the ASP.net Web API technology, for developing RESTful web servies. I quickly discovered that the Web API technology is actually a subset of the MVC 4 design pattern, which is available as a project template in Visual Studio 2012.

I decided to take a detour of my objectives and explore the MVC 4 design pattern in more detail. I must say, the detour has taken more time than I anticipated, but it has proven to be well worth the extra time. The MVC 4 template in Visual Studio 2012 automatically generates a fully functional website solution. The template contains all of the required components of the Model-View-Controller (MVC) design pattern. The Model-View-Controller (MVC) pattern separates the modeling of the domain, the presentation, and the actions based on user input into three separate classes:

  • Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).
  • View. The view manages the display of information.
  • Controller. The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.

Here is the structural relationship between the three objects.

Model-View-Controller design pattern

The MVC 4 template in Visual Studio 2012 effectively addresses the architectural concerns covered in this blog throughout the development life cycle of the World Of Art Web App;

  • Separation of concerns between database access, user interface and business logic
  • Incorporates the Entity Framework in a code first process, where the developer can code plain old classes to define the domain in the model, and then generate the database through the entity framework
  • The controller object automatically adheres to best practices when handling http request and response actions

The MVC 4 template automates and streamlines much of the development life cycle, by not only generating code, but also providing a well structured scaffolding approach, which causes architectural and design concerns to fade to the background, allowing the developer to focusing more attention on satisfying the functional requirements of the application.

The ASP.Net Web site has an entire section devoted to the MVC 4 template. There is a complete set of tutorials to help you become familiar with all the components and features of MVC 4. I will continue following the MVC 4 tutorial all the way through deployment of sample movie application. After completing the tutorials, I plan to develop an MVC 4 version of the World of Art Web app, and report on how this template improves application design and development inefficiencies.

Visual Studio 2012 and Web API

By Tim Daniels
Visual Studio 2012 recently became available, so I decided to download the Express for Web edition from the Microsoft Products Store. Here is a snapshot of the new look and feel of Visual Studio 2012. See what else is new in Visual Studio 2012.

Visual Studio 2012

Visual Studio 2012 – New look and Feel

Within Visual Studio 2012, I have discovered a new technology called Web API. According to ASP.net website, ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework. The Web API framework appears to be a subset of the MVC 4 design pattern, which is also included in Visual Studio Express 2012. ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and that gives you full control over markup for enjoyable, agile development. ASP.NET MVC includes many features that enable fast, Test Driven Development (TDD) for creating sophisticated applications that use the latest web standards.

As you know from my most recent articles, I’ve been focusing on developing web services based on the Microsoft Windows Communication Foundation (WCF). You may also recall that the web service I developed is based on the REST architectural model. Since the Web API technology is also based on REST, I have decided to add this technology to my repertoire. My objectives are the following;

  • Learn Web API from the online tutorials at the ASP.Net website
  • Develop a prototype web service with Visual Studio 2012
  • Deploy the web service to the could using Microsoft Azure
  • Develop a client app for mobile devices like IPad to consume the web service
  • If all of this works as expected, I will develop a new web service for the World of Art Web App based on Web API

Evolution of the User Interface

By Tim Daniels
At this time I have demonstrated three iterations of the Artist web page in The World of Art Web App. The first iteration, simply called the Artist page, was designed with the ASP.Net Web Form model. In this example, I developed the user interface with three ASP.Net server side controls;

The ASP.Net Web Form model depends on the ASP.Net Engine, in the web server, to generate the actual HTML rendered by the client. The input to the ASP.Net engine is the XHTML elements and attributes defined in the .aspx web form file. This model supports a very rapid development process. However, it is a poor architectural design for a distributed web app, because the user interface and database access are tightly coupled. This tight coupling of architectural elements, makes long term maintenance and further development of the app more complicated, because the impact of change has a broad affect across the application. Here is the XHTML source code of the first iteration of the Artist page;

In the second iteration of the Artist page, which I called Art Entities, I eliminated the SqlDataSource web server control, and created a separation of concerns between the user interface and data access. This is where I introduced the concept of a layered architectural design, with the development of the Data Access Layer, and the Data Model Layer. In the Data Model Layer, I developed an Entity Relationship Model with the ADO.Net Entity Framework, which created a line of clear separation, and decoupling, between data access functions and the SQL Server database.

In this phase of development, the user interface evolved into an external layer of the overall web app design. However, the user interface is still based on server side ASP.Net web server controls. I simply exploited the data binding methods, built into these controls, to create an API to the data access layer. Here is the XHTML and C# source code used to render the Art Entities page of the World of Art Web App;

The ASP.Net Web server controls, used in the first two iterations of the user interface, is a suitable design solution for an enterprise level application, which is running internally to the enterprise. The number of users is limited to the employee and associates base, the application is supported by the internal IT infrastructure of the enterprise as an intranet appliation. The view state and post back model, supported by these web server controls, performs very well in this environment. However, if the application is to be distributed over a wide range of clients, and accessible from the world wide web, a much leaner and flexible architectural model is required. This is a requirement for the World of Art Web App. Therefore, in this latest iteration of the app, I added a web service layer to the architecture, based on the Microsoft Windows Communication Foundation (WCF). Accessing the service layer of the World of Art Web app, was a driving factor behind the design of the third iteration of the user interface.

Accessing the service layer, reverses the flow of content between the client and the server. In the previous versions, where server side controls were used, content was pushed from the server to the client. However, acquiring content from the service layer, requires the client to pull content from the server through an HTTP request.

The third iteration of the artists page, which I called Art Entities from Service, is a very minimal and efficient design, utilizing an HTTP GET Request to retrieve artist data from the web service, and JavaScript to render the artist result set in HTML. I also employed a CSS 3 cascading style sheet to complete the look and feel of the page.

Here is the XHTML source code of the ASP.net web form and the JavaScript source code used in the third iteration of the user interface;

Here is the cascading style sheet. You will notice some of the new CSS 3 attributes like “border-radius”, which is used to form the rounded corners of the artist grid in the Entities From Service page.

The AJAX Client and Cross-Domain GET Requests

By Tim Daniels
In developing the client to consume the World of Art Web Service, I decided to stick with the ASP.Net Web Form (.aspx). This allowed me to take advantage of the master page concept already implemented in the World of Art Web App. Following is the markup source code of the .ASPX page. As you can see it has some very basic ingredients;

  • Page declaration with a reference to the web site master page
  • HTML Heading Tag
  • A script tag pointing to a JavaScript source code file called GetArtists.js

Here is the XHTML mark up code of the .aspx web form;

The heart of the client application is the JavaScript in the GetArtists.js file. There are two primary functions;

  • getArtistsAjax() – Requests artist data from the World Of Art Web Service
  • updateArtists() – Dynamically generates the user interface

Here is the JavaScript source code of the client.

Lets first examine the getArtistsAjax() function. There are many different ways to perform an HTTP GET request. After trying a number of different combinations, I found this implementation to be a straightforward and flexible solution. The getArtistsAjax() function uses the JQuery.ajax() method, to perform the asynchronous HTTP(Ajax) request for artist data from the ArtService. The jQuery.ajax() method can be configured with a multitude of settings and parameters. However, for the simple HTTP GET request required by this application, I found it necessary to only pass five arguments to the jQuery.ajax() method, which are coded as key/value pairs;

  • type: “GET” – The type of request to make (“POST” or “GET”)
  • url: ‘http://lexicon-software-apps.com/ArtService.svc/GetArtists’ – A string containing the URL to which the request is sent.
  • dataType: “jsonp” – Loads in a JSON block using JSONP. Adds an extra “?callback=?” to the end of your URL to specify the callback. Disables caching by appending a query string parameter, “_=[TIMESTAMP]“, to the URL unless the cache option is set to true.
  • success: function() – A function to be called if the request succeeds.
  • error: function() – A function to be called if the request fails.

The most interesting setting is the dataType set to “jsonP”. This gives the client flexibility to perform a “GET” request from a domain other than the one that served the original page request. The following illustrates an example of this. I setup an alternate domain at Winhost called Lexicon-Software-Learning.com. I published the World of Art Web Service at Lexicon-Software-Learning.com. I then changed the url setting to point to the learning domain. Here is the result;

Cross Domain Get Request

The artists entity web page is served from Lexicon-Software-Apps.com domain and the get artists data request is served from Lexicon-Software-Learning.com.

The other primary JavaScript function in GetArtists.js is the updateArtists() function. It receives the JSON formatted data result set from the HTTP GET request. The JSON data is basically an array of artist objects. This function simply iterates through the array of artist objects, and adds them as elements to the web pages’ Document Object Model (DOM) object. I also applied some new CSS 3 style sheet elements, to finish off the look and feel, of the list of artists page.

I will cover the user interface in more detail in my next post. We will also look at the evolution of the user interface in the World of Art Web App, from the first iteration which was server side generated, to this latest iteration which is client side generated.

Here is the CSS 3 Cascading Style Sheet

Deployment of the WCF Web Service

By Tim Daniels
Deployment of the World Of Art Web Service app was a very simple one step process.

From the Build Menu in Visual Studio 2010, select the Publish WorldOfArtServiceApp option. This will launch the Publish Web dialogue box.

As you can see from the illustration below, my hosting provider, WinHost, supports the one click publishing feature in Visual Studio 2010. I’m using the same server URL and domain name as the World of Art Web App. Notice the check box to indicate this is an IIS application on destination.

By structuring the WCF web service as a separate project I have the flexibility to publish to other domain names I have hosted at WinHost. The only disadvantage to this approach, is that anytime the World of Art Web App is re-published with new development, the World of Art Web Service also must be re-published. It appears that the World of Art Web Service looses the reference to the ArtWorldObjects in the World of Art Domain.

Publish WCF Web Service in Visual Studio 2010

Publish WCF Web Service in Visual Studio 2010 to hosting provider Winhost

My hosting provider, WinHost, fully supports IIS 7 hosting for my .NET web apps and WCF web services. In the illustration below, I’m using the IIS 7 Manager to remotely manage the IIS 7 server where my applications have been deployed.

Internet Information Services (IIS) 7 Manager is an administration UI that provides you with a new way to remotely manage IIS 7.x servers. I recommend reviewing the Getting Started with IIS Manager article by Tobin Titus on the IIS.net website.

IIS 7  Support at WinHost

IIS 7 Support at WinHost

As you can see, deployment and remote administration of the World Of Art Web Service, is a very straight forward process. The important thing to remember, is that I put some careful forethought into selecting a hosting provider, that fully supports the combination of technologies exploited in the architectural design of the applications;

  • ASP.net
  • MS SQL Server
  • WCF Web Services
  • IIS 7 Remote Management
  • One-Click Publishing

Visit WinHost for more information.

In my next post, we begin discussions about the World of Art Client, which is consuming the artist data from the World of Art Web Service.

Configuration of the WCF Web Service

Depending on the requirements of the application, configuration of a WCF web service can be simple and straightforward, or sophisticated and complex. This first iteration of the World of Art Web Service has very simple requirements, so I’ve attempted to keep the configuration as straightforward as possible. Here is the Web.Configuration file from the web service.

The first thing to notice is the ConnectionStrings tag. Since the World of Art Web Service references the Data Access Layer and the Data Model, from the World of Art Web App, it is necessary to provide the database connection information to the web service.

Now, lets discuss the configuration of the World of Art Web Service. Configuring the service in the Web.config file, gives us the same flexibility in deployment and management of the web service, as configuring a web application in the Web.config file. Instead of hard coding configuration elements in the application code, it is preferable to expose this information in the configuration file. This will allow an administrator to configure the application for final deployment, without having to modify code and rebuild the application.

The World of Art Web Service, is based on the Windows Communication Foundation (WCF) and will be hosted by Internet Information Services (IIS). The web service run time environment is .NET Framework version 4. Therefore, this configuration is specific to a WCF services hosted in IIS. IIS depends on the mark up found in the .svc file to implement the WCF service. My hosting provider, Winhost, supports all of these components.

The WCF configuration scheme includes three major sections;

  • serviceModel
  • bindings
  • services

Here is the serviceModel seciton ;

The entire service configuration is bound by the system.serviceModel elelment. Lets first examine the services element. Within services we find one or more service elements. It has a name attribute to identify the service. Within the service element, the endpoint element is used to configure one or more service endpoints. Notice how the address attribute has a null value. Since this service is hosted in IIS, it’s not necessary to specify an endpoint address. In fact, specifying a fully qualified endpoint address can lead to deployment errors in IIS. With an IIS hosted WCF service, the end point address is always relative to the .svc file that represents the service.

The binding attributes of the endpoint element, specify how the endpoint communicates with the world, in terms of transport and protocol schemes. In this case, I specified the WebHttpBinding Class. A binding used to configure endpoints for WCF Web services that are exposed through HTTP requests instead of SOAP messages. I am also utilizing the bindingConfiguration attribute of the endpoint element. This attribute is used in conjunction with binding to reference a specific binding configuration in the configuration file. In this case it is referencing a binding elelment in the webHttpBinding section named “webHttpBindingWithJsonP”, where I specify a custom binding configuration using the crossDomainScriptAccessEnabled, which is set to true. I will discuss cross domain access to web services in a future post.

The final attribute of the endpoint element is the behavior configuration attribute. I gave the behavior configuration a unique name of “webHttpBehavior”. The configuration of the endpoint itself can be found in the behavior element of endpoint behaviors section. The only required element I utilized is the webHttp element. This element specifies the WebHttpBehavior on an endpoint through configuration. This behavior, when used in conjunction with the webHttpBinding standard binding, enables the Web programming model for a Windows Communication Foundation (WCF) service. I simply took all of the default values for attributes in the webHttp element.

As you can see, it is absolutely necessary to completely understand the operational requirements of the WCF web service, prior to configuration. There is a lot of flexibility in this area of WCF. However, with more flexibility comes more responsibility. In my next post I will cover deployment of the WCF web service.

Data Contract and Data Member Attributes Complete the Service Contract

As I stated several times in recent posts, the purpose of the Art Service is not to expose the business logic of the World of Art web app. Instead, the purpose of the Art Service, is to simply expose specific data elements from the World of Art domain, which can be served upon request, to various software clients. We have already discussed in detail, how the service contract attribute and the operation contract attribute, transform a simple interface and class implementation, into an actual web service. Now lets look at how we can complete the details of the service contract, by specifying exactly which data elements are to be exposed by the web service.

In the early phases of development, I isolated the entities of the world of art domain into a separate class library called ArtWorldObjects. Artist is one of the entities defined in this class library. At this time, I want the WorldOfArtServiceApp to only expose the Artist object, and more specifically, only certain properties of the Artist object. To acomplish this, I will utilize the Data Contract Attribute Class and the Data Member Attribute Class. The data contract attribute specifies that the type defines or implements a data contract. The data member contract, when applied to the member of a type, specifies that the member is part of a data contract, and can be serialized by the DataContractSerializer Class.

Here is the source code for the IArtist interface and the Artist class, with data contract attribute and data member attribute applied;

Click on this link; Artists in JSON Format to send a “GET” request to the World of Art Web Service app. You can also copy the following link into your browser’s address bar to retrieve the JSON data;

Upon examining the JSON text returned by the service, you will see that the only properties of artist returned by the web service, are those enhanced with the Data Member attribute. Here is what the artist data in JSON format looks like;

For the sake of convenience I added three properties, of type string, to the IArtist Interface;

  • ExceptionMessage – messages to communicate any problems to the client
  • DateOfBirthShortString – date in “mm/dd/yyyy” format
  • DateOfBirthLongString – date in fomat “Monday, August 06, 1928″

The short and long data formats where necessary, because I discovered that when type System.DateTime is serialized to JSON, a very nasty date format is produced. Something like this; “DateOfBirth”:”\/Date(-296931600000-0700)\/”. Therefore, I excluded the date of birth property with a type of System.DateTime from being a Data Member. However, you can see in the source code, that the short and long date of birth properties are derived from the System.DateTime date of birth property.

In my next post, I will cover the configuration and deployment of the World Of Art Web Service. After that, I will cover the client, which uses JavaScript to consume artist data from the web service. This link; Artists from the Web Service will launch the client in the World of Art Web App.