# MementoEntity and MementoEntityCollection

In [Change Tracking Service](/memento/change-tracking-service.md) we introduced a code snippet as the following:

```csharp
var person = new Person();
person.FirstName = "first name value";
person.LastName = "last name value";
```

To start using memento services we can extend the above snippet as follows:

```csharp
var memento = new ChangeTrackingService();

var person = new Person();
memento.Attach(person);

person.FirstName = "first name";
person.LastName = "last name";

var isChanged = memento.IsChanged; //true
var canUndo = memento.CanUndo; //true
```

Calling `memento.Undo()` will trigger the undo of the last operation, we can call `Undo` until `CanUndo` is `true` rolling back changes change by change. In the above sample calling `Undo` will revert back the `LastName` property value to its default value.

The requirement to achieve the above is that the `Person` class is a Radical *memento* entity:

```csharp
class Person : MementoEntity
{   
    public String FirstName
    {
        get { return this.GetPropertyValue(() => this.FirstName); }
        set { this.SetPropertyValue(() => this.FirstName, value); }
    }

    public String LastName
    {
        get { return this.GetPropertyValue(() => this.LastName); }
        set { this.SetPropertyValue(() => this.LastName, value); }
    }
}
```

As we can see from the above snippet all we need to do is to create a class the inherits from the `MementoEntity` base class and declare all the properties we want to track as [Radical properties](https://github.com/RadicalFx/documentation/blob/master/entities/property-system.md).

A similar approach can be used to keep track of items in a collection:

```csharp
var memento = new ChangeTrackingService();

var list = new MementoEntityCollection<String>();
memento.Attach(list);

list.Add("a value");
list.Add("another value");

var isChanged = memento.IsChanged; //true
var canUndo = memento.CanUndo; //true
```

Calling `memento.Undo()` will trigger the undo of the last operation that in the above sample will revert the collection status removing the last added value.

We can do more:

* [Handling change tracking in a simple model](/memento/change-tracking-service/handling-change-tracking/simple-model.md);
* [Handling change tracking in collections](/memento/change-tracking-service/handling-change-tracking/collections.md);
* [Handling change tracking in complex objects graph](/memento/change-tracking-service/handling-change-tracking/complex-graph.md);


---

# Agent Instructions: 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:

```
GET https://docs.radicalframework.com/memento/change-tracking-service/memento-entities.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
