Business Objects Implementation Example
This example explains how to implement ice.NET Business Objects (BOs). The
data model that is used for this example is part of the ice.NET Ice Car Rental
turorial and can be found in the PDTec.ICR model package.
The example consists of multiple parts:
- The BO interface definition.
- The BO implementation.
- The BO factory class with the declaration of associated ice.NET object types.
- The registration of BO assemblies.
- A usage example.
The first part of the example shows a BO interface definition. It is a normal
C# interface that derives from the PDTec.IceNet.Core.Model.IObject
interface. It can contain arbitrary methods and properties .
The following code is stored in the file IVehicle.cs:
using System;
using PDTec.IceNet.Core.Model;
namespace PDTec.ICR.BusinessLogic.BusinessObjects
{
public interface IVehicle : IObject
{
double CurrentMileage
{
get;
set;
}
double CalculatePrice(DateTime pFromDate, DateTime pToDate);
}
}
The interface IVehicle is implemented in the next code example.
To implement a BO interface a class is required that is derived from
PDTec.IceNet.Sdk.Implementation.CObjectImplBase" and implements
the BO interface.
The following code is stored in the file CVehicleImpl.cs:
using System;
using System.Collections;
using PDTec.IceNet.Core.Model;
using PDTec.IceNet.Core.Model.Implementation;
using PDTec.IceNet.Sdk.Implementation;
using PDTec.ICR.BusinessLogic.BusinessObjects;
namespace PDTec.ICR.BusinessLogic.BusinessObjects.Implementation
{
public class CVehicleImpl : CObjectImplBase, IVehicle
{
public CVehicleImpl(IRepository pRepository, IObjectImpl pObject) :
base(pRepository, pObject)
{
}
public double CurrentMileage
{
get
{
return (double)GetAttrValue("PDTec.ICR.Vehicle", "CurrentMileage");
}
set
{
SetAttrValue("PDTec.ICR.Vehicle", "CurrentMileage", value);
}
}
private IModel Model
{
get
{
IRelationship pRelationship = GetSingleRelationship("PDTec.ICR.VehicleModel", RelDirection.Forward);
IModel pResult = pRelationship.GetObject(Direction.To).Cast<IModel>();
return pResult;
}
}
public double CalculatePrice(DateTime pFromDate, DateTime pToDate)
{
TimeSpan pTimeSpan = pToDate - pFromDate;
double price = pTimeSpan.Days * DailyRate - Model.CalculateRebate(this);
return price;
}
}
}
To associate the BO implementation with a modeled object type a BO factory
is required. This factory class is decorated with a code attributes of type
ObjectImplAttribute and implements IObjectImplFactory.
The property ObjectImplAttribute.ObjTypeName defines the association
with the ice.NET object type. All object instances of this type (or its subtypes)
provide the BO methods declated in the BO interface.
The following code is stored in the file CVehicleImplFactory.cs:
using PDTec.IceNet.Core.Model;
using PDTec.IceNet.Core.Model.Implementation;
namespace PDTec.ICR.BusinessLogic.BusinessObjects.Implementation
{
[ObjectImpl(ObjTypeName="PDTec.ICR.Vehicle", Interface=typeof(IVehicle))]
public class CVehicleImplFactory : IObjectImplFactory
{
public IObject CreateObject(IRepository pRepository, IObjectImpl pBaseImpl)
{
return new CVehicleImpl(pRepository, pBaseImpl);
}
}
}
The BO factory provides the connection between interface, implementation and
data model. In order to be used in an application, the containing .NET assembly
must be registered with the .NET platform. The easies way to do this is to add
a <implementationAssembly> tag that contains the assembly name into
the application's configuration file as shown in the follwing example.
If the BO interface, the implementation and the factory class are distributed over
multiple assemblies, the assembly containing the factory class must be registered
in the configuration file.
The following code is part of a .NET configuration file (e.g. web.config):
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="ice.net" type="PDTec.IceNet.Sdk.ConfigSectionHandler,PDTec.IceNet.Sdk"/>
</configSections>
<ice.net>
<database service="SQLServer" connectionString="server=(local)\SQLEXPRESS;trusted_connection=yes;database=icr" encrypted="false"/>
<businessObjects>
<implementationAssembly assemblyName="PDTec.ICR.BusinessLogic"/>
</businessObjects>
<!-- ... -->
</ice.net>
<!-- ... -->
</configuration>
The following code shows how to use the BO methods:
IVehicle pVehicle = Repository.Get<IVehicle>(Request["vid"]);
double price = pVehicle.CalculatePrice(DateTime.UtcNow, DateTime.UtcNow.AddDays(2.0));