The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by mk7306]

Hi Fergal,

I just finished reading your book and I learned a great deal!
I read from cover to cover and thoroughly enjoyed myself.

I'd like to ask you a question concerning the implementation
of public interfaces. For some reason I just can't seem to get
my head around this concept. In listings 5.13, 5.14 and 5.15
an example is presented using an interface. When the client
invokes the service it references the interface instead of
the Base64Client class:

IStringEncoder objRef = (IStringEncoder)Activator.GetObject(
typeof(IStringEncoder),
"http://localhost/RemoteEncoder/Base64Service.rem"

My question is: When an interface
is implemented in a class, is that interface then available
as a contained subset of the class which can be accessed
directly? In other words, the original class effectively
becomes "two classes" with separate functionalities where
any reference to the interface in istringencoder.dll is
routed to the contained interface functionality in the
Base64Client class.

If the code above were:

IStringEncoder objRef = (IStringEncoder)Activator.GetObject(
typeof(Base64Client),
"http://localhost/RemoteEncoder/Base64Service.rem"

where Base64Client was cast to IStringEncoder, I'd be OK.

Thanks again for this great book, Mark
import-bot (20211) [Avatar] Offline
#2
Re: Interfaces
[Originally posted by fgrimes]

Hi Mark,

>> I'd like to ask you a question concerning the implementation
>> of public interfaces... In listings 5.13, 5.14 and 5.15...

To make matters simpler, let's forget about remoting. The ability
to distribute an interface to developers of remote clients is just
a convenience, which I'll come back to below. Instead consider the
OO (C#) interface.

The proverb says that we shouldn't compare apples and oranges because
they are different. So, if we were to write a program to process
them, we would recognize their inherent difference by implementing
them as separate and distinct classes. The apple class and orange
class would not be related in any way: not by inheritance, not by
containment, etc.

Yet it is obvious that we can perform common operations on either
apples or oranges. Both can be collected, counted, sorted, and so on.
So for OO purposes, for example, both the apple class and the orange class
might implement an ICountable interface. (The framework contains just
such an interface; it is called IEnumerable.)

But you might say "Why bother?" The reason is that doing so allows a
"counter" program to count without knowing whether it is counting apples
or oranges. It can count anything that is countable; i.e. anything
that implements the ICountable interface.

This can bring enormous simplification. For example, more than 100
framework colection-type classes implement IEnumerable. This means that
C# can use the same "foreach" keyword to iterate through them without
regard for the fact that they are all inherently different and unrelated.

So an interface just captures similarities between unrelated classes. It
acts as a contract. For example, each class that implements IEnumerable
is "contractually obligated" to be enumerable.

Now back to remoting...

In listings 5.13, 5.14 and 5.15, I use an interface for a different reason.
I want to avoid the need to ship the Base64Service.dll to client developers.
So instead, I ship the interface; i.e. the contract. It is a tiny file
containing no executable code at all. As such, it is ideal for distribution,
and it is all the client needs to make remote Encode and Decode calls.

Does this help clarify the matter? If not, let me know. Perhaps I could
have said more in the book.

>> I read from cover to cover and thoroughly enjoyed myself.

Glad to hear it. If you wish, you could tell other potential readers at
Amazon.com. That helps pay the bills here! Thanks.

Regards,
Fergal


import-bot (20211) [Avatar] Offline
#3
Re: Interfaces
[Originally posted by mk7306]

Hi Fergal,

Actually, my question concerned the seeming ability to call
methods on an interface, which by definition has no implementation
defined. If the proxy is created with an object derived only from
the IStringEncoder interface, then how are the Encode and Decode
methods carried out. If, on the other hand, these methods are implicitly
called on the Base64Service class because it is of type IStringEncoder
then I'm cool.

Thanks, Mark
import-bot (20211) [Avatar] Offline
#4
Re: Interfaces
[Originally posted by mk7306]

Hi Fergal,

The quote below is what prompted my original question.

> As such, it is ideal for distribution,
> and it is all the client needs to make remote Encode and Decode calls.

I understand the reason for minimizing the file size for the proxy and
not casting Base64Service to the interface.

But what if the Base64Service.dll didn't exist. You would be calling
methods on an interface which had no implementation of it's defined
methods.

Since Base64Service.dll does exist I must then assume that calls made
through the proxy are routed to Base64Service class where implementations
of Encode and Decode do exist.

This is all my original question was about. I had simply not seen this
ability of making calls on an interface without a cast to the class
where the methods being called were fleshed out.

Maybe I'm making this more difficult than it needs to be. I understand the
use of interfaces, simply not the semantics of this particular instance.

I greatly appreciate you taking the time to discuss this with me and since
I understood everything else in your wonderful book, I think I'm probably
doing OK.

Thanks again, Mark
import-bot (20211) [Avatar] Offline
#5
Re: Interfaces
[Originally posted by fgrimes]

Hi Mark,

>> Actually, my question concerned the seeming ability to call
>> methods on an interface, which by definition has no implementation
>> defined.

My previous answer was my best shot... looks like I'm missing the point.

>> If the proxy is created...

Is it the coexistence of remoting and an interface in this example that
creates a special difficulty? If not, can one be omitted and your question
simplified?

Regards,
Fergal