Click here to Skip to main content
15,881,880 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have the constructor below. My question is:
1. Should I extract out interfaces for the objects being created in the constructor and then mock those objects with some mocking framework? 2. How would I test this constructor?

Constructor:
C#
protected SomeObjectIveCreated1 member1{ get; set; }
private readonly SomeObjectIveCreated2 member2;

public MyConstructor(string name)
        {
            this.member0= name;
            this.member1= new SomeObjectIveCreated1(this as ISomeInterface);
            this.member2= new SomeObjectIveCreated2();
        }  
Posted
Comments
Sergey Alexandrovich Kryukov 19-Nov-12 15:36pm    
Not clear. What interfaces? What is "extract"? Are you talking about unit testing? Unit testing facilities usually provide interfaces containing metadata pointing out what to test, for the automated testing application. What are you trying to do?
--SA
chuckdawit 28-Nov-12 20:21pm    
Should I replace the 'SomeObjectIveCreated2' with an interface to adhere to rules of encapsulation?
This is helpful for unit testing but is more for the rules regarding the S.O.L.I.D principles.

1 solution

chuckdawit wrote:
Should I replace the 'SomeObjectIveCreated2' with an interface to adhere to rules of encapsulation? This is helpful for unit testing but is more for the rules regarding the S.O.L.I.D principles.
You should understand that interfaces, as opposed to abstract classes and "classic" OOP (before introduction of interfaces) does not really "improve" encapsulation. Encapsulation is more basic OOP feature, not directly related to the heart of OOP, which is related to late binding and polymorphism.

Instead, interfaces provides loose coupling between interfaces and implementation. (Please don't be confused here: I used the word "interface" in two different meanings in one phrase — sorry; in first case, "interface" is used in narrow sense of this word, as a kind of type in .NET and other technologies typically using keyword "interface; in second case, the meaning of interface is abstract, in this sense, the content of abstract class and .NET interface are both interfaces.)

Besides, in .NET and other technologies without multiple inheritance in the sense C++, interfaces provides the only available multiple inheritance, sometimes called "weak form of multiple inheritance".

Typically, you use interfaces when you want some common features outside of the class hierarchy. Moreover, interfaces can be implemented by struct types, which are value types, not reference types. This way, polymorphism based on interfaces, is "more abstract": it hides the difference between reference types and value types: they all are represented in a polymorphic set through some base compile-time type which is the interface managed pointer, as if they all were reference types. And the weak form of multiple inheritance allows the same object instance to participate in different polymorphic sets with different base types. Such kind of loose coupling allows to go beyond the tree structure of inheritance diagram.

Interfaces are often used in APIs to free the API users from fitting their implementation types in rigid class hierarchy.

Now, you need to identify if you really need the flexibility of interfaces in your application.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900