Tutorial :Use types referenced by a referenced project in C#



Question:

I'm new to splitting up my applications into multiple solutions (in other words I just got into the real world), I have a situation where I'm fully seperating the logic from the UI into a seperate project. Where I'm falling down is my glue code.

As a simple example.

Project car references projects containing door,radio etc

Project garage references project car, but needs to use radio.

Do I need to add a reference to the radio project in the garage project?


Solution:1

Yes, you'll need to add a reference to both libraries. In general, you need to add a reference to all libraries declaring types you directly use; by declaring variables of those types, inheriting from them, using methods that has the type as parameters or return type, etc.


Solution:2

Depends on whether or not, within Garage, you are directly referencing Types defined in project Radio. If you are, then you need to create a reference. If the only use you are making of these radio Types is in the Car Assembly, the fact that the Car Types are used in Garage does Not require you to declare a reference to Radio in Garage, (except as mentioned in EDIT Note below).

EDIT (based on comment from @Pavel): There is one other scenario/condition where you need to reference another assembly: If you are using a Type from a referenced assembly that derives from a type, (or implements an interface) defined in a third assembly, you also need to reference that third assembly.


Solution:3

You might consider whether jumping levels like that is really what you want to do. In many of these cases, it's better for the garage to ask the car to turn down that damn noise, rather than accessing the radio directly.

It just depends on what makes sense for your system.

If the garage doesn't need to touch the radio directly, then the fact that the garage references the car and the car references the radio does not mean that garage needs to reference the radio.

But if the garage does need to mess with it, then it will need a reference.

Please be aware of the fact that skipping tiers is very often indicative of problems with the abstractions or the architecture. (Though skipping tiers may not be what is happening in this particular case.)


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »