Skip to content

Instantly share code, notes, and snippets.

@kberridge
Created February 5, 2012 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kberridge/1742463 to your computer and use it in GitHub Desktop.
Save kberridge/1742463 to your computer and use it in GitHub Desktop.
The Cost of DI: 4 different versions of the same code, demonstrating the cost of added indirection
public class SpeakerController : Controller
{
public ActionResult Create(int presentationId, FormCollection data)
{
var presentation = Presentation.Find(presentationId);
var vm = SpeakerNewViewModel.Build(presentation);
UpdateModel(vm, data);
var presentationApi = new PresentationApi(presentationId);
presentationApi.AddSpeaker(vm.Speaker);
return View();
}
}
public class PresentationApi
{
int presentationId;
PresentationSpeakers presentationSpeakers;
OrdersSpeakers ordersSpeakers;
public PresentationApi(int presentationId)
{
this.presentationId = presentationId;
ordersSpeakers = new OrdersSpeakers(presentationId);
}
public void AddSpeaker(Speaker speaker)
{
LoadPresentationSpeakersRecord();
speaker.PresentationId = presentationId;
presentationSpeakers.AddSpeaker(speaker);
ordersSpeakers.AddSpeaker(speaker);
speaker.Save();
presentationSpeakers.Save();
}
public void RemoveSpeaker(Speaker speaker)
{
// ... very similar to AddSpeaker ...
}
void LoadPresentationSpeakersRecord()
{
presentationSpeakers = PresentationSpeakers.FindByPresentationId(presentationId);
}
}
public class Modules
{
public void Register(IContainer container)
{
container.Register<IPresentationApi>.To<PresentationApi>();
}
}
public class SpeakerController : Controller
{
IPresentationApi presentationApi;
public SpeakerController(IPresentationApi presentationApi)
{
this.presentationApi = presentationApi;
}
public ActionResult Create(int presentationId, FormCollection data)
{
var presentation = Presentation.Find(presentationId);
var vm = SpeakerNewViewModel.Build(presentation);
UpdateModel(vm, data);
presentationApi.AddSpeaker(presentationId, vm.Speaker);
return View();
}
}
public interface IPresentationApi
{
void AddSpeaker(int presentationId, Speaker speaker);
void RemoveSpeaker(int presentationId, Speaker speaker);
}
public class PresentationApi : IPresentationApi
{
public void AddSpeaker(int presentationId, Speaker speaker)
{
var presentationSpeakers = LoadPresentationSpeakersRecord(presentationId);
speaker.PresentationId = presentationId;
presentationSpeakers.AddSpeaker(speaker);
var ordersSpeakers = new OrdersSpeakers(presentationId);
ordersSpeakers.AddSpeaker(speaker);
speaker.Save();
presentationSpeakers.Save();
}
public void RemoveSpeaker(int presentationId, Speaker speaker)
{
// ... very similar to AddSpeaker ...
}
PresentationSpeakers LoadPresentationSpeakersRecord(int presentationId)
{
return PresentationSpeakers.FindByPresentationId(presentationId);
}
}
public class Modules
{
public void Register(IContainer container)
{
container.Register<IPresentationApi>.To<PresentationApi>();
container.Register<IOrdersSpeaker>.To<OrdersSpeaker>();
}
}
public class SpeakerController : Controller
{
IPresentationApi presentationApi;
public SpeakerController(IPresentationApi presentationApi)
{
this.presentationApi = presentationApi;
}
public ActionResult Create(int presentationId, FormCollection data)
{
var presentation = Presentation.Find(presentationId);
var vm = SpeakerNewViewModel.Build(presentation);
UpdateModel(vm, data);
presentationApi.AddSpeaker(presentationId, vm.Speaker);
return View();
}
}
public interface IPresentationApi
{
void AddSpeaker(int presentationId, Speaker speaker);
void RemoveSpeaker(int presentationId, Speaker speaker);
}
public interface IOrdersSpeakers
{
void AddSpeaker(int presentationId, Speaker speaker);
void RemoveSpeaker(int presentationId, Speaker speaker);
}
public class PresentationApi : IPresentationApi
{
IOrdersSpeakers ordersSpeakers;
public PresentationApi(IOrdersSpeakers ordersSpeakers)
{
this.ordersSpeakers = ordersSpeakers;
}
public void AddSpeaker(int presentationId, Speaker speaker)
{
var presentationSpeakers = LoadPresentationSpeakersRecord(presentationId);
speaker.PresentationId = presentationId;
presentationSpeakers.AddSpeaker(speaker);
ordersSpeakers.AddSpeaker(presentationId, speaker);
speaker.Save();
presentationSpeakers.Save();
}
public void RemoveSpeaker(int presentationId, Speaker speaker)
{
// ... very similar to AddSpeaker ...
}
PresentationSpeakers LoadPresentationSpeakersRecord(int presentationId)
{
return PresentationSpeakers.FindByPresentationId(presentationId);
}
}
public class Modules
{
public void Register(IContainer container)
{
container.Register<IPresentationApi>.To<PresentationApi>();
container.Register<IOrdersSpeaker>.To<OrdersSpeaker>();
container.Register<IPresentationRepository>.To(...);
container.Register<IPresentationSpeakersRepository>.To(...);
}
}
public interface IPresentationRepository
{
...
}
public interface ISpeakerRepository
{
...
}
public interface IPresentationSpeakersRepository
{
...
}
public class SpeakerController : Controller
{
IPresentationApi presentationApi;
IPresentationRepository presentationRepo;
public SpeakerController(
IPresentationApi presentationApi,
IPresentationRepository presentationRepo)
{
this.presentationApi = presentationApi;
this.presentationRepo = presentationRepo;
}
public ActionResult Create(int presentationId, FormCollection data)
{
var presentation = presentationRepo.Find(presentationid);
var vm = SpeakerNewViewModel.Build(presentation);
UpdateModel(vm, data);
presentationApi.AddSpeaker(presentationId, vm.Speaker);
return View();
}
}
public interface IPresentationApi
{
void AddSpeaker(int presentationId, Speaker speaker);
void RemoveSpeaker(int presentationId, Speaker speaker);
}
public interface IOrdersSpeakers
{
void AddSpeaker(int presentationId, Speaker speaker);
void RemoveSpeaker(int presentationId, Speaker speaker);
}
public class PresentationApi : IPresentationApi
{
IOrdersSpeakers ordersSpeakers;
ISpeakerRepository speakerRepo;
IPresentationSpeakersRepository presentationSpeakerRepo;
public PresentationApi(
IOrdersSpeakers ordersSpeakers,
ISpeakerRepository speakerRepo,
IPresentationSpeakersRepository presentationSpeakersRepo)
{
this.ordersSpeakers = ordersSpeakers;
this.speakerRepo = speakerRepo;
this.presentationSpeakerRepo = presentationSpeakerRepo;
}
public void AddSpeaker(int presentationId, Speaker speaker)
{
var presentationSpeakers = LoadPresentationSpeakersRecord(presentationId);
speaker.PresentationId = presentationId;
presentationSpeakers.AddSpeaker(speaker);
ordersSpeakers.AddSpeaker(presentationId, speaker);
speakerRepo.Save(speaker);
presentationSpeakerRepo.Save(presentationSpeakers);
}
public void RemoveSpeaker(int presentationId, Speaker speaker)
{
// ... very similar to AddSpeaker ...
}
PresentationSpeakers LoadPresentationSpeakersRecord(int presentationId)
{
presentationSpeakerRepo.FindByPresentationId(presentationId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment