tags:

views:

68

answers:

3

Hello

I saw lot of posts in StackOverflow and elsewhere talking about concrete implementation. While fiddling with WCF i came through the line

Tying your service implementation, or any “service based” class to a concrete implementation is never a good idea.

Can anyone explain what Concrete Implementation is?

Regards

NLV

+1  A: 

If you have an interface or abstract class, it need to be implemented.

A class that implements such an interface or class is called a concrete implementation (because only such an implemented class can be instantiated).

The principle stated means that you shouldn't code against the concrete implementation directly, as you may want to swap it for another concrete implementation at a later time without changing your code. This means you should use interface and abstract class references instead of concrete implementations.

Oded
+5  A: 

It is implementation of something abstract (abstract class, interface). Note that you can instantiate only objects of concrete classes.

Just for example if you have :

abstract class AbstractClass
{
    .......
   // Here you have some abstract methods 
}

class ConcreteClass : AbstractClass
{
.......
}

In case of WCF it wants to say that although it is allowed to mark classes with ServiceContract attribute better to have it on separate Interface and implement that interface in concrete class marked with ServiceBehavior attribute.

Like this :

[ServiceContract(Namespace = "MyNamespaceName")]
interface IMyInterface
{
    [OperationContract]
    int SomeMethod(.....);

   ......    
   ......    
}

[ServiceBehavior(......)]
public class SomethingConcrete : IMyInterface
{
    // implementation of SomeMethod
}
Incognito
+1  A: 

In the term "concrete implementation", the word "concrete" is redundant. An implementation is always concrete, there is no abstract implementation. So, what's important is just the term "implementation".

What it means in this case is that you should code against an abstraction instead of directly against an implementation, i.e. you define an interface for what the classes has to support, and use the interface instead of the class. That way you can substitute one implementation against another as long as they implement the interface.

Guffa
would an abstract class that implements an interface be considered zero implementation, or half an implementation?
icelava
@icelava: The abstract class as whole is abstract, even if it contains a complete implemenation for the interface. Marking a class as abstract only prevents it from being instantiated, it doesn't prohibit the content from being implementation. An abstract class doesn't have to be totally abstract, like an interface is. The `abstract` keyword may be somewhat misleading in that sense, the equivalent VB keyword `MustInherit` is actually a more precise term.
Guffa