# 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.
