Getting Started with Microsoft MVC and Cloud Computing
The interesting part about the new MVC standard is the separation of data, front-end layout and business layers. The concept can be hard for long-term .NET developers to understand, but what follows is a short primer to the new MVC and to how it relates to cloud services.
Creating a New MVC Project
Open Visual Studio on the development desktop. After clicking the “File” and “New Project” option, Visual Studios shows the option to create o
Courtesy of d.boyd
When the new project template opens, notice the layout is different from old web forms. The developer has a list of categories in Solution Explorer — including controllers, models and views.
These three items are the separation in logic introduced in the new MVC framework. Models are the data, views are the front-end layout the user sees, and the controller contains code. For cloud hosting, the business can choose which layer to host in the cloud — which is typically the data layer. For example, the business can host the data in the cloud while running the views on the local web host.
Actions in MVC
MVC still contains the members and functions used in old .NET web forms, but the actions directive separates the types of activities users perform on the page. This is especially useful for businesses that have applications partially stored in the cloud. For instance, the following code contains the actions for users who navigate to the home “Index” page and those who navigate to the “Cloud” page:
public string Index()
{
return “You have navigated to the home page.”;
}
public string Cloud()
{
return “This text shows when the user hits the cloud application”;
}
In each of these functions, a user sees text dependent on the action taken. The advantage of this procedure is that the programmer can take the user to an internal application and redirect the user to a cloud application when using other types of actions. The two functions allow the business to operate both types of applications simultaneously without hosting two separate applications.
Adding Views to a Controller’s Action
Views are tied to the controller’s action, so the developer can create front-end layouts for local and cloud applications. Right-click the “Cloud” function text created earlier and select “Add View.” A view template is created for the app.
This file is where the developer includes all the images, CSS styles and general layout for the application. For cloud applications, the developer can host many of the scripts, images and code in the cloud. So most of the layout work is hosted securely on the cloud host servers, meaning that the layout plugins are always available — even if the internal application fails.
Working with MVC Data Hosted in the Cloud
The advantage of separating the layers of a web application is that the business can host data in the cloud, which is typically faster. Business that cannot afford the expense of fast data servers can host the data processes in the cloud — also affording the business protection from data loss. The models query the database server and return the data to the controller. The controller then returns the data to the front-end view.
Right-click the “Models” section of Solution Explorer and select “Add Model.” Choose a controller for the model. The controller used should be the one used to “talk” to the view. For example, use the “Cloud” controller for the “Cloud” page of the application.
The cloud database connection is set in the web.config. For instance, the following code connects to the database “mydb” in the MVC application:
<connectionStrings>
<add name=”shop” connectionString=”Data Source=<cloudIP>;Initial Catalog=mydb;Persist Security Info=True;User ID=user;Password=pass”
providerName=”System.Data.SqlClient” />
</connectionStrings>
The model created uses the database connection string to load and manipulate the cloud database. To add content to the database, MVC uses LINQ technology. Developers were also able to use LINQ in the .NET web forms framework, so it’s nothing new for developers.
For example, to view a list of customers, the following code can be added to the controller that connects to the sales database:
CustomersEntities db = new CustomerEntities();
public ActionResult Index()
{
var customers = from m in db.Customers
where m.SignupDate > new DateTime(2011, 6, 1)
select customers;
return View(customers.ToList());
}
In the code above, a list of customers is retrieved from the cloud database for instances in which the signupdate is greater than 6/1/2011. The returned data set is converted into a list and returned to the “Index” view.
Displaying the Data on the Web Page
The MVC framework makes it simpler to display data on a page. Instead of creating the database connection and cycling through a data set, the View retrieves the data sent from the controller and allows the programmer step through the data using the “foreach” loop. The following code finalizes the programmer to display the data to the user:
<% foreach (var item in Model)
{ %>
<tr>
<td><%: item.CustomerName %></td>
<td><%: item.CustomerAddress %></td>
</tr>
<% } %>
In this example, a list of customer names and addresses are displayed.
Unlike other languages that make the programmer combine all logic into one component, the new MVC framework lets cloud developers segment logic design and efficiently use cloud hosting — while keeping some data internally. The technology is especially useful for enterprise businesses with several applications running internally.
0 comments:
Post a Comment