> For the complete documentation index, see [llms.txt](https://coldbox-orm.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coldbox-orm.ortusbooks.com/v5.x/virtual-services/concrete-virtual-services.md).

# Concrete Virtual Services

Concrete Virtual Services - Building custom services that inherit from Virtual Entity Services

![](https://raw.githubusercontent.com/wiki/coldbox-modules/cbox-cborm/ConcreteORMServices.jpg)

Let's say you are using the virtual service but you find that they do not complete your requirements, or you need some custom methods or change functionality. Then you will be building concrete services that inherit from the virtual entity service. This is the very purpose of these support classes as most of the time you will have custom requirements and your own style of coding. You will do this in two steps:

1. Inherit from `cborm.models.VirtualEntityService`
2. Call the `super.init()` constructor with the entity to root the service and any other options

Below is a sample service layer:

```javascript
class extends="cborm.models.VirtualEntityService" singleton {

    // DI
    property name="settingService"  inject="id:settingService@cb";
    property name="CBHelper"        inject="id:CBHelper@cb";
    property name="log"             inject="logbox:logger:{this}";

    /**
    * Constructor
    */
    CommentService function init(){
        super.init( entityName="cbComment", useQueryCaching="true");
        return this;
    }

    /**
    * Get the total number of approved comments in the system
    */
    numeric function getApprovedCommentCount(){
        return countWhere( isApproved = true );
    }

    /**
    * Get the total number of unapproved comments in the system
    */
    numeric function getUnApprovedCommentCount(){
        return countWhere( isApproved = false );
    }

    /**
    * Comment listing for UI of approved comments, returns struct of results=[comments,count]
    * @contentID The content ID to filter on
    * @max The maximum number of records to return, 0 means all
    * @offset The offset in the paging, 0 means 0
    * @sortOrder Sort the comments asc or desc, by default it is desc
    */
    function findApprovedComments(
        contentID,
        max=0,
        offset=0,
        sortOrder="desc"
    ){
        var results = {};
        var c = newCriteria();

        // only approved comments
        c.isTrue("isApproved");

        // By Content? Dotted paths join the association for you, and no casting is needed
        if( structKeyExists( arguments,"contentID" ) AND len( arguments.contentID ) ){
            c.isEq( "relatedContent.contentID", arguments.contentID );
        }

        // run criteria query and projections count
        results.count    = c.count();
        results.comments = c.list(
            offset    = arguments.offset,
            max       = arguments.max,
            sortOrder = "createdDate #arguments.sortOrder#",
            asQuery   = false
        );

        return results;
    }
}
```

{% hint style="info" %}
`newCriteria()` returns a `bx-orm` `entityCriteria()` builder. Check out the [Criteria Builder](/v5.x/criteria-queries/criteria-builder.md) section for all its methods.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://coldbox-orm.ortusbooks.com/v5.x/virtual-services/concrete-virtual-services.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
