Commit for development environment setup
This commit is contained in:
@@ -0,0 +1,448 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Remoting;
|
||||
using System.Runtime.Remoting.Channels;
|
||||
using System.Runtime.Remoting.Channels.Http;
|
||||
using Csla.Properties;
|
||||
|
||||
namespace Csla
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// This is the client-side DataPortal as described in
|
||||
/// Chapter 4.
|
||||
/// </summary>
|
||||
public static class DataPortal
|
||||
{
|
||||
|
||||
#region DataPortal events
|
||||
|
||||
/// <summary>
|
||||
/// Raised by DataPortal prior to calling the
|
||||
/// requested server-side DataPortal method.
|
||||
/// </summary>
|
||||
public static event Action<DataPortalEventArgs> DataPortalInvoke;
|
||||
|
||||
/// <summary>
|
||||
/// Raised by DataPortal after the requested
|
||||
/// server-side DataPortal method call is complete.
|
||||
/// </summary>
|
||||
public static event Action<DataPortalEventArgs> DataPortalInvokeComplete;
|
||||
|
||||
private static void OnDataPortalInvoke(DataPortalEventArgs e)
|
||||
{
|
||||
Action<DataPortalEventArgs> action = DataPortalInvoke;
|
||||
if (action != null)
|
||||
action(e);
|
||||
}
|
||||
|
||||
private static void OnDataPortalInvokeComplete(DataPortalEventArgs e)
|
||||
{
|
||||
Action<DataPortalEventArgs> action = DataPortalInvokeComplete;
|
||||
if (action != null)
|
||||
action(e);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Data Access methods
|
||||
|
||||
private const int EmptyCriteria = 1;
|
||||
|
||||
/// <summary>
|
||||
/// Called by a factory method in a business class to create
|
||||
/// a new object, which is loaded with default
|
||||
/// values from the database.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Specific type of the business object.</typeparam>
|
||||
/// <param name="criteria">Object-specific criteria.</param>
|
||||
/// <returns>A new object, populated with default values.</returns>
|
||||
public static T Create<T>(object criteria)
|
||||
{
|
||||
return (T)Create(typeof(T), criteria);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by a factory method in a business class to create
|
||||
/// a new object, which is loaded with default
|
||||
/// values from the database.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Specific type of the business object.</typeparam>
|
||||
/// <returns>A new object, populated with default values.</returns>
|
||||
public static T Create<T>()
|
||||
{
|
||||
return (T)Create(typeof(T), EmptyCriteria);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by a factory method in a business class to create
|
||||
/// a new object, which is loaded with default
|
||||
/// values from the database.
|
||||
/// </summary>
|
||||
/// <param name="criteria">Object-specific criteria.</param>
|
||||
/// <returns>A new object, populated with default values.</returns>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2223:MembersShouldDifferByMoreThanReturnType")]
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
|
||||
public static object Create(object criteria)
|
||||
{
|
||||
return Create(MethodCaller.GetObjectType(criteria), criteria);
|
||||
}
|
||||
|
||||
private static object Create(Type objectType, object criteria)
|
||||
{
|
||||
Server.DataPortalResult result;
|
||||
|
||||
MethodInfo method = MethodCaller.GetCreateMethod(objectType, criteria);
|
||||
|
||||
DataPortalClient.IDataPortalProxy proxy;
|
||||
proxy = GetDataPortalProxy(RunLocal(method));
|
||||
|
||||
Server.DataPortalContext dpContext =
|
||||
new Csla.Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote);
|
||||
|
||||
OnDataPortalInvoke(new DataPortalEventArgs(dpContext));
|
||||
|
||||
try
|
||||
{
|
||||
result = proxy.Create(objectType, criteria, dpContext);
|
||||
}
|
||||
catch (Server.DataPortalException ex)
|
||||
{
|
||||
result = ex.Result;
|
||||
if (proxy.IsServerRemote)
|
||||
ApplicationContext.SetGlobalContext(result.GlobalContext);
|
||||
throw new DataPortalException(
|
||||
string.Format("DataPortal.Create {0} ({1})", Resources.Failed, ex.InnerException.InnerException),
|
||||
ex.InnerException, result.ReturnObject);
|
||||
}
|
||||
|
||||
if (proxy.IsServerRemote)
|
||||
ApplicationContext.SetGlobalContext(result.GlobalContext);
|
||||
|
||||
OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext));
|
||||
|
||||
return result.ReturnObject;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by a factory method in a business class to retrieve
|
||||
/// an object, which is loaded with values from the database.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Specific type of the business object.</typeparam>
|
||||
/// <param name="criteria">Object-specific criteria.</param>
|
||||
/// <returns>An object populated with values from the database.</returns>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2223:MembersShouldDifferByMoreThanReturnType")]
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.DataPortalException.#ctor(System.String,System.Exception,System.Object)")]
|
||||
public static T Fetch<T>(object criteria)
|
||||
{
|
||||
return (T)Fetch(typeof(T), criteria);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by a factory method in a business class to retrieve
|
||||
/// an object, which is loaded with values from the database.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Specific type of the business object.</typeparam>
|
||||
/// <returns>An object populated with values from the database.</returns>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2223:MembersShouldDifferByMoreThanReturnType")]
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.DataPortalException.#ctor(System.String,System.Exception,System.Object)")]
|
||||
public static T Fetch<T>()
|
||||
{
|
||||
return (T)Fetch(typeof(T), EmptyCriteria);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by a factory method in a business class to retrieve
|
||||
/// an object, which is loaded with values from the database.
|
||||
/// </summary>
|
||||
/// <param name="criteria">Object-specific criteria.</param>
|
||||
/// <returns>An object populated with values from the database.</returns>
|
||||
public static object Fetch(object criteria)
|
||||
{
|
||||
return Fetch(MethodCaller.GetObjectType(criteria), criteria);
|
||||
}
|
||||
|
||||
private static object Fetch(Type objectType, object criteria)
|
||||
{
|
||||
Server.DataPortalResult result;
|
||||
|
||||
MethodInfo method = MethodCaller.GetFetchMethod(objectType, criteria);
|
||||
|
||||
DataPortalClient.IDataPortalProxy proxy;
|
||||
proxy = GetDataPortalProxy(RunLocal(method));
|
||||
|
||||
Server.DataPortalContext dpContext =
|
||||
new Server.DataPortalContext(GetPrincipal(),
|
||||
proxy.IsServerRemote);
|
||||
|
||||
OnDataPortalInvoke(new DataPortalEventArgs(dpContext));
|
||||
|
||||
try
|
||||
{
|
||||
result = proxy.Fetch(objectType, criteria, dpContext);
|
||||
}
|
||||
catch (Server.DataPortalException ex)
|
||||
{
|
||||
result = ex.Result;
|
||||
if (proxy.IsServerRemote)
|
||||
ApplicationContext.SetGlobalContext(result.GlobalContext);
|
||||
throw new DataPortalException(
|
||||
String.Format("DataPortal.Fetch {0} ({1})", Resources.Failed, ex.InnerException.InnerException),
|
||||
ex.InnerException, result.ReturnObject);
|
||||
}
|
||||
|
||||
if (proxy.IsServerRemote)
|
||||
ApplicationContext.SetGlobalContext(result.GlobalContext);
|
||||
|
||||
OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext));
|
||||
|
||||
return result.ReturnObject;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called to execute a Command object on the server.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// To be a Command object, the object must inherit from
|
||||
/// <see cref="CommandBase">CommandBase</see>.
|
||||
/// </para><para>
|
||||
/// Note that this method returns a reference to the updated business object.
|
||||
/// If the server-side DataPortal is running remotely, this will be a new and
|
||||
/// different object from the original, and all object references MUST be updated
|
||||
/// to use this new object.
|
||||
/// </para><para>
|
||||
/// On the server, the Command object's DataPortal_Execute() method will
|
||||
/// be invoked. Write any server-side code in that method.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <typeparam name="T">Specific type of the Command object.</typeparam>
|
||||
/// <param name="obj">A reference to the Command object to be executed.</param>
|
||||
/// <returns>A reference to the updated Command object.</returns>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters",
|
||||
MessageId="Csla.DataPortalException.#ctor(System.String,System.Exception,System.Object)")]
|
||||
public static T Execute<T>(T obj) where T : CommandBase
|
||||
{
|
||||
return (T)Update(obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called to execute a Command object on the server.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Note that this method returns a reference to the updated business object.
|
||||
/// If the server-side DataPortal is running remotely, this will be a new and
|
||||
/// different object from the original, and all object references MUST be updated
|
||||
/// to use this new object.
|
||||
/// </para><para>
|
||||
/// On the server, the Command object's DataPortal_Execute() method will
|
||||
/// be invoked. Write any server-side code in that method.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="obj">A reference to the Command object to be executed.</param>
|
||||
/// <returns>A reference to the updated Command object.</returns>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId="Csla.DataPortalException.#ctor(System.String,System.Exception,System.Object)")]
|
||||
public static CommandBase Execute(CommandBase obj)
|
||||
{
|
||||
return (CommandBase)Update(obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by the business object's Save() method to
|
||||
/// insert, update or delete an object in the database.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Note that this method returns a reference to the updated business object.
|
||||
/// If the server-side DataPortal is running remotely, this will be a new and
|
||||
/// different object from the original, and all object references MUST be updated
|
||||
/// to use this new object.
|
||||
/// </remarks>
|
||||
/// <typeparam name="T">Specific type of the business object.</typeparam>
|
||||
/// <param name="obj">A reference to the business object to be updated.</param>
|
||||
/// <returns>A reference to the updated business object.</returns>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.DataPortalException.#ctor(System.String,System.Exception,System.Object)")]
|
||||
public static T Update<T>(T obj)
|
||||
{
|
||||
return (T)Update((object)obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by the business object's Save() method to
|
||||
/// insert, update or delete an object in the database.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Note that this method returns a reference to the updated business object.
|
||||
/// If the server-side DataPortal is running remotely, this will be a new and
|
||||
/// different object from the original, and all object references MUST be updated
|
||||
/// to use this new object.
|
||||
/// </remarks>
|
||||
/// <param name="obj">A reference to the business object to be updated.</param>
|
||||
/// <returns>A reference to the updated business object.</returns>
|
||||
public static object Update(object obj)
|
||||
{
|
||||
Server.DataPortalResult result;
|
||||
|
||||
MethodInfo method;
|
||||
string methodName;
|
||||
if (obj is CommandBase)
|
||||
methodName = "DataPortal_Execute";
|
||||
else if (obj is Core.BusinessBase)
|
||||
{
|
||||
Core.BusinessBase tmp = (Core.BusinessBase)obj;
|
||||
if (tmp.IsDeleted)
|
||||
methodName = "DataPortal_DeleteSelf";
|
||||
else
|
||||
if (tmp.IsNew)
|
||||
methodName = "DataPortal_Insert";
|
||||
else
|
||||
methodName = "DataPortal_Update";
|
||||
}
|
||||
else
|
||||
methodName = "DataPortal_Update";
|
||||
|
||||
method = MethodCaller.GetMethod(obj.GetType(), methodName);
|
||||
|
||||
DataPortalClient.IDataPortalProxy proxy;
|
||||
proxy = GetDataPortalProxy(RunLocal(method));
|
||||
|
||||
Server.DataPortalContext dpContext =
|
||||
new Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote);
|
||||
|
||||
OnDataPortalInvoke(new DataPortalEventArgs(dpContext));
|
||||
|
||||
try
|
||||
{
|
||||
result = proxy.Update(obj, dpContext);
|
||||
}
|
||||
catch (Server.DataPortalException ex)
|
||||
{
|
||||
result = ex.Result;
|
||||
if (proxy.IsServerRemote)
|
||||
ApplicationContext.SetGlobalContext(result.GlobalContext);
|
||||
throw new DataPortalException(
|
||||
String.Format("DataPortal.Update {0} ({1})", Resources.Failed, ex.InnerException.InnerException),
|
||||
ex.InnerException, result.ReturnObject);
|
||||
}
|
||||
|
||||
if (proxy.IsServerRemote)
|
||||
ApplicationContext.SetGlobalContext(result.GlobalContext);
|
||||
|
||||
OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext));
|
||||
|
||||
return result.ReturnObject;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by a Shared (static in C#) method in the business class to cause
|
||||
/// immediate deletion of a specific object from the database.
|
||||
/// </summary>
|
||||
/// <param name="criteria">Object-specific criteria.</param>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.DataPortalException.#ctor(System.String,System.Exception,System.Object)")]
|
||||
public static void Delete(object criteria)
|
||||
{
|
||||
Server.DataPortalResult result;
|
||||
|
||||
MethodInfo method = MethodCaller.GetMethod(
|
||||
MethodCaller.GetObjectType(criteria), "DataPortal_Delete", criteria);
|
||||
|
||||
DataPortalClient.IDataPortalProxy proxy;
|
||||
proxy = GetDataPortalProxy(RunLocal(method));
|
||||
|
||||
Server.DataPortalContext dpContext = new Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote);
|
||||
|
||||
OnDataPortalInvoke(new DataPortalEventArgs(dpContext));
|
||||
|
||||
try
|
||||
{
|
||||
result = proxy.Delete(criteria, dpContext);
|
||||
}
|
||||
catch (Server.DataPortalException ex)
|
||||
{
|
||||
result = ex.Result;
|
||||
if (proxy.IsServerRemote)
|
||||
ApplicationContext.SetGlobalContext(result.GlobalContext);
|
||||
throw new DataPortalException(
|
||||
String.Format("DataPortal.Delete {0} ({1})", Resources.Failed, ex.InnerException.InnerException),
|
||||
ex.InnerException, result.ReturnObject);
|
||||
}
|
||||
|
||||
if (proxy.IsServerRemote)
|
||||
ApplicationContext.SetGlobalContext(result.GlobalContext);
|
||||
|
||||
OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DataPortal Proxy
|
||||
|
||||
private static DataPortalClient.IDataPortalProxy _localPortal;
|
||||
private static DataPortalClient.IDataPortalProxy _portal;
|
||||
|
||||
private static DataPortalClient.IDataPortalProxy GetDataPortalProxy(bool forceLocal)
|
||||
{
|
||||
if (forceLocal)
|
||||
{
|
||||
if (_localPortal == null)
|
||||
_localPortal = new DataPortalClient.LocalProxy();
|
||||
return _localPortal;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_portal == null)
|
||||
{
|
||||
string proxyTypeName = ApplicationContext.DataPortalProxy;
|
||||
if (proxyTypeName == "Local")
|
||||
_portal = new DataPortalClient.LocalProxy();
|
||||
else
|
||||
{
|
||||
string typeName =
|
||||
proxyTypeName.Substring(0, proxyTypeName.IndexOf(",")).Trim();
|
||||
string assemblyName =
|
||||
proxyTypeName.Substring(proxyTypeName.IndexOf(",") + 1).Trim();
|
||||
_portal = (DataPortalClient.IDataPortalProxy)
|
||||
Activator.CreateInstance(assemblyName, typeName).Unwrap();
|
||||
}
|
||||
}
|
||||
return _portal;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Security
|
||||
|
||||
private static System.Security.Principal.IPrincipal GetPrincipal()
|
||||
{
|
||||
if (ApplicationContext.AuthenticationType == "Windows")
|
||||
{
|
||||
// Windows integrated security
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// we assume using the CSLA framework security
|
||||
return ApplicationContext.User;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper methods
|
||||
|
||||
private static bool RunLocal(MethodInfo method)
|
||||
{
|
||||
return Attribute.IsDefined(method, typeof(RunLocalAttribute), false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
|
||||
namespace Csla.DataPortalClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements a data portal proxy to relay data portal
|
||||
/// calls to an application server hosted in COM+.
|
||||
/// </summary>
|
||||
public abstract class EnterpriseServicesProxy : DataPortalClient.IDataPortalProxy
|
||||
{
|
||||
/// <summary>
|
||||
/// Override this method to return a reference to
|
||||
/// the server-side COM+ (ServicedComponent) object
|
||||
/// implementing the data portal server functionality.
|
||||
/// </summary>
|
||||
protected abstract Server.Hosts.EnterpriseServicesPortal GetServerObject();
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to create a
|
||||
/// new business object.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of business object to create.</param>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to load an
|
||||
/// existing business object.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of business object to retrieve.</param>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to update a
|
||||
/// business object.
|
||||
/// </summary>
|
||||
/// <param name="obj">The business object to update.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to delete a
|
||||
/// business object.
|
||||
/// </summary>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public virtual bool IsServerRemote
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
|
||||
namespace Csla.DataPortalClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface implemented by client-side
|
||||
/// data portal proxy objects.
|
||||
/// </summary>
|
||||
public interface IDataPortalProxy : Server.IDataPortalServer
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
bool IsServerRemote { get;}
|
||||
}
|
||||
}
|
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using Csla.Server;
|
||||
|
||||
namespace Csla.DataPortalClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements a data portal proxy to relay data portal
|
||||
/// calls to an application server hosted locally
|
||||
/// in the client process and AppDomain.
|
||||
/// </summary>
|
||||
public class LocalProxy : DataPortalClient.IDataPortalProxy
|
||||
{
|
||||
private Server.IDataPortalServer _portal =
|
||||
new Server.DataPortal();
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to create a
|
||||
/// new business object.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of business object to create.</param>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public DataPortalResult Create(
|
||||
Type objectType, object criteria, DataPortalContext context)
|
||||
{
|
||||
return _portal.Create(objectType, criteria, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to load an
|
||||
/// existing business object.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of business object to retrieve.</param>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public DataPortalResult Fetch(Type objectType, object criteria, DataPortalContext context)
|
||||
{
|
||||
return _portal.Fetch(objectType, criteria, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to update a
|
||||
/// business object.
|
||||
/// </summary>
|
||||
/// <param name="obj">The business object to update.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public DataPortalResult Update(object obj, DataPortalContext context)
|
||||
{
|
||||
return _portal.Update(obj, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to delete a
|
||||
/// business object.
|
||||
/// </summary>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public DataPortalResult Delete(object criteria, DataPortalContext context)
|
||||
{
|
||||
return _portal.Delete(criteria, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public bool IsServerRemote
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,150 @@
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Collections;
|
||||
using System.Threading;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Remoting;
|
||||
using System.Runtime.Remoting.Channels;
|
||||
using System.Runtime.Remoting.Channels.Http;
|
||||
|
||||
namespace Csla.DataPortalClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements a data portal proxy to relay data portal
|
||||
/// calls to a remote application server by using the
|
||||
/// .NET Remoting technology.
|
||||
/// </summary>
|
||||
public class RemotingProxy : DataPortalClient.IDataPortalProxy
|
||||
{
|
||||
|
||||
#region Configure Remoting
|
||||
|
||||
/// <summary>
|
||||
/// Configure .NET Remoting to use a binary
|
||||
/// serialization technology even when using
|
||||
/// the HTTP channel. Also ensures that the
|
||||
/// user's Windows credentials are passed to
|
||||
/// the server appropriately.
|
||||
/// </summary>
|
||||
static RemotingProxy()
|
||||
{
|
||||
// create and register a custom HTTP channel
|
||||
// that uses the binary formatter
|
||||
Hashtable properties = new Hashtable();
|
||||
properties["name"] = "HttpBinary";
|
||||
|
||||
if (ApplicationContext.AuthenticationType == "Windows" || AlwaysImpersonate)
|
||||
{
|
||||
// make sure we pass the user's Windows credentials
|
||||
// to the server
|
||||
properties["useDefaultCredentials"] = true;
|
||||
}
|
||||
|
||||
BinaryClientFormatterSinkProvider
|
||||
formatter = new BinaryClientFormatterSinkProvider();
|
||||
HttpChannel channel = new HttpChannel(properties, formatter, null);
|
||||
ChannelServices.RegisterChannel(channel, EncryptChannel);
|
||||
}
|
||||
|
||||
private static bool AlwaysImpersonate
|
||||
{
|
||||
get
|
||||
{
|
||||
bool result =
|
||||
(ConfigurationManager.AppSettings["AlwaysImpersonate"] == "true");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool EncryptChannel
|
||||
{
|
||||
get
|
||||
{
|
||||
bool encrypt =
|
||||
(ConfigurationManager.AppSettings["CslaEncryptRemoting"] == "true");
|
||||
return encrypt;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private Server.IDataPortalServer _portal;
|
||||
|
||||
private Server.IDataPortalServer Portal
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_portal == null)
|
||||
_portal = (Server.IDataPortalServer)Activator.GetObject(
|
||||
typeof(Server.Hosts.RemotingPortal),
|
||||
ApplicationContext.DataPortalUrl.ToString());
|
||||
return _portal;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to create a
|
||||
/// new business object.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of business object to create.</param>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public Server.DataPortalResult Create(
|
||||
Type objectType, object criteria, Server.DataPortalContext context)
|
||||
{
|
||||
return Portal.Create(objectType, criteria, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to load an
|
||||
/// existing business object.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of business object to retrieve.</param>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public Server.DataPortalResult Fetch(Type objectType, object criteria, Server.DataPortalContext context)
|
||||
{
|
||||
return Portal.Fetch(objectType, criteria, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to update a
|
||||
/// business object.
|
||||
/// </summary>
|
||||
/// <param name="obj">The business object to update.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public Server.DataPortalResult Update(object obj, Server.DataPortalContext context)
|
||||
{
|
||||
return Portal.Update(obj, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to delete a
|
||||
/// business object.
|
||||
/// </summary>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public Server.DataPortalResult Delete(object criteria, Server.DataPortalContext context)
|
||||
{
|
||||
return Portal.Delete(criteria, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public bool IsServerRemote
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,177 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using Csla.WebServiceHost;
|
||||
|
||||
namespace Csla.DataPortalClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements a data portal proxy to relay data portal
|
||||
/// calls to a remote application server by using
|
||||
/// Web services.
|
||||
/// </summary>
|
||||
public class WebServicesProxy : DataPortalClient.IDataPortalProxy
|
||||
{
|
||||
private WebServiceHost.WebServicePortal GetPortal()
|
||||
{
|
||||
WebServiceHost.WebServicePortal wsvc =
|
||||
new WebServiceHost.WebServicePortal();
|
||||
wsvc.Url = ApplicationContext.DataPortalUrl.ToString();
|
||||
return wsvc;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to create a
|
||||
/// new business object.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of business object to create.</param>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public Server.DataPortalResult Create(
|
||||
Type objectType, object criteria, Server.DataPortalContext context)
|
||||
{
|
||||
object result;
|
||||
Csla.Server.Hosts.WebServicePortal.CreateRequest
|
||||
request = new Csla.Server.Hosts.WebServicePortal.CreateRequest();
|
||||
request.ObjectType = objectType;
|
||||
request.Criteria = criteria;
|
||||
request.Context = context;
|
||||
|
||||
using (WebServiceHost.WebServicePortal wsvc = GetPortal())
|
||||
{
|
||||
byte[] rd = Serialize(request);
|
||||
byte[] rp = wsvc.Create(rd);
|
||||
result = Deserialize(rp);
|
||||
}
|
||||
|
||||
if (result is Exception)
|
||||
throw (Exception)result;
|
||||
return (Server.DataPortalResult)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to load an
|
||||
/// existing business object.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of business object to retrieve.</param>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public Server.DataPortalResult Fetch(
|
||||
Type objectType, object criteria, Server.DataPortalContext context)
|
||||
{
|
||||
object result;
|
||||
Server.Hosts.WebServicePortal.FetchRequest request =
|
||||
new Server.Hosts.WebServicePortal.FetchRequest();
|
||||
request.ObjectType = objectType;
|
||||
request.Criteria = criteria;
|
||||
request.Context = context;
|
||||
|
||||
using (WebServiceHost.WebServicePortal wsvc = GetPortal())
|
||||
{
|
||||
result = Deserialize(wsvc.Fetch(Serialize(request)));
|
||||
}
|
||||
|
||||
if (result is Exception)
|
||||
throw (Exception)result;
|
||||
return (Server.DataPortalResult)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to update a
|
||||
/// business object.
|
||||
/// </summary>
|
||||
/// <param name="obj">The business object to update.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public Server.DataPortalResult Update(object obj, Server.DataPortalContext context)
|
||||
{
|
||||
object result;
|
||||
Server.Hosts.WebServicePortal.UpdateRequest request = new Server.Hosts.WebServicePortal.UpdateRequest();
|
||||
request.Object = obj;
|
||||
request.Context = context;
|
||||
|
||||
using (WebServiceHost.WebServicePortal wsvc = GetPortal())
|
||||
{
|
||||
result = Deserialize(wsvc.Update(Serialize(request)));
|
||||
}
|
||||
|
||||
if (result is Exception)
|
||||
throw (Exception)result;
|
||||
return (Server.DataPortalResult)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by <see cref="DataPortal" /> to delete a
|
||||
/// business object.
|
||||
/// </summary>
|
||||
/// <param name="criteria">Criteria object describing business object.</param>
|
||||
/// <param name="context">
|
||||
/// <see cref="Server.DataPortalContext" /> object passed to the server.
|
||||
/// </param>
|
||||
public Server.DataPortalResult Delete(object criteria, Server.DataPortalContext context)
|
||||
{
|
||||
object result;
|
||||
Server.Hosts.WebServicePortal.DeleteRequest request = new Server.Hosts.WebServicePortal.DeleteRequest();
|
||||
request.Criteria = criteria;
|
||||
request.Context = context;
|
||||
|
||||
using (WebServiceHost.WebServicePortal wsvc = GetPortal())
|
||||
{
|
||||
result = Deserialize(wsvc.Delete(Serialize(request)));
|
||||
}
|
||||
|
||||
if (result is Exception)
|
||||
throw (Exception)result;
|
||||
return (Server.DataPortalResult)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public bool IsServerRemote
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
|
||||
#region Helper functions
|
||||
|
||||
private static byte[] Serialize(object obj)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
using (MemoryStream buffer = new MemoryStream())
|
||||
{
|
||||
BinaryFormatter formatter = new BinaryFormatter();
|
||||
formatter.Serialize(buffer, obj);
|
||||
return buffer.ToArray();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static object Deserialize(byte[] obj)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
using (MemoryStream buffer = new MemoryStream(obj))
|
||||
{
|
||||
BinaryFormatter formatter = new BinaryFormatter();
|
||||
return formatter.Deserialize(buffer);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user