0

I am trying to understand Microsoft.practices.Unity.

So, I have this solution:

  • webproject
  • business classlibrary project as my logic tier
  • data classlibrary project as my data access tier

And I want to use Unity to separate web tier from logic and separate logic tier from data, using DI.

I have created an unity.config file in my web project, cause I wanna control the registration from a configuration file, and not inside binary code. This is OK for me. I am using Unity.MVC4.

But, with that, I only resolve my dependency injection only from web to business tier. And how can I make the same thing for business to data tier ?

I have already seen some web examples but I am still confused, because no example shows me the process through the web tier to data tier, step by step, to understand how to implement the Unity DI.

I would like to see a simple example, with a n-tier solution with total DI implementation with Unity.

halfer
  • 19,824
  • 17
  • 99
  • 186
Olivertech
  • 567
  • 2
  • 4
  • 24
  • 1
    Related reading: http://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application – Randy Levy Sep 06 '13 at 05:53

1 Answers1

0

Prevent from using the config file for registration of dependencies. This is brittle and error prone and you can only do a subset of things that you can do in code. If you're doing this because you want to prevent dependency references, please note that by using the config file, the same referencing still applies, but now it's implicit and there's no compile time checking to help you.

This doesn't mean though that you should never use the config file, but you should only use it to configure things that can actually change during or after deployment. Most things shouldn't change during that time, since most changes must be changed by a developer, either manually by starting the application, or in an automated fashion using unit tests.

Neither would place class names in the config file for the same reason as it is brittle. Using configuration switches is usually much better, since this allows you to move the class names to the code (with a switch case statement or if statement to change configuration based on the config setting) and enables compile time checking.

For the rest of your questions, Tuzo's link will probably give you enough information.

Community
  • 1
  • 1
Steven
  • 166,672
  • 24
  • 332
  • 435