> For the complete documentation index, see [llms.txt](https://estiom.gitbook.io/bloqs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://estiom.gitbook.io/bloqs/implementing-command-handlers/implementing-the-newentity-command.md).

# Implementing the NewEntity Command

In Bloqs, every entity can be created via a command. By default, if you **don’t implement a NewEntity command**, Bloqs will automatically instantiate a new entity object.

Implementing your own **NewEntity command** is useful when you want to **initialize certain fields** or run **custom logic** when a new entity is created.

***

### How to Implement

You create a class that implements `ICommandHandler<NewEntityCommand, NewEntityCommandResult>` and decorate it with the `[EntityCommandHandler<TEntity>]` attribute, where `TEntity` is your entity type.

This can be done **both in the API project** (code runs on the server) **and in the Blazor UI project** (code runs in WebAssembly).

***

### Sample Code

```csharp
using Bloqs.App.Engine.Commands;
using Bloqs.App.Engine.Commands.Data;
using Templates.SampleApp.Models.Data;

namespace Templates.SampleApp.Api.Commands.Activities;

[EntityCommandHandler<Activity>]
public class NewActivity : ICommandHandler<NewEntityCommand, NewEntityCommandResult>
{
    public Task<NewEntityCommandResult> HandleAsync(
        NewEntityCommand command,
        CancellationToken cancellationToken = default
    )
    {
        var newActivity = new Activity()
        {
            Name = "New Activity",
            Description = "New Activity Description",
        };

        return Task.FromResult(NewEntityCommandResult.CreateSuccess(newActivity));
    }
}
```

***

### Key Points

* **Custom initialization**: You can set default values or generate IDs, timestamps, or related entities.
* **Optional**: If not implemented, Bloqs will just create a plain instance of the entity.
* **Seamless integration**: Your command is automatically called whenever a new entity of that type is created in the app.
* **UI & API**: Implementing the command in the **UI project** executes in the browser via Blazor WASM, while in the **API project** it runs on the server.


---

# 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://estiom.gitbook.io/bloqs/implementing-command-handlers/implementing-the-newentity-command.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.
