using System;
namespace Csla.DataPortalClient
{
  /// 
  /// Implements a data portal proxy to relay data portal
  /// calls to an application server hosted in COM+.
  /// 
  public abstract class EnterpriseServicesProxy : DataPortalClient.IDataPortalProxy
  {
    /// 
    /// Override this method to return a reference to
    /// the server-side COM+ (ServicedComponent) object
    /// implementing the data portal server functionality.
    /// 
    protected abstract Server.Hosts.EnterpriseServicesPortal GetServerObject();
    /// 
    /// Called by  to create a
    /// new business object.
    /// 
    /// Type of business object to create.
    /// Criteria object describing business object.
    /// 
    ///  object passed to the server.
    /// 
    public virtual Server.DataPortalResult Create(Type objectType, object criteria, Server.DataPortalContext context)
    {
      Server.Hosts.EnterpriseServicesPortal svc = GetServerObject();
      try
      {
        return svc.Create(objectType, criteria, context);
      }
      finally
      {
        if (svc != null)
          svc.Dispose();
      }
    }
    /// 
    /// Called by  to load an
    /// existing business object.
    /// 
    /// Type of business object to retrieve.
    /// Criteria object describing business object.
    /// 
    ///  object passed to the server.
    /// 
    public virtual Server.DataPortalResult Fetch(Type objectType, object criteria, Server.DataPortalContext context)
    {
      Server.Hosts.EnterpriseServicesPortal svc = GetServerObject();
      try
      {
        return svc.Fetch(objectType, criteria, context);
      }
      finally
      {
        if (svc != null)
          svc.Dispose();
      }
    }
    /// 
    /// Called by  to update a
    /// business object.
    /// 
    /// The business object to update.
    /// 
    ///  object passed to the server.
    /// 
    public virtual Server.DataPortalResult Update(object obj, Server.DataPortalContext context)
    {
      Server.Hosts.EnterpriseServicesPortal svc = GetServerObject();
      try
      {
        return svc.Update(obj, context);
      }
      finally
      {
        if (svc != null)
          svc.Dispose();
      }
    }
    /// 
    /// Called by  to delete a
    /// business object.
    /// 
    /// Criteria object describing business object.
    /// 
    ///  object passed to the server.
    /// 
    public virtual Server.DataPortalResult Delete(object criteria, Server.DataPortalContext context)
    {
      Server.Hosts.EnterpriseServicesPortal svc = GetServerObject();
      try
      {
        return svc.Delete(criteria, context);
      }
      finally
      {
        if (svc != null)
          svc.Dispose();
      }
    }
    /// 
    /// Get a value indicating whether this proxy will invoke
    /// a remote data portal server, or run the "server-side"
    /// data portal in the caller's process and AppDomain.
    /// 
    public virtual bool IsServerRemote
    {
      get { return true; }
    }
  }
}