Skip to main content
clarified opening para
Source Link
Stephen Byrne
  • 725
  • 1
  • 5
  • 10

In ASP.Net, Views are still typically rendered on the serverbackend but even so that part of the question doesn't really make sense.

However as to your suggested approach - a Factory - that is indeed an accepted way of doing this.

Template methods don't necessarily come into it either, you can choose to use them or not; the implementations of the classes that have the client-specific code in them might use them as the mechanism to achieve Polymorphism via inheritance and implementation of abstract base class or methods, or you might go with a plainer interface implementation.

Here's an example;example using a more classic Controller->Model->View scenario; note that I haven't shown injection of the dependencies and the factory is really just a switch statement so in real life, you'd use an IOC library, and inject the factory into the Controller, as well as using the container's built-in factory ability. I just wanted to keep things clear in this case.

public interface IClientSpecificFoo
{
    FooDataModel GetTheFoo();
}

Implementations of IClientSpecificFoo are not shown, this is where you might decide to implement them using inheritance or composition, it's really up to you.

public class ClientSpecificFooFactory
{
    public IClientSpecificFoo GetFooProviderFor(string clientId)
    {      
       switch(clientId)
       {
           case "clientA":
              return new ClientAFoo();
              break;
            case "clientB":
              return new ClientBFoo();
                break;
            default:
                return new GenericClientFoo();         
       }
    }
}

public class ClientSpecificController: Controller
{
    public ActionResult SomethingSpecific()
    {       
        var clientId=GetClientIdFromHeaders(); //or whatever way you identify the client
        var factory = new ClientSpecificFooFactory();
        var clientSpecificFoo = factory.GetFooProviderFor(clientId);
        var model = clientspecificFoo.GetTheFoo();
        return View(model);
    }
}

The View itself in all cases would be the same, it just renders the FooDataModel given to it by the Controller.

and that's it really.

In ASP.Net, Views are still rendered on the server so that part of the question doesn't really make sense.

However as to your suggested approach - a Factory - that is indeed an accepted way of doing this.

Template methods don't necessarily come into it either, you can choose to use them or not; the implementations of the classes that have the client-specific code in them might use them as the mechanism to achieve Polymorphism via inheritance and implementation of abstract base class or methods, or you might go with a plainer interface implementation.

Here's an example; note that I haven't shown injection of the dependencies and the factory is really just a switch statement so in real life, you'd use an IOC library, and inject the factory into the Controller, as well as using the container's built-in factory ability. I just wanted to keep things clear in this case.

public interface IClientSpecificFoo
{
    FooDataModel GetTheFoo();
}

Implementations of IClientSpecificFoo are not shown, this is where you might decide to implement them using inheritance or composition, it's really up to you.

public class ClientSpecificFooFactory
{
    public IClientSpecificFoo GetFooProviderFor(string clientId)
    {      
       switch(clientId)
       {
           case "clientA":
              return new ClientAFoo();
              break;
            case "clientB":
              return new ClientBFoo();
                break;
            default:
                return new GenericClientFoo();         
       }
    }
}

public class ClientSpecificController: Controller
{
    public ActionResult SomethingSpecific()
    {       
        var clientId=GetClientIdFromHeaders(); //or whatever way you identify the client
        var factory = new ClientSpecificFooFactory();
        var clientSpecificFoo = factory.GetFooProviderFor(clientId);
        var model = clientspecificFoo.GetTheFoo();
        return View(model);
    }
}

The View itself in all cases would be the same, it just renders the FooDataModel given to it by the Controller.

and that's it really.

In ASP.Net, Views are still typically rendered on the backend but even so your suggested approach - a Factory - that is indeed an accepted way of doing this.

Template methods don't necessarily come into it either, you can choose to use them or not; the implementations of the classes that have the client-specific code in them might use them as the mechanism to achieve Polymorphism via inheritance and implementation of abstract base class or methods, or you might go with a plainer interface implementation.

Here's an example using a more classic Controller->Model->View scenario; note that I haven't shown injection of the dependencies and the factory is really just a switch statement so in real life, you'd use an IOC library, and inject the factory into the Controller, as well as using the container's built-in factory ability. I just wanted to keep things clear in this case.

public interface IClientSpecificFoo
{
    FooDataModel GetTheFoo();
}

Implementations of IClientSpecificFoo are not shown, this is where you might decide to implement them using inheritance or composition, it's really up to you.

public class ClientSpecificFooFactory
{
    public IClientSpecificFoo GetFooProviderFor(string clientId)
    {      
       switch(clientId)
       {
           case "clientA":
              return new ClientAFoo();
              break;
            case "clientB":
              return new ClientBFoo();
                break;
            default:
                return new GenericClientFoo();         
       }
    }
}

public class ClientSpecificController: Controller
{
    public ActionResult SomethingSpecific()
    {       
        var clientId=GetClientIdFromHeaders(); //or whatever way you identify the client
        var factory = new ClientSpecificFooFactory();
        var clientSpecificFoo = factory.GetFooProviderFor(clientId);
        var model = clientspecificFoo.GetTheFoo();
        return View(model);
    }
}

The View itself in all cases would be the same, it just renders the FooDataModel given to it by the Controller.

and that's it really.

Source Link
Stephen Byrne
  • 725
  • 1
  • 5
  • 10

In ASP.Net, Views are still rendered on the server so that part of the question doesn't really make sense.

However as to your suggested approach - a Factory - that is indeed an accepted way of doing this.

Template methods don't necessarily come into it either, you can choose to use them or not; the implementations of the classes that have the client-specific code in them might use them as the mechanism to achieve Polymorphism via inheritance and implementation of abstract base class or methods, or you might go with a plainer interface implementation.

Here's an example; note that I haven't shown injection of the dependencies and the factory is really just a switch statement so in real life, you'd use an IOC library, and inject the factory into the Controller, as well as using the container's built-in factory ability. I just wanted to keep things clear in this case.

public interface IClientSpecificFoo
{
    FooDataModel GetTheFoo();
}

Implementations of IClientSpecificFoo are not shown, this is where you might decide to implement them using inheritance or composition, it's really up to you.

public class ClientSpecificFooFactory
{
    public IClientSpecificFoo GetFooProviderFor(string clientId)
    {      
       switch(clientId)
       {
           case "clientA":
              return new ClientAFoo();
              break;
            case "clientB":
              return new ClientBFoo();
                break;
            default:
                return new GenericClientFoo();         
       }
    }
}

public class ClientSpecificController: Controller
{
    public ActionResult SomethingSpecific()
    {       
        var clientId=GetClientIdFromHeaders(); //or whatever way you identify the client
        var factory = new ClientSpecificFooFactory();
        var clientSpecificFoo = factory.GetFooProviderFor(clientId);
        var model = clientspecificFoo.GetTheFoo();
        return View(model);
    }
}

The View itself in all cases would be the same, it just renders the FooDataModel given to it by the Controller.

and that's it really.