Mixins and C#

In one of my previous lives when I first heard about mixin and tried to look it up I hit into various conflicting definitions. The definition of Mixin I settled for is "MixIn programming is a style of software development where units of functionality are created in a class and then mixed in with other classes". In C++ two common ways of doing this is multiple-inheritance and parameterized Abstract Sub Class (yep not Abstract Base class). I'll not get into MI because the basic design of C# will never allow it. However C# may be expanded to include Mixin using generics.

Mixin in C++

The definition of mixin above is pretty general. Even though mixin is a class it is intended to be combined with other classes and not to be used standalone. Mixins can be defined using parameterized inheritance of Abstract Sub class as follows

 template <typename Base>class MyMixin : public Base{};template <typename Base>class AnotherMixin : public Base{}; MyMixin<AnotherMixin<string> > mm; 

In the above code both MyMixin and AnotherMixin derive from the type parameter passed to it. They are Abstract Sub classes because their super classes are not pre-determined. It is possible to chain them to create a singly-inherited hierarchy so that the combined class gets the public functionality of all the mixin classes in the chain. Lets take a look at a more concrete example....

 #include <iostream>#include <string>#include <ctime>using namespace std; template  <typename T> class AgeProvider : public T{    time_t createdOn;public:    AgeProvider() {        time(&createdOn);    }    double age() {        time_t currTime;         time(&currTime);        return difftime(currTime, createdOn);    }    string CreatedOn () { return ctime(&createdOn); }}; template  <typename T> class CountProvider : public T{    static unsigned counter;public:    CountProvider() {        CountProvider::counter++;    }    unsigned GetCount() {        return counter;    }};template<class T> unsigned CountProvider<T>::counter = 0;
typedef AgeProvider<CountProvider<string> > TrackedString; int main(int argc, char* argv[]){    AgeProvider<CountProvider<string> > tstr;     tstr.append("Abhinaba ");    tstr.append("Basu");    cout << "Content : " << tstr << endl;    cout << "Created : " << tstr.CreatedOn() ;    cout << "Age : " << tstr.age()  << endl ;    cout << "Count : " << tstr.GetCount()  << endl;    return 0;}

Here the first mixin class in AgeProvider which gives info on the age in seconds of an instance of the class as well as when the instance was created. The other mixin is CountProvider which gives how many instances of a specific class was ever made. Both of these classes have no use on their own. However when they are mixed together with a stand-alone class like string they provide the funtionality of counting and age to that class. Interestingly the whole of the above works without any multiple-inheritance and not imposing any restriction on the Mixin classes on what they inherit from or the inheritance order.

Mixins In C#

Some suggest that extension methods in the upcoming C# 3.0 are a kind of Mixins, because you can put in functionalities in these methods and arbitrarily tag it onto any class you want. In the C# 2.0 specification section 20.1.3 it is clearly called out that the base class of a generic class has to be a constructed class type so this rules out using the above approach to be used in C# . I am not too sure on why we choose to explicitly disallow abstract subclass. Since C# does not support multiple-inheritance, IMO it should have supported Mixin style coding.