Friday, April 17, 2009

sharing content between different sites

Constantly we are facing some issues when sharing content of site A with site B. Normally the content to be shared is header/footer, and they are user controls at site A. On the other hand, site B wants an easy yet reliable way to access the content and automatically updated content without changing anything on its own side. During past years of building application to solve this problem, I think these two designs are both good solutions:

Design A

  • site A wrap the user control into web service
  • site B call web service to get html content
  • site B implement both memory cache and cache file to fail back on in case web service call failed. when failing, it trys to load from memory cache first, if there is no content in memory cache, then load it from cache file. when next time a web service call is successful, update memory cache and cache file

pro of Design A:

  • very reliable as both memory cache and cache file are available for fall back

cons if Design A:

  • site A need to be careful in implementing user control, can't assume some page information like session is available is httprequest
  • complex logic in site B to implement fall back workflow

Design B

  • Site A wrap user control in a regular aspx page, using javascript document.write to emit content
  • Site B consume content by reference to this page as a javascript source

pro of design B:

  • easy to implemet on both sides

cons of design B

  • no fallback logic, when site A is done, it screw up site B as well

Both wraping solution can't solve the issue if the content need to post back to itself after submit. For example, if a user control has some code behind logic after submit, it won't be available in both wrapped version.

Source code:

Design A at Site A:

                    HeaderControl hc = (HeaderControl)p.LoadControl("~/UserControls/" + ControlName);
hc.IsSecure = IsSecure;
hc.IsOLSMenu = true;
hc.Page_Load(null, null); //call page_load to load mennu
hc.RenderControl(htmlWriter);
Design B at site A:
                    StringWriter sw = new StringWriter();
Page p = new Page();
HtmlForm f = new HtmlForm();
p.Controls.Add(f);
Header h = (Header)p.LoadControl("~/controls/header.ascx");
p.Controls[0].Controls.Add(h);
Server.Execute(p, sw, false);


Design B at site B:


<script language="javascript" src="http://ta-homecorporate.progressive.com/scripts/headerwrapperjs.aspx"></script>

No comments: