How do people get mixin style reuse in C #?

I come from a C ++ background where I can use mixins templates to write code that refers to FinalClass, which is the parameter of the template that is passed. This allows you to use reusable functions for any derived class, simply inheriting from ReusableMixin with the MyFinalClass parameter template. It all goes into the class, so it’s as if I wrote a big class that did everything - that is, really fast! Since mixins can chain, I can mix all kinds of behavior (and state) into one object.

If someone wants to clarify the technique, please ask. My question is, how can I reuse it in C #? Note: C # generics do not allow inheriting from a common parameter.

+4
source share
5 answers

In C #, the closest thing to C ++ style mixes is adding mixins as the fields of a class, and a bunch of transfer methods are added to the class:

public class MyClass { private readonly Mixin1 mixin1 = new Mixin1(); private readonly Mixin2 mixin2 = new Mixin2(); public int Property1 { get { return this.mixin1.Property1; } set { this.mixin1.Property1 = value; } } public void Do1() { this.mixin2.Do2(); } } 

This is usually enough if you want to import the functionality and state of mixes. Of course, mixin can be implemented as you like, complete with (private) fields, properties, methods, etc.

If your class also needs to express an is-a relationship with mixins, you need to do the following:

 interface IMixin1 { int Property1 { get; set; } } interface IMixin2 { void Do2(); } class MyClass : IMixin1, IMixin2 { // implementation same as before } 

(This is also the standard way how multiple inheritance is emulated in C #.)

Of course, mixin interfaces as well as mixin classes can be generics, for example. with the parameter of the most derived class or any other.

+4
source

You can use interfaces and extension methods. For instance:

 public interface MDoSomething // Where M is akin to I { // Don't really need any implementation } public static class MDoSomethingImplementation { public static string DoSomething(this MDoSomething @this, string bar) { /* TODO */ } } 

Now you can use mixins, inheriting from MDoSomething. Remember that using extension methods inside this class requires this qualifier. For instance:

 public class MixedIn : MDoSomething { public string DoSomethingGreat(string greatness) { // NB: this is used here. return this.DoSomething(greatness) + " is great."; } } public class Program { public static void Main() { MixedIn m = new MixedIn(); Console.WriteLine(m.DoSomething("SO")); Console.WriteLine(m.DoSomethingGreat("SO")); Console.ReadLine(); } } 

NTN.

+12
source

In C #, you can use the keyword "partial" to indicate that your class is implemented in multiple source files. Then you can use a small template tool to automatically generate additional source code files containing the methods that you want to enter into your class.

The T4 template tool included with Visual Studio can be used for this, but a more simplified approach can be used. See my own template engine: http://myxin.codeplex.com/

+2
source

Check out the remix library on .net at http://remix.codeplex.com

This library brings mixins to .NET.

+1
source

Do extension methods help in your scenario?

0
source

All Articles