Skip to main content

Posts tagged with 'or'

This is a repost that originally appeared on the Couchbase Blog: Dependency Injection with ASP.NET Core and Couchbase.

Dependency Injection is a design pattern that makes coding easier. It saves you the hassle of instantiating objects with complex dependencies, and it makes it easier for you to write tests. With the Couchbase.Extensions.DependencyInjection library (GitHub), you can use Couchbase clusters and buckets within the ASP.NET Core dependency injection framework.

In my last blog post on distributed caching with ASP.NET, I mentioned the DependencyInjection library. Dependency injection will be explored in-depth in this post. Feel free to follow along with the code samples I’ve created, available on GitHub.

Basic setup of Couchbase

First, you’ll need a Couchbase Server cluster running. You can:

Next, you’ll need to create a bucket in Couchbase. This can be the "travel-sample" bucket that comes with Couchbase, or a bucket that you create yourself.

If you are using Couchbase Server 5.0, you’ll also need to create a user. Give that user Cluster Admin permission, and give it the same name as the bucket, just to keep things simple if you are following along.

Dependency Injection with Couchbase.Extensions

The Couchbase.Extensions (GitHub) project aims to make working with Couchbase Server and ASP.NET Core simpler. Dependency Injection is just one of these extensions.

You can add it to your ASP.NET Core project with NuGet:

  • By using Package Manager: Install-Package Couchbase.Extensions.DependencyInjection -Version 1.0.2

  • With the NuGet UI

  • Use the .NET command line: dotnet add package Couchbase.Extensions.DependencyInjection --version 1.0.2

(Version 1.0.2 is the latest version at the time of writing).

Couchbase extension for dependency injection on NuGet

Next, you’ll need to make changes to your Startup class in Startup.cs.

In the blog post on caching, I hard-coded the configuration:

services.AddCouchbase(client =>
{
    client.Servers = new List<Uri> { new Uri("http://localhost:8091")};
    client.UseSsl = false;
});

This is fine for demos and blog posts, but you’ll likely want to use a configuration file for a production project.

services.AddCouchbase(Configuration.GetSection("Couchbase"));

Assuming you’re using the default appsettings.json, update that file to add a Couchbase section:

"Couchbase" : {
  "Servers": [
    "http://localhost:8091"
  ],
  "UseSsl": false
}

By making a "Couchbase" section, the dependency injection module will read right from the appsettings.json text file.

Constructor Injection

After dependency injection is setup, you can start injecting useful objects into your classes. You might inject them into Controllers, services, or repositories.

Here’s an example of injecting into HomeController:

public class HomeController : Controller
{
    private readonly IBucket _bucket;

    public HomeController(IBucketProvider bucketProvider)
    {
        _bucket = bucketProvider.GetBucket("travel-sample", "password");
    }

    // ... snip ...
}

Next, let’s do a simple Get operation on a well-known document in "travel-sample". This token usage of the Couchbase .NET SDK will show dependency injection in action. I’ll make a change to the generated About action method. In that method, it will retrieve a route document and write out the equipment number.

public IActionResult About()
{
    // get the route document for Columbus to Chicago (United)
    var route = _bucket.Get<dynamic>("route_56027").Value;

    // display the equipment number of the route
    ViewData["Message"] = "CMH to ORD - " + route.equipment;

    return View();
}

And the result is:

Travel sample output of a single route

Success! Dependency injection worked, and we’re ready to use a Couchbase bucket.

If you aren’t using "travel-sample", use a key from your own bucket.

Named buckets

You can use dependency injection for a single bucket instead of having to specify the name each time.

Start by creating an interface that implements INamedBucketProvider. Leave it empty. Here’s an example:

public interface ITravelSampleBucketProvider : INamedBucketProvider
{
    // nothing goes in here!
}

Then, back in Startup.cs, map this interface to a bucket using AddCouchbaseBucket:

services
    .AddCouchbase(Configuration.GetSection("Couchbase"))
    .AddCouchbaseBucket<ITravelSampleBucketProvider>("travel-sample", "password");

Now, the ITravelSampleBucketProvider gets injected instead of the more general provider.

public HomeController(ITravelSampleBucketProvider travelBucketProvider)
{
    _bucket = travelBucketProvider.GetBucket();
}

More complex dependency injection

Until this point, we’ve only used dependency injection on Controllers. Dependency injection starts to pay dividends with more complex, deeper object graphs.

As an example, imagine a service class that uses a Couchbase bucket, but also uses an email service.

public class ComplexService : IComplexService
{
    private readonly IBucket _bucket;
    private readonly IEmailService _email;

    public ComplexService(ITravelSampleBucketProvider bucketProvider, IEmailService emailService)
    {
        _bucket = bucketProvider.GetBucket();
        _email = emailService;
    }

    public void ApproveApplication(string emailAddress)
    {
        _bucket.Upsert(emailAddress, new {emailAddress, approved = true});
        _email.SendEmail(emailAddress, "Approved", "Your application has been approved!");
    }
}

Next, let’s use this service in a controller (aka making it a dependency). But notice that the controller is not directly using either the bucket or the email service.

public class ApproveController : Controller
{
    private readonly IComplexService _svc;

    public ApproveController(IComplexService svc)
    {
        _svc = svc;
    }

    public IActionResult Index()
    {
        var fakeEmailAddress = Faker.Internet.Email();
        _svc.ApproveApplication(fakeEmailAddress);
        ViewData["Message"] = "Approved '" + fakeEmailAddress + "'";
        return View();
    }
}

If I were to instantiate ComplexService manually, I would have to instantiate at least two other objects. It would look something like: new ComplexService(new BucketProvider(), new MyEmailService(). That’s a lot that I have to keep track of, and if any dependencies change, it’s a lot of manual maintenance.

Instead, I can have ASP.NET Core use dependency injection to do all this for me. Back in Startup:

services.AddTransient<IEmailService, MyEmailService>();
services.AddTransient<IComplexService, ComplexService>();

Now, ASP.NET Core knows how to instantiate:

  • ITravelSampleBucketProvider, thanks to Couchbase.Extensions.DependencyInjection

  • IEmailService - I told it to use MyEmailService

  • IComplexService - I told it to use ComplexService

Finally, when ApproveController is instantiated, ASP.NET Core will know how to do it. It will create ComplexService by instantiating MyEmailService and ComplexService. It will inject ComplexService automatically into `ApproveController’s constructor. The end result:

Complex service in action using dependency injection

For the complete example, be sure to check out the source code that accompanies this blog post on GitHub.

Cleaning up

Don’t forget to clean up after yourself. When the ASP.NET Core application is stops, release any resources that the Couchbase .NET SDK is using. In the Configure method in Startup, add a parameter of type IApplicationLifetime:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime applicationLifetime)

Within that Configure method, setup an ApplicationStopped event:

applicationLifetime.ApplicationStopped.Register(() =>
{
    app.ApplicationServices.GetRequiredService<ICouchbaseLifetimeService>().Close();
});

Summary

Dependency injection is a rich subject. Entire books have been written about it and its benefits to your application. This blog post just scratched the surface and didn’t even cover the testability benefits.

Couchbase.Extensions.DependencyInjection makes it easier to inject Couchbase into ASP.NET Core.

If you have questions or comments, make sure to check out the GitHub repository or the Couchbase .NET SDK forums.

And please reach out to me with questions by leaving a comment below or finding me on Twitter @mgroves.

This is a repost that originally appeared on the Couchbase Blog: Distributed caching with ASP.NET Core and Couchbase.

Distributed caching can help to improve performance of an ASP.NET Core application. This is especially true for an ASP.NET application that’s deployed to a server farm or scalable cloud environment. Using Couchbase Server for caching is one of the many features that make it an ideal choice for your engagement database needs.

In this blog post, I’ll show you how to use the Couchbase.Extensions.Caching middleware plugin to easily add distributed caching capabilities to your application.

You can follow along with the sample code I wrote for this post on GitHub.

Please note that Couchbase.Extensions.Caching is currently in a beta2 release (as I’m writing this blog post), so some things may change.

Basic setup of Couchbase

First, you’ll need a Couchbase Server cluster running (you can install it on-premise, or with Docker, or even in Azure if you’d like).

Next, you’ll need to create a bucket in Couchbase where cached data will be stored. I called mine "cachebucket". You may want to take advantage of the new ephemeral bucket feature in Couchbase Server 5.0 for caching, but it is not required.

If you are using Couchbase Server 5.0, you’ll also need to create a user with permissions (Data Writer and Data Reader) on that bucket. To keep things simple, create a user that has the same name as the bucket (e.g. "cachebucket").

Distributed Caching with Couchbase.Extensions

The Couchbase.Extensions (GitHub) project aims to make working with Couchbase Server and .NET Core simpler. Caching is just one of these extensions.

You can add it to your ASP.NET Core project with NuGet, via Package Manager: Install-Package Couchbase.Extensions.Caching -Version 1.0.0-beta2, or with the NuGet UI, or you can use the .NET command line: dotnet add package Couchbase.Extensions.Caching --version 1.0.0-beta2.

Couchbase extension for distributed caching available on NuGet

Once you’ve added this to your project, you’ll need to make a couple minor changes to your Startup class in Startup.cs.

First, in ConfigureServices, add a couple namespaces:

using Couchbase.Extensions.Caching;
using Couchbase.Extensions.DependencyInjection;

This will make the Caching namespace available, and specifically the AddDistributedCouchbaseCache extension method for IServiceCollection. Next, call that extension method from within the ConfigureServices method.

The other namespace in there, DependencyInjection, is necessary to inject Couchbase functionality. In this case, it’s going to be used only by the Caching extension. But you can use it for other purposes too, which I will cover in a future blog post.

But for now, it’s just needed for the AddCouchbase extension method on IServiceCollection.

Finally, put them both together, and your ConfigureServices method should look like this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddCouchbase(opt =>
    {
        opt.Servers = new List<Uri>
        {
            new Uri("http://localhost:8091")
        };
    });

    services.AddDistributedCouchbaseCache("cachebucket", "password", opt => { });
}

Using distributed caching

Now that you have distributed caching setup with Couchbase in your ASP.NET Core project, you can use IDistributedCache elsewhere in your project.

Injecting IDistributedCache

A simple example would be to use it directly in a controller. It can be injected into constructors as you need it:

public class HomeController : Controller
{
    private readonly IDistributedCache _cache;

    public HomeController(IDistributedCache cache)
    {
        _cache = cache;
    }

    // ... snip ...

}

Caching strings

You can use the GetString and SetString methods to retrieve/set a string value in the cache.

// cache/retrieve from cache
// a string, stored under key "CachedString1"
var message = _cache.GetString("CachedString1");
if (message == null)
{
    message = DateTime.Now + " " + Path.GetRandomFileName();
    _cache.SetString("CachedString1", message);
}
ViewData["Message"] = "'CachedString1' is '" + message + "'";

This would appear in the "cachebucket" bucket as an encoded binary value (not JSON).

String cached in Couchbase

In the sample code, I simply print out the ViewData["Message"] in the Razor view. It should look something like this:

Cached string output to Razor

Caching objects

You can also use Set<> and Get<> methods to save and retrieve objects in the cache. I created a very simple POCO (Plain Old CLR Object) to demonstrate:

public class MyPoco
{
    public string Name { get; set; }
    public int ShoeSize { get; set; }
    public decimal Price { get; set; }
}

Next, in the sample, I generate a random string to use as a cache key, and a random generated instance of MyPoco. First, I store them in the cache using the Set<> method:

var pocoKey = Path.GetRandomFileName();
_cache.Set(pocoKey, MyPoco.Generate(), null);
ViewData["Message2"] = "Cached a POCO in '" + pocoKey + "'";

Then, I print out the key to the Razor view:

Cached POCO output to Razor

Next, I can use this key to look up the value in Couchbase:

Cached POCO in Couchbase

Also, notice that it’s been serialized to JSON. This not only means that you can read it, but you can also query it with N1QL (if you need to).

Distributed caching with expiration

If you want the values in the cache to expire after a certain period of time, you can specify that with DistributedCacheEntryOptions (only SlidingExpiration is supported at this time).

var anotherPocoKey = Path.GetRandomFileName();
_cache.Set(anotherPocoKey, MyPoco.Generate(), new DistributedCacheEntryOptions
{
    SlidingExpiration = new TimeSpan(0, 0, 10) // 10 seconds
});
ViewData["Message3"] = "Cached a POCO in '" + anotherPocoKey + "'";

In the sample project, I’ve also set this to print out to Razor.

Cached POCO with expiration

If you view that document (before the 10 seconds runs out) in Couchbase Console, you’ll see that it has an expiration value in its metadata. Here’s an example:

{
  "id": "xjkmswko.v35",
  "rev": "1-14e1d9998125000059b0404502000001",
  "expiration": 1504723013,
  "flags": 33554433
}

After 10 seconds, that document will be gone from the bucket, and you’ll see an error "not found (Document does not exist)".

Tearing down distributed caching

Finally, don’t forget to cleanup the resources used by the Couchbase .NET SDK for distributed caching. One easy way to do this is with the ApplicationStopped event. You can wire this up in Startup:

appLifetime.ApplicationStopped.Register(() =>
{
    app.ApplicationServices.GetRequiredService<ICouchbaseLifetimeService>().Close();
});

Note that you will have to add IApplicationLifetime appLifetime as a parameter to the Configure method in Startup if you haven’t already.

Summary

Using Couchbase Server for distributed caching in your ASP.NET Core application is a great way to improve performance and scalability in your application. These kind of "engagement" use cases are what Couchbase Server excels at. To see customers that are using Couchbase Server for caching, check out the Couchbase Customers page.

If you have questions or comments about the Couchbase.Extensions.Caching project, make sure to check out the GitHub repository or the Couchbase .NET SDK forums.

As always, you can reach me by leaving a comment below or finding me on Twitter @mgroves.

Joe Ferguson is using Sitecore.

Show Notes:

Joe Ferguson is on Twitter

Want to be on the next episode? You can! All you need is the willingness to talk about something technical.

Theme music is "Crosscutting Concerns" by The Dirty Truckers, check out their music on Amazon or iTunes.

This is a repost that originally appeared on the Couchbase Blog: Tooling Improvements in Couchbase 5.0 Beta.

Tooling improvements have come to Couchbase Server 5.0 Beta. In this blog post, I’m going to show you some of the tooling improvements in:

  • Query plan visualization - to better understand how a query is going to execute

  • Query monitoring - to see how a query is actually executing

  • Improved UX - highlighting the new Couchbase Web Console

  • Import/export - the new cbimport and cbexport tooling

Some of these topics have been covered in earlier blog posts for the developer builds (but not the Beta). For your reference:

Query Plan Visualization tooling

In order to help you write efficient queries, the tooling in Couchbase Server 5.0 has been enhanced to give you a Visual Query Plan when writing N1QL queries. If you’ve ever used the Execution Plan feature in SQL Server Management Studio, this should feel familiar to you.

As a quick example, I’ll write a UNION query against Couchbase’s travel-sample bucket (optional sample data that ships with Couchbase Server). First, I’ll click "Query" to bring up the Couchbase Query Workbench. Then, I’ll enter a query into the Query Editor.

Query Editor tooling

This is a relatively complex query that involves the following steps (and more):

  1. Identify and scan the correct index(es)

  2. Fetch the corresponding data

  3. Project the fields named in the SELECT clause

  4. Find distinct results

  5. UNION the results together

  6. Stream the results back to the web console

In Couchbase Server 4.x, you could use the EXPLAIN N1QL command to get an idea of the query plan. Now, in Couchbase Server 5.0 beta, you can view the plan visually.

Query Plan Visualization tooling

This tooling shows you, at a glance, the costliest parts of the query, which can help you to identify improvements.

Query monitoring

It’s important to have tooling to monitor your queries in action. Couchbase Server 5.0 beta has tooling to monitor active, completed, and prepared queries. In addition, you have the ability to cancel queries that are in progress.

Start by clicking "Query" on the Web Console menu, and then click "Query Monitor". You’ll see the "Active", "Completed", and "Prepared" options at the top of the page.

Let’s look at the "Completed" queries page. The query text and other information about the query is displayed in a table.

Query Monitor of completed queries

Next, you can sort the table to see which query took the longest to run (duration), return the most results (result count), and so on. Finally, if you click "edit", you’ll be taken to the Query Workbench with the text of that query.

New Couchbase Web Console

If you’ve been following along, you’ve probably already noticed the new Couchbase Web Console. The UI has been given an overhaul in Couchbase Server 5.0. The goal is to improve navigation and optimize the UI.

New Couchbase Web Console

This new design maximizes usability of existing features from Server 4.x, while leaving room to expand the feature set of 5.0 and beyond.

cbimport and cbexport

New command line tooling includes cbimport and cbexport for moving data around.

cbimport supports importing both CSV and JSON data. The documentation on cbimport should tell you all you want to know, but I want to highlight a couple things:

  • Load data from a URI by using the -d,--dataset <uri> flags

  • Generate keys according to a template by using the -g,--generate-key <key_expr> flags. This gives you a powerful templating system to generate unique keys that fit your data model and access patterns

  • Specify a variety of JSON formats when importing: JSON per line (lines), JSON list/array (list), JSON ZIP file/folder containing multiple files (sample). So no matter what format you receive JSON in, cbimport can handle it.

For more about cbimport in action, check out Using cbimport to import Wikibase data to JSON documents.

cbexport exports data from Couchbase to file(s). Currently, only the JSON format is supported. Again, the documentation on cbexport will tell you what you want to know. A couple things to point out:

  • Include the document key in your export by using the --include-key <key> flag.

  • Export to either "lines" or "list" format (see above).

Here’s an example of cbexport in action (I’m using Powershell on Windows, but it will be very similar on Mac/Linux):

PS C:\Program Files\Couchbase\Server\bin> .\cbexport.exe json -c localhost -u Administrator -p password -b mybucketname -f list -o c:\exportdirectory\cbexporttest.json --include-key _id

Json exported to `c:\exportdirectory\cbexporttest.json` successfully

PS C:\Program Files\Couchbase\Server\bin> type C:\exportdirectory\cbexporttest.json

[
{"_id":"463f8111-2000-48cc-bb69-e2ba07defa37","body":"Eveniet sed unde officiis dignissimos.","type":"Update"},
{"_id":"e39375ab-2cdf-4dc4-9659-6c19b39e377d","name":"Jack Johnston","type":"User"}
]

Notice that the key was included in an "_id" field.

Summary

Tooling for Couchbase Server 5.0 beta is designed to make your life easier. These tools will help you whether you’re writing queries, integrating with data, monitoring, or performing administrative tasks.

We’re always looking for feedback. Inside of the Web Console, there is a feedback icon at the bottom right of the screen. You can click that to send us feedback about the tooling directly. Or, feel free to leave a comment below, or reach out to me on Twitter @mgroves.

This is a special crossover episode of Cross Cutting Concerns with the Eat Sleep Code podcast, hosted by Ed Charbeneau (Microsoft MVP). This was recorded at the Stir Trek conference.

Show Notes:

Ed Charbeneau is on Twitter

Want to be on the next episode? You can! All you need is the willingness to talk about something technical.

Theme music is "Crosscutting Concerns" by The Dirty Truckers, check out their music on Amazon or iTunes.

Matthew D. Groves

About the Author

Matthew D. Groves lives in Central Ohio. He works remotely, loves to code, and is a Microsoft MVP.

Latest Comments

Twitter