This blog post is part of a series of posts that demonstrates building every aspect of a Windows 8 Store application, see the main index for more posts.
INTRODUCTION
Now that we have a database, we need a way to expose it to our new Windows Store Application. In this case, we will implement our middle tier using ASP.NET Web API. From the ASP.NET official website, Web API is defined as the following:
“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.”
In a nutshell what ASP.NET Web API gives us is a lightweight pipeline to marshal objects across the wire using HTTP verbs like the following:
CREATING THE WEB API PROJECT
Let’s go ahead now and crack open Visual Studio. First create an empty solution, I’ve named mine “UserGroupAttendance.sln”. Into this solution, add a new ASP.NET MVC 4 Web Application, and name it “UserGroupAttendanceServer”, select Web API as the project template, and click OK to continue.
GENERATING OUR MODEL
We are now ready to create our model. We are going to use Telerik’s free OpenAccess ORM. The selection of this tool will save us tons of coding time as you’ll see in a moment. Right-click the Models folder and select Add –> New Item. Select “Telerik OpenAccess Domain Model”, and name it “UGAttendanceModel.rlinq”. Populate the model from a database, and then select the database that we created in the data tier post.
Select all the tables to be included in our model, then click Finish. The model will then be generated for us.
We will now tweak a couple settings in the model to get the data loading behavior that we want from our model. First select the Members navigation property in the Meeting entity and set the following property value:
Property |
Value |
IsManaged | True |
Next select the Meetings property in the Member entity and set the following property values:
Property |
Value |
IsManaged | True |
Load Behavior | Eager |
Lastly, select the Picture field in Member, and set the following property value:
Property |
Value |
Load Behavior | Eager |
Finally, save the changes we made to our model. Be sure to build the project so that the model is available to use in the project.
GENERATING OUR ASP.NET WEB API SERVICES
Earlier I mentioned that I chose OpenAccess ORM for a reason, I will now show you why. OpenAccess ORM out of the box comes with extensions that allow you to generate all sorts of services, one of which is ASP.NET Web API. Right click on your model file (UGAttendanceModel.rlinq) and select “Add OpenAccess Service”.
For source and output, keep the defaults, which essentially tells OpenAccess to generate services based on the UGAttendanceModel and store/host the result in the UserGroupAttendanceServer project. Next select ASP.NET Web API Service, and click “Next”.
Ensure all Entities, as well as Insert/Update/Delete checkboxes are checked so their operations can be generated. You will now be shown a summary of the changes and additions to your project.
Click Finish to exit this dialog. Open MembersController.cs and add the following methods (you may also create another partial class file for this instead).
/// <summary>
/// api/members/?search=cru
/// </summary>
/// <param name="search"></param>
/// <returns></returns>
public virtual IQueryable<Member> GetMembersBySearch(string search)
{
IQueryable<Member> retvalue;
int id;
if (int.TryParse(search, out id))
{
//search member id
retvalue = repository.GetAll().Where(x => x.MemberId.Equals(id));
}
else
{
//search member name
retvalue = repository.GetAll().Where(x => x.Name.ToUpper().Contains(search.ToUpper()));
}
return retvalue;
}
/// <summary>
/// api/members/?meetingid=1
/// </summary>
/// <param name="meetingId"></param>
/// <returns></returns>
public virtual IQueryable<Member> GetMembersByMeetingId(Int64 meetingId)
{
return repository.GetAll().Where(x => x.Meetings.Count(m => m.MeetingId.Equals(meetingId)) == 1);
}
These methods add a couple different API methods. The first method allows us to search for a user group member using string search criteria. The second method allows us to retrieve all members that attended a specific meeting. You can see in the Summary comments the routes you will need to use in order to access those methods.
CLEANING UP
Let’s now spend a minute re-arranging our files in Solution Explorer. When we generated the Web API services, the source files were all stuck in the root folder of the project. To maintain a clean-looking solution, move all the *Controller.cs files to the Controllers folder, similarly, move the *Repository.cs files to the Models folder.
Make sure you save, and build the project.
CONCLUSION
In this post we saw how we can quickly and easily generate ASP.NET Web API services from our database using Telerik OpenAccess ORM. We also tweaked our model to achieve the data loading behavior that we want for our application. Lastly we added a couple custom Web API methods so that we can perform search as well as meeting attendance lookups. In the next post we will use these generated ASP.NET Web API services to seed the database from Twitter followers using a simple command-line program.
Comments