views:

69

answers:

3

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?

+3  A: 

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.

Mehrdad Afshari
+3  A: 

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.

Charles Bretana
"The fact that the Car Types are used in Garage does Not require you to declare a reference to Radio in Garage" ... except when types from `Radio` are used in base class list or implemented interface list of _any_ public type in `Car`.
Pavel Minaev
@Pavel, I will check this to make sure, but if you don't actually USE the base class list/implemented interface type IN Garage, there's no need to reference it. If the only reference to it is in Car, then that's the only place a reference is needed.
Charles Bretana
@Charles, I've checked it, and it seems that you're correct - if you use `Derived` from `Car`, and `Derived` inherits from `Base` in `Radio`, then you have to reference `Radio` as well (even if you don't use any of its members), otherwise it works. I'm pretty sure there was an explicit rule covering this, either in ECMA CLI spec or C# spec - I'm trying to find that to see the exact wording.
Pavel Minaev
Ah, it was actually the documentation for csc.exe `/reference` option: "If you reference an assembly (Assembly A) that references another assembly (Assembly B), you will need to reference Assembly B if: 1) A type you use from Assembly A inherits from a type or implements an interface from Assembly B. 2) You invoke a field, property, event, or method that has a return type or parameter type from Assembly B"
Pavel Minaev
@Pavel, Thx!, I found your reference, and it's possibe to read that as "... . you will need to reference Assembly B if: ... " conditions 1) AND 2) are BOTH true, or, if EITHER is true. Apparently the latter interpretation is correct...
Charles Bretana
Thankyou for your effort guys
Tarks
A: 

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.)

Jeffrey L Whitledge