# Create an Observable Object

When it comes to making an observable class, it is important to take into account some considerations that can facilitate obtaining the desired behavior. This guide presents the principal aspects to create observable classes.

## How to indicate a class is observable Class <a href="#how-to-indicate-a-class-is-observable-class" id="how-to-indicate-a-class-is-observable-class"></a>

To create an observable class, first of all, you need to add the **`Observable`** attribute.

```
using Mobilize.WebMAP.Common.Attribute;

[Observable]
public class Class1
{
    .
    .
    .
}
```

### Classes Observations <a href="#classes-observations" id="classes-observations"></a>

* An observable class can have `private`, `internal` or `public` modifier.
* An observable class can be `sealed`.
* An observable class can be `abstract`.
* When you add `Observable` attribute to a class. All classes that inherit from that class will be observables. Even if the attribute is not present in one of those classes.

## How to add a Property that can notify changes in an observable class <a href="#how-to-add-a-property-that-can-notify-changes-in-an-observable-class" id="how-to-add-a-property-that-can-notify-changes-in-an-observable-class"></a>

To add a property that you want it to notify changes, you need to declare it as an auto implemented property with the attribute `Intercepted`

```
    [Intercepted]
    public string MyProperty
    {
        get;
        set;
    }
```

When declaring an intercepted property, consider the following recommendations:

* Properties can be virtual or non virtual.
* Properties cannot be abstract.
* Properties can be `public`, `private`, `internal` or `protected`.
* Properties must be auto implemented
* Property type can be any value type or string, or any type marked as an observable.
* Properties can be static

### Properties Observations <a href="#properties-observations" id="properties-observations"></a>

Remember: Fields and events can not be intercepted. So, state in classes that needs to notify changes must be declared as properties.

You can use non auto implemented properties, but you will not be allowed to add `Intercepted` attribute.

```
public string Property1
{
    get
    {
        // Example code for get block
        return this.GetPropertyValue("Property1");
    }
    set
    {
        // Example code for set block
        this.SetPropertyValue("Property1", value);
    }
}
```

## How to properly declare an Event in an observable class <a href="#how-to-properly-declare-an-event-in-an-observable-class" id="how-to-properly-declare-an-event-in-an-observable-class"></a>

To create an Event in your observable class. You must declare a Property which will have a delegate as the return type. Been a property, you need to declare it as an auto implemented property with the attribute `Intercepted`.

With the aim that the event can notify their changes, it is necessary to declare it in the aforementioned way. If instead of property it is declared as an event, the operation will not be as expected, since the changes will not be notified and the stored value will be lost. What may affect the reception of invocations to the event as part of a new request from the client.

```
    [Intercepted]
    public EventHandler Click
    {
        get;
        set;
    }
```

## How to indicate a static observable class <a href="#how-to-indicate-a-static-observable-class" id="how-to-indicate-a-static-observable-class"></a>

Just as one can declare an instance class as observable, it can also be done with static classes.

```
using Mobilize.WebMAP.Common.Attribute;

[Observable]
public static class class
{

}
```

## How to use static members <a href="#how-to-use-static-members" id="how-to-use-static-members"></a>

Regardless of whether the class is static or not, we can use static members

### Constructors <a href="#constructors" id="constructors"></a>

A static constructor can be used in an *observable* class. When the static constructor is inside an observable class it will be executed *once per session*\*.

When a static constructor is inside a class without a `Observable` attribute, it will be executed only once and it will affect the application widely.

```
[Observable]
public static class Class1
{
    public static Class1 ()
    {
        .
        .
        .
    }
}
```

### Properties <a href="#properties" id="properties"></a>

If you use static properties **without** `intercepted` attribute, it will have a different behavior, so you must be careful. In this case, the static properties state will behave as wide application state.

### Methods <a href="#methods" id="methods"></a>

If you have a class with only static methods, that is, without properties or constructors. It is not necessary to declare the static class as observable.

## How to declare Structs <a href="#how-to-declare-structs" id="how-to-declare-structs"></a>

Structs cannot be observable, so, a struct should be converted to an intercepted class. See the following example:

For the following input Struct.

```
    public struct MySruct
    {
        public string Prop1
        {
            get;
            set;
        }
    }
```

You must convert it to a class as the following code represents:

Example:

```
[Observable]
public class MySruct
{
    [Intercepted]
    public string Prop1
    {
        get;
        set;
    }
}
```


---

# 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.gapvelocity.ai/webmap/general/backend/dcp-desktop-compatibly-platform/library-bundles/bundle-library/create-an-observable-object.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.
