Xomega Framework promotes a design pattern for a service layer that provides flexibility and reusability when defining and working with the service layer. This is described in details in the article on
how to Build Flexible and Reusable WCF Services.
This approach requires ability to call several service operations within the same session, where changes from the previous calls will be visible in subsequent calls in the same session. The easiest way to enable this is to use WCF bindings that support sessions, such as
netTcpBinding. In this case, you can create a WCF channel on the client side, call several operations on the same channel, and then close the channel.
Note, that Silverlight doesn't support such bindings, so you can read
how to enable WCF conversation sessions in Silverlight.
The following snippet shows a client endpoint configuration that uses
netTcpBinding.
Code:
<system.serviceModel>
<client>
<endpoint name="IEmployeeService" address="net.tcp://localhost:8002/EmployeeService"
binding="netTcpBinding"
contract="AdventureWorks.Services.IEmployeeService"/>
</client>
</system.serviceModel>
The method below demonstrates how
Create,
Update and
Save Changes operations are called in sequence on the employee service within the same session to create or update an employer record.
Code:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
ChannelFactory<IEmployeeService> cfEmployee = new ChannelFactory<IEmployeeService>("IEmployeeService");
IEmployeeService svcEmployee = cfEmployee.CreateChannel();
// for new objects create the object and store a temporary key
if (isNew) obj.FromDataContract(svcEmployee.Create());
// update object values
Employee_UpdateInput inUpdate = new Employee_UpdateInput();
obj.ToDataContract(inUpdate);
svcEmployee.Update(inUpdate);
svcEmployee.SaveChanges(true);
cfEmployee.Close();
}