# Customizing Generated Code

\[ **This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date** \
&#x20;Please see [Release Notes](https://github.com/OpenRIAServices/OpenRiaServices/releases) or [Changelog](https://github.com/OpenRIAServices/OpenRiaServices/blob/main/Changelog.md) for a list of changes since WCF RIA Services ]

This topic describes how to customize the code generated on a Open Ria Services client. In some cases for Open Ria Services, you want to add to the code that is generated for your client project. However, you cannot directly customize the generated code because your changes will be overwritten the next time the middle tier code is compiled. Open Ria Services provides partial methods in the generated code that you can implement in a separate code file to customize the client tier code. These partial methods are "hook points" that you can use to attach your code to the generated code. The methods are called only when you have created a corresponding partial method.

For information about customizing the generated code to compute new values based on properties in the entity class, see [How to: Add Computed Properties on the Client](/openriaservices/ee707349/ee707345/ee707331.md).

## Partial Methods

The Open Ria Services framework generates partial methods for domain context classes and the entity classes.

For domain context classes, the following partial method is provided.

| Member      | Use                                                                                                                                                                                     |
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| OnCreated() | Executed when the [DomainContext](https://github.com/OpenRIAServices/OpenRiaServices/tree/086ea8c8fcb115000749be6b2b01cd43bb95bf80/docs/ff422732\(v=vs.91\).md) object is instantiated. |

For entity classes, the following partial methods are provided.

| Member                        | User                                                                                                                                                                                                                   |
| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| OnCreated()                   | Executed when the entity object is instantiated.                                                                                                                                                                       |
| OnLoaded(boolean)             | Executed either when the entity is loaded and deserialized for the first time, or when the entity is deserialized from the server, but it already exists on the client.                                                |
| On\[PropertyName]Changing     | Called after validation, but before the value is set.                                                                                                                                                                  |
| On\[PropertyName]Changed      | Called just after value is set and before the [RaiseDataMemberChanged](https://github.com/OpenRIAServices/OpenRiaServices/tree/086ea8c8fcb115000749be6b2b01cd43bb95bf80/docs/ff423101\(v=vs.91\).md) method is called. |
| On\[CustomMethodName]Invoking | Called when custom method is called, but before it is invoked.                                                                                                                                                         |
| On\[CustomMethodName]Invoked  | Called after custom method is called and invoked.                                                                                                                                                                      |

## Implementing the Partial Methods

To use these methods, you add a partial class with the same name and namespace as the generated class you want to customize. Because the automatically-generated client code has the same namespace as the code on the server project, the namespace for your partial class will typically have the format projectname.Web. Then, you implement the method that is executed at the time when your custom code must be executed. For example, to load a domain context when it is created, you add the following code.

```
Imports OpenRiaServices.Client
Namespace Web
  Partial Public Class EmployeeDomainContext
    Inherits DomainContext
    Private Sub OnCreated()
      Me.Load(Me.GetEmployeesQuery())
    End Sub
  End Class
End Namespace
```

```csharp
using OpenRiaServices.Client;

namespace RIAServiceExample.Web
{
  public partial class EmployeeDomainContext : DomainContext
  {
    partial void OnCreated()
    {
      this.Load(this.GetEmployeesQuery());
    }
  }
}
```

You can set properties on the generated entity class in a partial method. For example, if the Employee table in your database includes a field named CreatedBy, you can set the value of the property by implementing a partial method for OnCreated(). To track who created a new instance of an entity, you add the following code.

```
Imports OpenRiaServices.Client
Namespace Web
  Partial Public Class Employee
    Inherits Entity
    Private Sub OnCreated()
      Me.CreatedBy = WebContext.Current.User.Name
    End Sub
  End Class
End Namespace
```

```csharp
using OpenRiaServices.Client;

namespace RIAServiceExample.Web
{
  public partial class Employee : Entity
  {
    partial void OnCreated()
    {
      this.CreatedBy = WebContext.Current.User.Name;
    }
  }
}
```

## See Also

### Tasks

[How to: Add Computed Properties on the Client](/openriaservices/ee707349/ee707345/ee707331.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://openriaservices.gitbook.io/openriaservices/ee707349/ee707345.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.
