Applying Unity in dynamic menu

Posted by Rajarshi on Stack Overflow See other posts from Stack Overflow or by Rajarshi
Published on 2010-04-25T12:04:35Z Indexed on 2010/04/25 12:13 UTC
Read the original article Hit count: 224

Filed under:

I was going through Unity 2.0 to check if it has an effective use in our new application. My application is a Windows Forms application and uses a traditional bar menu (at the top), currently.

My UIs (Windows Forms) more or less support Dependency Injection pattern since they all work with a class (Presentation Model Class) supplied to them via the constructor. The form then binds to the properties of the supplied P Model class and calls methods on the P Model class to perform its duties. Pretty simple and straightforward.

How P Model reacts to the UI actions and responds to them by co-ordinating with the Domain Class (Business Logic/Model) is irrelevant here and thus not mentioned.

The object creation sequence to show up one UI from menu then goes like this -

  1. Create Business Model instance
  2. Create Presentation Model instance with Business Model instance passed to P Model constructor.
  3. Create UI instance with Presentation Model instance passed to UI constructor.

My present solution:

To show an UI in the method above from my menu I would have to refer all assemblies (Business, PModel, UI) from my Menu class. Considering I have split the modules into a number of physical assemblies, that would be a dificult task to add references to about 60 different assemblies. Also the approach is not very scalable since I would certainly need to release more modules and with this approach I would have to change the source code every time I release a new module.

So primarily to avoid the reference of so many assemblies from my Menu class (assembly) I did as below -

Stored all the dependency described above in a database table (SQL Server), e.g.

ModuleShortCode | BModelAssembly | BModelFullTypeName | PModelAssembly | PModelFullTypeName | UIAssembly | UIFullTypeName

Now used a static class named "Launcher" with a method "Launch" as below -

Launcher.Launch("Discount")

Launcher.Launch("Customers")

The Launcher internally uses data from the dependency table and uses Activator.CreateInstance() to create each of the objects and uses the instance as constructor parameter to the next object being created, till the UI is built. The UI is then shown as a modal dialog. The code inside Launcher is somewhat like -

Form frm = ResolveForm("Discount");

frm.ShowDialog();

The ResolveForm does the trick of building the chain of objects.

Can Unity help me here?

Now when I did that I did not have enough information on Unity and now that I have studied Unity I think I have been doing more or less the same thing. So I tried to replace my code with Unity.

However, as soon as I started I hit a block. If I try to resolve UI forms in my Menu as

Form customers = myUnityContainer.Resolve();

or

Form customers = myUnityContainer.Resolve(typeof(Customers));

Then either way, I need to refer to my UI assembly from my Menu assembly since the target Type "Customers" need to be known for Unity to resolve it. So I am back to same place since I would have to refer all UI assemblies from the Menu assembly. I understand that with Unity I would have to refer fewer assemblies (only UI assemblies) but those references are needed which defeats my objectives below -

  1. Create the chain of objects dynamically without any assembly reference from Menu assembly. This is to avoid Menu source code changing every time I release a new module. My Menu also is built dynamically from a table.

  2. Be able to supply new modules just by supplying the new assemblies and inserting the new Dependency row in the table by a database patch.

At this stage, I have a feeling that I have to do it the way I was doing, i.e. Activator.CreateInstance() to fulfil all my objectives. I need to verify whether the community thinks the same way as me or have a better suggestion to solve the problem.

The post is really long and I sincerely thank you if you come til this point. Waiting for your valuable suggestions.

  • Rajarshi

© Stack Overflow or respective owner

Related posts about unity