![]() |
Web Development »
ASP.NET »
Data
Intermediate
License: The Code Project Open License (CPOL)
N-Tier development with ASP.NET MVC, WCF & LINQBy Emmanuel NuyttensThis article is presenting a demo bugtracking application build on ASP.NET MVC, WCF & LINQ to SQL |
C#, .NET (.NET 3.5), ASP.NET, WCF, Architect, Dev
|
||||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
This article is about a possible way to implement a N-Tier architecture for ASP.NET MVC (Model View Control), using WCF (Windows Communication Foundation = replacement for .NET Remoting) and LINQ to SQL. This article will not go in depth explaining the ins and outs of MVC, WCF & LINQ (there's already enough stuff which explains these technologies ...), instead, i'll be showing a possible way to implement these features in an N-Tier environment, this by means of a simple bugtracking demo application.
First we should state the requirments of our demo application. These are quit straight forward, as mentioned beneath:
![]()
The .NET Solution consists of 3 main projects:
Another project, not mentioned on the diagram above is CommonLib, which contains commonly used routines (like a business base class which each model class derives from, method for cloning objects & a business rule validator engine).
![]()
These are the LINQ-TO-SQL classes retrieved from the SQL-SERVER Bugtracker database (database template included in zip file). As this model should be remotable, the serialization mode on the datacontext should be set to Unidirectional.
We want to be able to load a userlist, so we can select a user for whom the ticket applies to:
public static ListGetUserList() { // Create the DataContext to retrieve the Users from. BugTrackerDataContext db = new BugTrackerDataContext(); // Return a list of Users. return db.Users.ToList (); }
We want to be able to load tickettype list, so we can select the type of ticket (BUG, INFO, etc ...) for the ticket:
public static ListGetTicketTypeList() { // Create the DataContext to retrieve the Ticket Types from. BugTrackerDataContext db = new BugTrackerDataContext(); // Return a list of TicketTypes. return db.TicketTypes.ToList (); }
We want to be able to retrieve all the current tickets from our ticket table:
public static IEnumerableGetTickets() { // Create the DataContainer to retrieve the tickets. BugTrackerDataContext db = new BugTrackerDataContext(); // Return a List of Tickets return db.Tickets; }
Finaly, we want to have the possibility to persist a newly added ticket (one at a time for the demo application, but we make the method all-purpose, so it can accept many added or modified objects):
public static bool PersistTickets(ref TicketList p_tickets) { // Only persist if any tickets to add or modify. if (p_tickets == null || p_tickets.Count == 0) return false; // Create the persistence datacontext BugTrackerDataContext db = new BugTrackerDataContext(); // Persist any new or modified tickets. foreach (Ticket t in p_tickets) { if (t.TicketId == 0) { db.Tickets.InsertOnSubmit(t); } else { db.Tickets.Attach(t, t.IsDirty); } } try { db.SubmitChanges(ConflictMode.ContinueOnConflict); // Reset the isDirty flag. foreach (Ticket t in p_tickets) { t.IsDirty = false; } } catch (ChangeConflictException ex) { throw ex; } return true; }
The purpose of our WCF enabled service layer is the make the model & data-access routines (to forsee the model of data ...) available to the presentation layer).
Our service layer consists mainly of 2 routines:
[ServiceContract]
public interface IBtService
{
[OperationContract()]
IEnumerable GetUserList();
[OperationContract()]
IEnumerable GetTicketTypeList();
[OperationContract()]
bool PersistTickets(ref TicketList p_tickets);
[OperationContract()]
IEnumerable GetTickets();
}
public IEnumerableGetUserList() { return BtDataManager.GetUserList(); } public IEnumerable GetTicketTypeList() { return BtDataManager.GetTicketTypeList(); } public bool PersistTickets(ref TicketList p_tickets) { return BtDataManager.PersistTickets(ref p_tickets); } public IEnumerable GetTickets() { return BtDataManager.GetTickets(); }
The presentation layer contains our ASP MVC application. As already mentioned above, I willnot go in depth on MVC, as there is already a lot of info available on the net. But important to retain is that MVC contains 3 important items, namely the Model (which represents our class a validation logic, and is remotly available through the Service Layer), the View (our webpage) and the Controller (which basically "hooks" the Model to the View).
![]()
With this page, the user can add a bugticket to the system. The HomeController is responsible for rendering the page. While interacting with the page, there are 2 actions envolved, one for creating a new ticket (the "GET") version, and one for persisting a new ticket (when the user hits the submit button, (the "POST) version).
[AcceptVerbs("GET")] public ActionResult Ticket() { this.LoadSelectionLists(); // Get the userlist var userList = new SelectList(_userList, "UserId", "Username"); ViewData["UserId"] = userList; // Get the tickettypelist var ticketTypeList = new SelectList (_ticketTypeList, "TicketTypeId", "TicketTypeDescription"); ViewData["TicketTypeId"] = ticketTypeList; return View(); }
When the user selects the "Create Ticket" in the index page, then we must create a new page where the user may enter the ticket details. As the user should have the possibility to select a user and tickettype for the ticket, these data are loaded from the service and added to the viewstate of the form:
private void LoadSelectionLists() { // Get a proxy to the data service provider BtServiceRef.BtServiceClient proxy = new BugTracking.BtServiceRef.BtServiceClient(); // Get the userlist _userList = proxy.GetUserList(); // Get the tickettypelist _ticketTypeList = proxy.GetTicketTypeList(); }
On the other hand, the post version is executed when the user hits the "submit" button. We will create a ticket object, map the properties to the field properties of the form, validate the objects data (see source code of the model for detailed validation information, as validation data is stored in the model classes) and persist the newlay added ticket through the service to the database.
[AcceptVerbs("POST")]
public ActionResult Ticket(FormCollection p_form)
{
// Create placeholder for the ticket to add.
var TicketToCreate = new Ticket();
try
{
// Map the controls of the View to the PlaceHolder.
this.UpdateModel(TicketToCreate, new[]
{ "UserId", "TicketTypeId", "Comment", "GrantedToDate" });
// Force Validation
TicketToCreate.Validate();
// First check if any validation Errors Exist on the newly
added ticket
if (TicketToCreate.HasErrors)
{
// View Validation Errors
foreach(DictionaryEntry entry in
TicketToCreate.ValidationErrors)
{
ViewData.ModelState.AddModelError(entry.Key.ToString
(), entry.Value.ToString());
}
throw new InvalidOperationException();
}
else
{
// Create the WCF Remote service and let the service
persist the new ticket.
BtServiceRef.BtServiceClient proxy = new
BugTracking.BtServiceRef.BtServiceClient();
Ticket[] newTicketList = new Ticket[] { TicketToCreate };
proxy.PersistTickets(ref newTicketList);
}
}
catch
{
this.LoadSelectionLists();
var userList = new SelectList(_userList, "UserId", "Username");
ViewData["UserId"] = userList;
var ticketTypeList = new SelectList
(_ticketTypeList, "TicketTypeId", "TicketTypeDescription");
ViewData["TicketTypeId"] = ticketTypeList;
// Show possible validation errors.
return View(TicketToCreate);
}
// Return to main page.
return RedirectToAction("Index");
}
There are 2 comboboxes on the ticket.aspx form, one for user selection and one for tickettype selection. As shown in the code above, we load the user and tickettype records from the service and add them as ViewData of the ticket page. Next we have to bind the ViewData as mentioned beneath: (you can also notice how we execute the bindings for validation purpose)
![]()
When loading the application in the browser, the index page is the first page showed. We get a list of current tickets in the database and the possibility to add a new ticket to the system:
![]()
Again, the HomeController is responsible for rendering the page, the action envolved is:
public ActionResult Index()
{
ViewData["Title"] = "Home Page";
ViewData["Message"] = "Welcome to ASP.NET MVC Bugtracker !";
// Create a proxy to the WCF service.
BtServiceRef.BtServiceClient proxy = new BugTracking.BtServiceRef.BtServiceClient();
// Get all tickets from the DataLayer.
return View(proxy.GetTickets());
}
As we will show the current tickets in this page, we first create a reference to our service. This service calls the GetTickets() method of our datalayer data-access class and adds it as input parameter of the view. Next to be sure that our Index Page is aware of the loaded ticket-list, the BtDataLayer.Ticket type should be added as attribute to the derieved ViewPage :
![]()
Finally, in the html code of our index.aspx page, we can loop through our loaded ticketlist and place each ticket in a datarow:
![]()
You may have noticed that I display the userId and TickeTypeId instead of username and tickettype description. If you want to view for example the username instead of the id, you should code : t.User.UserName, but this will return an error, as the user is not loaded with the tickets. You can change this by altering the data access method and add a dataloadoptions for the user and tickettype. In this case the related user and tickettype objects will be loaded too.
Voilà, that's all to mention. This article showed a possible way to separate logical layers and pass data between bounderies using new technology such as WCF and LINQ-TO-SQL. Finally it showed a possible presentation layer, using the newest ASP.NET MVC technology. Special thanx goes to the people running the asp.net website www.asp.net and also special thanx goes to Beth Massi http://blogs.msdn.com/bethmassi/, for explaining LINQ-TO-SQL and N-Tier through WCF in a well structured and clear view.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 5 Jan 2009 Editor: |
Copyright 2009 by Emmanuel Nuyttens Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |