How to: Add Methods and Non-Persistent Properties
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
When working with automatically generated source (like the classes generated by the Visual Designer), code can be added to the class without having to modify the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code and so on. You can create code that uses these classes without having to edit the file created by Visual Studio. Using partial classes is a perfect solution for separating auto-generated code from developer code.
You may ask yourself "Why do I need to use partial classes?". You could try the following experiment. Modify any domain model class by adding a new dummy method to the class definition. After that, open your domain model in the Visual Designer and save it (Ctrl+S). Now take a look at the class again. The method you just added is gone and the original definition of the class has been restored. All classes (including the OpenAccessContext) that are part of your domain model are automatically generated by Telerik Data Access. Telerik Data Access uses code generation templates to generate the entities and the context.
Do not modify the original source file of a domain model class or a context. The OpenAccessContext and the domain classes are auto-generated. Each time you re-save your domain model, all changes will be lost.
In this topic, you will learn how to extend the generated domain classes and OpenAccessContext by adding new methods and non-persistent properties:
- Extending Domain Classes with New Methods
- Extending Domain Classes with New Properties
- Adding Properties with Backing Field in Domain Classes with Attributes Mapping
-
Extending the OpenAccessContext with New Methods and Properties
- Next Steps
How to Extend Domain Model Classes with New Methods
Consider the following domain model that contains two entities - Product and Category.
The following example demonstrates how to add a new method to the Product entity:
- Create a new class with the same name as the name of the entity you want to extend. Use the following naming pattern: <DomainClassName>.partial.cs for C# or <DomainClassName>.partial.vb for VB. For this example, the class file name will be Product.partial.cs.
-
Mark the class with the partial keyword.
public partial class Product { }
Partial Public Class Product End Class
-
The new class should be in the same namespace as the namespace of the generated domain entity. In this case, the generated Product class is in the DemoApplication namespace. That's why the namespace of the new partial class will also be DemoApplication.
namespace DemoApplication { public partial class Product { } }
Namespace DemoApplication Partial Public Class Product End Class End Namespace
-
Implement your business logic. For example:
namespace DemoApplication { public partial class Product { public string ProductSummary() { return this.ProductName + " (" + this.Category.CategoryName + ")"; } } }
Namespace DemoApplication Partial Public Class Product Public Function ProductSummary() As String Return Me.ProductName & " (" & Me.Category.CategoryName & ")" End Function End Class End Namespace
-
Using the generated method is straightforward.
using (EntitiesModel dbContext = new EntitiesModel()) { foreach (Product product in dbContext.Products.Take(3)) { Console.WriteLine(product.ProductSummary()); } }
Using dbContext As New EntitiesModel() For Each _product As Product In dbContext.Products.Take(3) Console.WriteLine(_product.ProductSummary()) Next _product End Using
How to Extend Domain Model Classes with New Properties
Extending your domain classes with new properties is easy and could be achieved by following the same pattern. The only thing that should be pointed out here is that the new properties won't be persistent. For example:
public partial class Product
{
public string ProductSummary
{
get
{
return this.ProductName + " (" + this.Category.CategoryName + ")";
}
}
}
Partial Public Class Product
Public ReadOnly Property ProductSummary() As String
Get
Return Me.ProductName & " (" & Me.Category.CategoryName & ")"
End Get
End Property
End Class
How to Add Properties with Backing Field in Domain Classes with Attributes Mapping
A special case is when you want to add new properties with backing fields and the domain model uses Attributes Mapping in the same time. In this scenario, you need to mark the backing field with the Telerik.OpenAccess.TransientAttribute.
public partial class Product
{
[Telerik.OpenAccess.Transient]
private string productSummary;
public string ProductSummary
{
get
{
this.productSummary = this.ProductName + ":" + this.Category.CategoryName;
return this.productSummary;
}
set
{
if ( string.IsNullOrWhiteSpace( value ) )
{
return;
}
this.productSummary = null;
string[] data = value.Split( new[] { ":" }, StringSplitOptions.None );
this.ProductName = data[0];
this.Category.CategoryName = data[1];
}
}
}
Partial Public Class Product
<Telerik.OpenAccess.Transient>
Private _productSummary As String
Public Property ProductSummary() As String
Get
Me._productSummary = Me.ProductName & ":" & Me.Category.CategoryName
Return Me._productSummary
End Get
Set(ByVal value As String)
If String.IsNullOrWhiteSpace(value) Then
Return
End If
Me._productSummary = Nothing
Dim data() As String = value.Split({":"}, StringSplitOptions.None)
Me.ProductName = data(0)
Me.Category.CategoryName = data(1)
End Set
End Property
End Class
How to Extend the OpenAccessContext with New Methods and Properties
The process of extending the OpenAccessContext with new methods and properties is the same. Basically, what you need to do is:
-
Create a new class with the same name as the name of the context class. Use the following naming pattern: <ContextClassName>.partial.cs for C# or <ContextClassName>.partial.vb for VB.
Mark the class with the partial keyword.
-
The new class should be in the same namespace as the namespace of the generated OpenAccessContext. In this case, the generated EntitiesModel class is in the DemoApplication namespace. That's why the namespace of the new partial class will also be DemoApplication.
namespace DemoApplication { public partial class EntitiesModel { } }
Namespace DemoApplication Partial Public Class EntitiesModel End Class End Namespace
-
Implement your business logic. For example:
using System.Linq; namespace DemoApplication { public partial class EntitiesModel { public decimal AverageProductPrice() { return this.Products.Average( p => p.UnitPrice ); } } }
Namespace DemoApplication Partial Public Class EntitiesModel Public Function AverageProductPrice() As Decimal Return Me.Products.Average(Function(p) p.UnitPrice) End Function End Class End Namespace
-
Using the generated method is straightforward.
using ( EntitiesModel dbContext = new EntitiesModel() ) { Console.WriteLine( dbContext.AverageProductPrice() ); }
Using dbContext As New EntitiesModel() Console.WriteLine(dbContext.AverageProductPrice()) End Using
Next Steps
Another way to customize the generated by the Visual Designer classes is to modify the code generation templates. A common scenario is to automatically implement the INotifyPropertyChanged/ing interfaces. For more information, check out the Customizing Code Generation section.