Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library: .NET (C#) 1.2
Community Wiki and Resources
Wiki: .NET Client Library
Download Client Library
.NET Client Library
Couchbase Developer Guide 2.0
Couchbase Server Manual 2.0
SDK Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
2 Couchbase and ASP.NET MVC
Chapter Sections
Chapters

2.3. Working with Models

The first task to solve is displaying a list of breweries in from our beer-sample bucket. To add this functionality, there is some plumbing to setup in our application. These tasks are enumerated below.

As a JSON document database, Couchbase supports a natural mapping of domain objects to data items. In other words, there's very little difference between the representation of your data as a class in C# and the representation of your data as a document in Couchbase. Your object becomes the schema defined in the JSON document.

When working with domain objects that will map to documents in Couchbase, it's useful, but not required, to define a base class from which your model classes will derive. This base class will be abstract and contain two properties, “Id” and “Type.”

Right click on the “Models” directory and add a new class named “ModelBase” and include the following code.

public abstract class ModelBase
{
    public virtual string Id { get; set; }
    public abstract string Type { get; }
}

Note that the Type method is abstract and readonly. It will be implemented by subclasses simply by returning a hard-coded string, typically matching the class name, lower-cased. The purpose of the Type property is to provide taxonomy to the JSON documents stored in your Couchbase bucket. The utility will be more obvious when creating views.

Next, create a new class named in the “Models” directory of your project. This class will be a plain old CLR object (POCO) that simply has properties mapping to the properties of brewery documents in the beer-sample bucket. It will also extend ModelBase.

public class Brewery : ModelBase
{
    public string Name { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Code { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Website { get; set; }
    public DateTime Updated { get; set; }
    public string Description { get; set; }
    public IList<string> Addresses { get; set; }
    public IDictionary<string, object> Geo { get; set; }
    public override string Type
   {
        get { return "brewery"; }
   }
}
{
  "name": "Thomas Hooker Brewing",
  "city": "Bloomfield",
  "state": "Connecticut",
  "code": "6002",
  "country": "United States",
  "phone": "860-242-3111",
  "website": "http://www.hookerbeer.com/",
  "type": "brewery",
  "updated": "2010-07-22 20:00:20",
  "description": "Tastings every Saturday from 12-6pm, and 1st and 3rd Friday of every month from 5-8.",
  "address": [
    "16 Tobey Road"
  ],
  "geo": {
    "accuracy": "RANGE_INTERPOLATED",
    "lat": 41.8087,
    "lng": -72.7108
  }
}