Click here to Skip to main content
13,152,063 members (37,891 online)
Rate this:
Please Sign up or sign in to 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?

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 19-Nov-12 9:13am
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?
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170924.2 | Last Updated 29 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100