---
title: "How can I extend an API controller to hold a variable for my database Context?"  
description: "How can I extend an API controller to hold a variable for my database Context?"  
author: "Anonymous User"  
published: 2015-01-26  
updated: 2015-01-27  
canonical: https://www.mindstick.com/forum/12899/how-can-i-extend-an-api-controller-to-hold-a-variable-for-my-database-context  
category: "asp.net"  
tags: ["c#", "asp.net mvc"]  
reading_time: 2 minutes  

---

# How can I extend an API controller to hold a variable for my database Context?

I have many classes like this one. They all have two things in common. They declare a [private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) called [Context](https://www.mindstick.com/forum/23245/what-is-context-in-android) and they have a [method](https://www.mindstick.com/forum/166/webservice-method) to dipose of that.

Is there a way I can create an intermediate [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) that I could inherit from that has the [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) to declare the variable and the dispose method? I assume it's easy to do but I am not sure how to declare the [variables](https://www.mindstick.com/articles/715/php-variables) and the method. Should they be private?

```
public class TypeController : ApiController{    private Context db = new Context();     [Route("GetMapData")]    public async Task<IHttpActionResult> GetMapData()    {        var result = await db.Types                             .Select(e => new                             {                                 Id = e.TypeId,                                 Name = e.Name                             })                             .ToListAsync();        return Ok(result);    }     protected override void Dispose(bool disposing)    {        if (disposing)        {            db.Dispose();        }        base.Dispose(disposing);    } }
```

## Replies

### Reply by Anonymous User

You can make a base class that does that, and you'd declare the db variable as protected

```
public class BaseController : ApiController{    protected Context db = new Context();     protected override void Dispose(bool disposing)    {        if (disposing)        {            db.Dispose();        }         base.Dispose(disposing);    }} public class TypeController : BaseController{       [Route("GetMapData")]    public async Task<IHttpActionResult> GetMapData()    {        var result = await db.Types                             .Select(e => new                             {                                 Id = e.TypeId,                                 Name = e.Name                             })                             .ToListAsync();        return Ok(result);    }  }
```


---

Original Source: https://www.mindstick.com/forum/12899/how-can-i-extend-an-api-controller-to-hold-a-variable-for-my-database-context

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
