5

Think at this scenario:

I have a c# windows form application. This application was the same for all my customers. Now one of them needs to modify a form adding new textbox and new logic.

I obviously don't wanto to duplicate my application, and inserting IF statements with customer-Id to control the logic can easly drive to a spaghetti-style code.

I think that in this situation I can create a separate dll project for each customer; Inside I can write custom forms implements same interface as default form (and same for logic classes) and I can try to switch those dll via configuration file or build the project with the right customer dll (or using, for example, Windsor Castle for DI).

Is this a valid pattern? Exists a different way?

update

I try to list:

danyolgiax
  • 12,798
  • 10
  • 65
  • 116
  • 1
    See this answer on [MEF vs IOC/DI](http://stackoverflow.com/questions/108116/mef-managed-extensibility-framework-vs-ioc-di). I think MEF or DI will work for you. Personally, I would use Autofac, but I am biased. – bentayloruk Jun 11 '11 at 20:25

2 Answers2

3

I think in this case, MEF would be a better choice. Castle is more like a DI engine for business logic, useful for controlling the object life cycle, especially when you want to be able to switch the way the program works (multiple small threads or one single large operation in one thread).
MEF, on the other hand, strips you of the need to add a config file for this type of configuration. You just operate with libraries. I think MEF is best for client-side GUI forms.

Alex
  • 14,338
  • 5
  • 41
  • 59
  • Castle Windsor enables DI. However, this is not limited to business logic and can be done without configuration. The same is true for Autofac. Therefore, your reason for recommending MEF in this answer appears misleading. Also, I do not understand what you mean when you say "multiple small threads or one single large operation in one thread". Can you expand on this? – bentayloruk Jun 11 '11 at 20:17
  • I did not write it is limited to BL, it's just that I find it to be more suitable. And you can either have the DI hardcoded, or in a configuration file (I think that a configuration file is more flexible). With MEF, you achieve something else - you can control what must be loaded, bu just providing the appropriate libraries, without modifying any file. Sort of Plugins. There are a lot of cases where you would need this functionality in a GUI, and mostly suitable for BLL when it's part of the whole client application. – Alex Jun 11 '11 at 22:44
  • As for threads... I did have a situation where parallelizing a process in the BLL caused some exceptions, which I din't have time to solve. So I wrote 2 implementations of the same functionality, and had no problems changing the component lifestyle in the application config file. – Alex Jun 11 '11 at 22:48
  • You do say "Castle is more like a DI engine for *business logic*", hence my comment. Also, you can achieve non-config, non-modification plugins with containers like Autofac e.g. [Autofac scanning](http://code.google.com/p/autofac/wiki/Scanning). All of this is probably overkill for this question, unless *a lot* of customisation is being made. – bentayloruk Jun 12 '11 at 10:15
  • I did not mention Autofac, because, frankly, I never got my hands on it. Maybe it is better in some ways, but for Castle and MEF I found it best to implement in these situations. And for the GUI, I actually found MEF to be the most recommended tool for plugins, when I did my research. Will look into Autofac though. – Alex Jun 12 '11 at 10:31
  • 1
    Just to be clear, I am not arguing that MEF is the *wrong* answer. I am just pointing out that the reasons you cite for preferring MEF are flawed in my opinion and could be misleading. Also, [Windsor supports scanning functionality](http://docs.castleproject.org/Windsor.Registering-components-by-conventions.ashx) similar to Autofac. – bentayloruk Jun 12 '11 at 15:17
1

If you are developing a multitenant application, there are DI frameworks like Autofac which support this kind of customization. Take a look at this article

You can also use your Source Control system to help you out. When you need to customize, create a branch and do the customization there so you do not have to duplicate your code.

Eranga
  • 32,181
  • 5
  • 97
  • 96
  • I see Windsor supports multitenant too! Are you suggesting me Autofac however? – danyolgiax Jun 11 '11 at 10:01
  • No, he is suggesting you to use an DI container. Any container will work, but Autofac has useful documentation about developing a multitenant application. – Steven Jun 11 '11 at 11:22