0

Imagine I have two sites that share basic code. Most of their code matches exactly, but a couple functions don't match. For example, what if these two sites differed in the method they used for determining if the current user was using an acceptably new Web browser. So isGoodBrowser() returns true or false, but the sites have a different version of this function. How can I make this one function interchangeable while keeping the rest of the code the same? I'd like to contain such a function in a file of its own. Thanks.

user1325179
  • 1,535
  • 2
  • 19
  • 29
  • Can you put the shared methods in a separate assembly? – Shai Cohen Apr 25 '12 at 18:52
  • If the code won't be the same and isn't reusable elsewhere, then *don't share it in the first place*. – Andrew Barber Apr 25 '12 at 18:58
  • What if in the constructor of the "Browser" object you have a property set by a variable passed that decides which site this browser belongs to and from there you make your isGoodBrowser() function do one thing or another?. – CoderRoller Apr 25 '12 at 19:11

1 Answers1

1

Make three assemblies:

  1. Assembly containing all the code which is shared.
  2. Assembly containing functionality specific to the 1st web site.
  3. Assembly containing functionality specific to the 2nd web site.

Reference the common assembly from both web site projects.

You can even specify an interface for isGoodBrowser() in the shared assembly and implement it differently in site-specific assemblies. Then inject the appropriate implementation either manually, or via an IoC container, such as Unity.

Dmytro Shevchenko
  • 33,431
  • 6
  • 51
  • 67
  • I'm trying to figure out out to implement this. I've tried making a BasicClass (which calls isGoodBrowser()), an IGoodBrowser interface which states its use of isGoodBrowser(), and a third interchangeable file with a varying definition of isGoodBrowser(). I've tried this setup, but BasicClass can't seem to access isGoodBrowser(). – user1325179 Apr 25 '12 at 20:21
  • I didn't end up using your suggestion, but I wanted to check it as the best answer anyway since it makes sense to me. Thank you. – user1325179 Jul 02 '13 at 04:10