> 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://estiom.gitbook.io/bloqs/implementing-command-handlers/implementing-the-newentity-command.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
