Wiki

Clone wiki

Mixins / Home

Welcome

A mixin is a class that provides a certain functionality to be inherited or just reused by a subclass.

This project gives simple Mixin implementation fo c# using PostSharp AOP library.

Mixins give simple way to cope with common issues like adding funcionality to comon classes that have to inherit from some framework base class, or compose custom behaviours of more than one base classes.

For now examples of using mixins are in the test project.

Basic%20scenario

Here's an example:

public interface IAddedFunc
{
    string Func();
}

public class AddedFunc: IAddedFunc
{
    public string Func()
    {
       // Implementation
    }
}

[Mixin(typeof(AddedFunc))]
public class ClassWithAddedFunc : BaseClass1
{
}

[Mixin(typeof(AddedFunc))]
public class ClassWithAddedFunc2 : BaseClass2
{
}

PostSharp.Mixins gives also suport for overriding methods inherited from Mixins:

 public interface IFormating
    {
        string GetFormat();
        string GetPrefix();
    }

    public class TypeStringWithVirtualMethod : ITypeString, IFormating
    {
        public object Target { get; set; }

        public string GetTypeString()
        {
            return string.Format(GetFormat(), this.Target.GetType().ToString());
        }

        public string GetTypeStringFormated(string format)
        {
            return string.Format(this.GetPrefix() + format, this.Target.GetType().ToString());
        }
        
        [Abstract]
        public virtual string GetFormat()
        {
            throw ToBe.Overrided;
        }

        [Virtual]
        public virtual string GetPrefix()
        {
            return "mixinPrefix-";
        }
    }

    [Mixin(typeof(TypeStringWithVirtualMethod))]
    public class ClassWithOverridenMixinMethod : BaseClass, ITypeString, IFormating
    {
        private string _format;

        private TypeStringWithVirtualMethod baseTypeStringWithVirtualMethod { get; set; }

        public ClassWithOverridenMixinMethod(string format)
        {
            this._format = format;
        }

        public string GetTypeString()
        {
            throw ToBe.Introduced;
        }

        public string GetTypeStringFormated(string format)
        {
            throw ToBe.Introduced;
        }

        [Override]
        public virtual string GetFormat()
        {
            return _format;
        }

        [Override]
        public virtual string GetPrefix()
        {
            return baseTypeStringWithVirtualMethod.GetPrefix(true) + "class prefix-";
        } 
    }

Updated