views:

81

answers:

1

I am working on a program where I want to be able to add functionality using DI, C#, so I will create an example to see if this is possible.

I have a program that a user enters any two numbers. The program then looks at all the plugins in an xml file and uses those classes that are injected to calculate with these two numbers.

So, I create two classes, Add and Subtract. I put them in and run my program.

Ideally, I would like to have the program find these two classes and show the results after going through the equations.

Now, someone else writes "Multiply", drops it into a directory, updates the xml file, and next time I run my program, with two numbers, there are three results.

Now, I don't know how many plugins there will be.

Is there any way in C#, using DI, that I can get this type of functionality?

I don't know if it is possible.

I am trying to figure this out as I want to show a way to graph some data, using different types of graphs, but I think using the binary equation approach makes explaining simpler, I think.

I could do this by manually loading the class referenced in the xml file, so it can be done, but, I would like to have a dependency framework take care of the heavy work for me.

Update: I am hoping to use the Unity framework for this.

+2  A: 

You can expose an interface (IMathOperation for example, with a single method such as Compute) and then specify types implementing the interface (Add, Subtract etc) in the DI framework config file. DI frameworks generally allow you to specify the assembly name as well as the type name, and they're also usually good at allowing you to create collections of similar types, so you could have one type which has a property or constructor parameter of type IList<IMathOperation>. Set that property/parameter in the config file, and you should be good to go.

The exact details will depend on the DI framework you want to use, of course.

Jon Skeet