Looking equivalent class to HttpContext in Win app

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi
I am looking for a class that will give me the same functionality as the HttpContext
that i can use in a Windows app

Thanks Ron
 
This is from another post of mine here in the NewsGroups and thought it answered your question as well

In a Windows application context information can be stored in the AppDomain.CurrentDomain which inherits from System.MarshalByRefObject, which is completely different object hierarchy for the web world. So in order to have your application work accross both platforms you must create an abstract layer for your context and your application will interact with the abstract/interface object. To accomplish this you will need a factory object that determines what environment you are in and call the appropriate class to return the abstract context. Here is a code example in C#, error handling and comments were not included in order to keep the size of the post small. All you need to do is implement the concrete class for dealing with the web environment. Constructive critisism is welcome

Good Luck -- Joh

using System
namespace iGoBusinessManager.Runtim

/// <summary
/// Summary description for ContextFactory that is used to determine what type of context I will be dealing with
/// This is a static call and will be used in the application as the following: ContextFactory.GetContext(
/// You can set this up as a singleton if you like
/// </summary
public class ContextFactor

public ContextFactory(

/
// TODO: Add constructor logic her
/


public static IApplicationContext GetContext()

// You would normally have code to dermine the what context I am in and create the appropriate object
// In this example I am just creating the WindowsContext class and returning it as IApplicationContext
return (IApplicationContext)Activator.CreateInstance(Type.GetType("WindowsContext"))




using System

namespace iGoBusinessManager.Runtim

/// <summary
/// Summary description for IApplicationContext
/// </summary
public interface IApplicationContex

object GetObject(string objectName)
bool SetObject(string objectName, object myObject)



using System

namespace iGoBusinessManager.Runtim

/// <summary
/// Summary description for WindowsContext
/// </summary
public class WindowsContext : IApplicationContex

public WindowsContext(

/
// TODO: Add constructor logic her
/


public object GetObject(string objectName)

return AppDomain.CurrentDomain.GetData(objectName)


public bool SetObject(string objectName, object myObject)


try

AppDomain.CurrentDomain.SetData(objectName, objectName)
return true

catch (Exception ex

//do something with the exceptio

//then return fals
return false





using System
using iGoBusinessManager.EntityObjects.Entities

namespace iGoBusinessManager.Runtim

/// <summary
/// Summary description for Sample
/// </summary
public class Sampl



public Sample(

/
// TODO: Add constructor logic her
/


public Customer _CurrentCustomer

get

IApplicationContext myContext = ContextFactory.GetContext()
return (Customer)myContext.GetObject("Customer")


set

IApplicationContext myContext = ContextFactory.GetContext()
myContext.SetObject("Customer", value)
 
Back
Top