Rush Frisby
Rush Frisby

Reputation: 11454

Generic context

I have a class that will be used in multiple types of apps: web, wcf service, windows app. In the web app I want to use HttpContext in wcf the OperationContext and in Windows... a (I don't even know maybe an IDictionary?) How can this be done so that in my class I am just accessing a generic thing to pull data out of but use the appropriate context for each type of application?

Upvotes: 1

Views: 695

Answers (2)

Hertzel Guinness
Hertzel Guinness

Reputation: 5940

Use Adapter design patten.
A C# example can be found here.

HTH

Upvotes: 1

Richard Blewett
Richard Blewett

Reputation: 6109

Create your own abstraction around the context and then inject a concrete version into the class depending on the environment so for example

interface IContextWrapper
{   
}

class HttpContextWrapper : IContextWrapper
{      
}

class OperationContextWrapper : IContextWrapper
{
}

class UnitTestWrapper : IContextWrapper
{
}

class MyContextUser
{
    private readonly IContextWrapper contextWrapper;

    public MyContextUser(IContextWrapper contextWrapper)
    {
        this.contextWrapper = contextWrapper;
    }
}

As you can see this has the added benefit of making your class unit testable

Upvotes: 1

Related Questions