开发者

Where do I place my Unity Configuration File?

开发者 https://www.devze.com 2023-02-01 10:48 出处:网络
In a 3 layered windows application, where will I place my config file which will contain unity configuration?

In a 3 layered windows application, where will I place my config file which will contain unity configuration? If it's in front layer then do I need to reference unity.dlls in all of my front, business and data layer projects for the projects to recognise I开发者_StackOverflow中文版UnityContainer interfaces etc?

Where will I load the configuration container? Will it be in the front layer's Main method or Data Layer or I will load the container as and when needed?

If everything is placed in front layer then isn't the whole project dependent on front layer and if I change the front layer from Windows App to Web app then I have to do the unity work again!


Unity, or any other IoC container for that matter, should never play in a role in your application beyond the initial bootstrapping of the application. And it emphatically should not go into the business and data layers.

Therefore, you bootstrap your application in the Main using Unity and after that Unity is done. Do not let Unity touch any other part of your application beyond that.

Here's an elaboration on this topic: How I use Inversion of Control containers and the follow up Pulling from the container.

If everything is placed in front layer then isn't the whole project dependent on front layer and if I change the front layer from Windows App to Web app then I have to do the unity work again!

Not if you've set things up properly. You don't need Unity to resolve the components for your application. Unity, and other IoC containers, are just very useful tools for solving that problem. You should first write your application to not need Unity. Then you can easily plug in Unity, or some other IoC container, to resolve the components that your application needs to run.

The three Rs of dependency injection are

  1. Register
  2. Resolve
  3. Release

Register is when you setup the container. This happens when your application starts in Main. Resolve is when you resolve the root components for your application. This is what gets the core components of your application working together. Release is when the application exits and you dispose of the container so that the components are released properly. Note that the container never, Never, NEVER enters your application beyond this.


It will all depend on whether you are using Unity in the front layer, or only in the mid/back layers. If you are only using Unity on the back-end, then you would put the Unity configuration file in the same directory as the back-end executable. If you are also using Unity at the front end, you may find that you need a second copy of the configuration file with the front-ends executable as it is independent from the back-end.

Jason's advice on only referencing Unity in the bootstrapping method is good advice -- so the configuration file will be required in the same directory as the server executable that fires the bootstrapping and/or in the directory of the client executable that fires the bootstrapping.

In the system I currently work on, we use the same bootstrapping code (in a mid-layer DLL) in both the client and the server, as we use IoC throughout both. This means that both the client EXE and our server EXE need separate copies of the configuration.

0

精彩评论

暂无评论...
验证码 换一张
取 消