After creating the Brewery class, the next step is to create the
data access classes that will encapsulate our Couchbase CRUD and
View operations. Create a new file in “Models” named
“RepositoryBase`1.cs” with a class name of
“RepositoryBase.” This will be an abstract class, generically
constrained to work with ModelBase instances.
Note the “`1” suffix on the file name is a convention used for
generic classes in C# projects.
public abstract class RepositoryBase<T> where T : ModelBase { //CRUD methods }
The process of creating an instance of a
CouchbaseClient is expensive. There is a fair
amount of overhead as the client establishes connections to the
cluster. It is therefore recommended to minimize the number of
times that a client instance is created in your application. The
simplest approach is to create a static property or singleton that
may be accessed from data access code. Using the
RepositoryBase, setting up a protected static
property will provide access for subclasses.
public abstract class RepositoryBase<T> where T : ModelBase { protected static CouchbaseClient _Client { get; set; } static RepositoryBase() { _Client = new CouchbaseClient(); } }
The code above requires setting configuration in web.config. It is of course equally valid to define the configuration in code if that is your preference. See getting started for more details.
<configSections> <section name="couchbase" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/> </configSections> <couchbase> <servers bucket="beer-sample"> <add uri="http://127.0.0.1:8091/pools"/> </servers> </couchbase>