Open Visual Studio 2010 and create a new project. Select ASP.NET MVC 3 Web Application from the Web templates as shown in Figure 1.
Next you need to select the Empty template, and select the Razor view engine.
You will need to create a few files. Right click the Controllers
folder in Solution Explorer and choose Add | Controller. Name this
controller HomeController and choose the Empty
controller template. See Figure 3.
Next, right click on the Views folder, and create a folder named Home and then right click on that folder and choose Add | View. Name the view Index and use the Razor template. You can leave everything else on the dialog as is, and click the Add button.
After you've completed this, you will be left with Solution Explorer looking like Figure 4.
Open the HomeController.cs file and make its contents look like Listing 1 now.
Listing 1: HomeController.cs
using System.Web.Mvc; namespace MembaseTutorial.Controllers { public class HomeController : Controller { /// <summary> /// GET: /Home /// </summary> /// <returns></returns> public ActionResult Index() { ViewData["Message"] = "I don't know you."; return View(); } } }
Then edit the Index.cshtml view file and set the contents to be that of Listing 2.
Listing 2: Index.cshtml
@using MembaseTutorial.Models; @{ ViewBag.Title = "Home"; } <h2>Home</h2> <p>@ViewData["Message"]</p>
In the next Stage, you will be downloading and configuring the
Enyim Memcache and Membase client library. We will refer to this
as the Enyim client library. The client library allows any .NET
object to be saved into Membase as long as that object is
annotated with Serializable. So, we'll make a
simple data model object for storing information in our database.
See Listing 3. Right click on the Models folder and add a new
class.
Listing 3: User.cs
using System; namespace MembaseTutorial.Models { [Serializable] public class User { public Guid UserId { get; set; } public String UserName { get; set; } public String Password { get; set; } public String FullName { get; set; } } }
There's nothing fancy about this, just a few auto properties to
hold the data in, and the very important
Serializable attribute. Without this attribute,
Enyim would not be able to store the object into Membase. With it,
Enyim will serialize your model object directly into the database,
and back again without any extra work on your part - very
convenient. Do keep in mind that if you change the structure of
your objects, you will have to provide some sort of custom
serialization to accomplish the task. You could also create
multiple hierarchically named keys and store all of these fields
separately in Membase. For the sake of keeping this tutorial
simple we'll just use serialized objects.
Speaking of Enyim, now would be a good time to install it, add some references, and get it ready to start writing data into your database.