Log

The aspect code

public class LogAttribute : Attribute
{
    public interface ILogger
    {
        void Info(string text);
        void Error(string text);
    }
    public static ILogger Logger;
    public bool NetAspectAttribute = true;
    private static string ComputeLog(string eventName, MethodInfo method)
    {
        return string.Format("{0} {1} ", eventName, method.Name);
    }
    public void BeforeMethod(MethodInfo method)
    {
        Logger.Info(ComputeLog("On enter :", method));
    }
    public void AfterMethod(MethodInfo method)
    {
        Logger.Info(ComputeLog("On exit :", method));
    }
    public void OnExceptionMethod(MethodInfo method, Exception exception)
    {
        Logger.Error(ComputeLog(exception.Message, method));
    }
}

A sample class

public class ClassToWeave
{
    [Log]
    public int Divide(int value, int divideBy)
    {
        return value / divideBy;
    }
}

The following code is working

{
    var defaultLogger = new DefaultLogger();
    LogAttribute.Logger = defaultLogger;
    var classToWeave = new ClassToWeave();
    classToWeave.Divide(12, 3);
    Assert.AreEqual(2, defaultLogger.infos.Count);
    Assert.AreEqual(0, defaultLogger.errors.Count);
    try
    {
        classToWeave.Divide(12, 0);
    }
    catch (Exception)
    {
    }
    Assert.AreEqual(3, defaultLogger.infos.Count);
    Assert.AreEqual(1, defaultLogger.errors.Count);
}

Transactional

The aspect code

public class TransactionalAttribute : Attribute
{
    private static NHibernate.ISession session;
    public bool NetAspectAttribute = true;
    private NHibernate.ITransaction transaction;
    public static void SetSession(NHibernate.ISession newSession)
    {
        session = newSession;
    }
    public void AfterMethod()
    {
        transaction.Commit();
    }
    public void BeforeMethod()
    {
        transaction = session.BeginTransaction();
    }
    public void OnExceptionMethod()
    {
        transaction.Rollback();
    }
}

A sample class

public class ClassToWeave
{
    [Transactional]
    public int Divide(int value, int divideBy)
    {
        return value / divideBy;
    }
}

The following code is working

{
    TransactionalAttribute.SetSession(new FakeSession());
    var classToWeave = new ClassToWeave();
    classToWeave.Divide(12, 3);
    Assert.True(FakeTransation.IsCommited);
    Assert.False(FakeTransation.IsRollback);
}

GreaterThanOrEqualTo

The aspect code

public class GreaterThanOrEqualToAttribute : Attribute
{
    private readonly int _value;
    public bool NetAspectAttribute = true;
    public GreaterThanOrEqualToAttribute(int value)
    {
        _value = value;
    }
    public void BeforeMethodForParameter(ParameterInfo parameter, int parameterValue)
    {
        BeforeMethodForParameter(parameter, (long)parameterValue);
    }
    public void BeforeMethodForParameter(ParameterInfo parameter, long parameterValue)
    {
        if (parameterValue < _value)
            throw new Exception(string.Format("{0} must be greater than or equal to {1}", parameter.Name, _value));
    }
}

A sample class

public class ClassToWeave
{
    public void Check([GreaterThanOrEqualTo(10)] int param)
    {
    }
}

The following code is working

{
    var classToWeave_L = new ClassToWeave();
    classToWeave_L.Check(10);
    try
    {
        classToWeave_L.Check(9);
        Assert.Fail("Must fail");
    }
    catch (Exception e)
    {
    }
    classToWeave_L.Check(11);
}

GreaterThan

The aspect code

public class GreaterThanAttribute : Attribute
{
    private readonly int _value;
    public bool NetAspectAttribute = true;
    public GreaterThanAttribute(int value)
    {
        _value = value;
    }
    public void BeforeMethodForParameter(ParameterInfo parameter, int parameterValue)
    {
        BeforeMethodForParameter(parameter, (long)parameterValue);
    }
    public void BeforeMethodForParameter(ParameterInfo parameter, long parameterValue)
    {
        if (parameterValue <= _value)
            throw new Exception(string.Format("{0} must be greater than {1}", parameter.Name, _value));
    }
}

A sample class

public class ClassToWeave
{
    public void Check([GreaterThan(10)] int param)
    {
    }
    public void CheckLong([GreaterThan(10)] long param)
    {
    }
}

The following code is working

{
    var classToWeave_L = new ClassToWeave();
    try
    {
        classToWeave_L.Check(10);
        Assert.Fail("Must fail");
    }
    catch (Exception e)
    {
    }
    try
    {
        classToWeave_L.Check(9);
        Assert.Fail("Must fail");
    }
    catch (Exception e)
    {
    }
    try
    {
        classToWeave_L.CheckLong(9);
        Assert.Fail("Must fail");
    }
    catch (Exception e)
    {
    }
    classToWeave_L.Check(11);
    classToWeave_L.CheckLong(11);
}

LowerThan

The aspect code

public class LowerThanAttribute : Attribute
{
    private readonly int _value;
    public bool NetAspectAttribute = true;
    public LowerThanAttribute(int value)
    {
        _value = value;
    }
    public void BeforeMethodForParameter(ParameterInfo parameter, int parameterValue)
    {
        BeforeMethodForParameter(parameter, (long)parameterValue);
    }
    public void BeforeMethodForParameter(ParameterInfo parameter, long parameterValue)
    {
        if (parameterValue >= _value)
            throw new Exception(string.Format("{0} must be lower than {1}", parameter.Name, _value));
    }
}

A sample class

public class ClassToWeave
{
    public void Check([LowerThan(10)] int param)
    {
    }
}

The following code is working

{
    var classToWeave_L = new ClassToWeave();
    try
    {
        classToWeave_L.Check(10);
        Assert.Fail("Must fail");
    }
    catch (Exception e)
    {
    }
    try
    {
        classToWeave_L.Check(11);
        Assert.Fail("Must fail");
    }
    catch (Exception e)
    {
    }
    classToWeave_L.Check(9);
}

NotNullOrEmpty

The aspect code

public class NotNullOrEmptyAttribute : Attribute
{
    public bool NetAspectAttribute = true;
    public void BeforeMethodForParameter(ParameterInfo parameter, string parameterValue)
    {
        if (string.IsNullOrEmpty(parameterValue))
            throw new ArgumentNullException(parameter.Name);
    }
}

A sample class

public class ClassToWeave
{
    public void Check([NotNullOrEmpty] string param)
    {
    }
}

The following code is working

{
    var classToWeave_L = new ClassToWeave();
    try
    {
        classToWeave_L.Check(null);
        Assert.Fail("Must fail");
    }
    catch (ArgumentNullException e)
    {
        Assert.AreEqual("param", e.ParamName);
    }
    try
    {
        classToWeave_L.Check("");
        Assert.Fail("Must fail");
    }
    catch (ArgumentNullException e)
    {
        Assert.AreEqual("param", e.ParamName);
    }
    classToWeave_L.Check("not empty");
}

NotNull

The aspect code

public class NotNullAttribute : Attribute
{
    public bool NetAspectAttribute = true;
    public void BeforeMethodForParameter(ParameterInfo parameter, object parameterValue)
    {
        if (parameterValue == null)
            throw new ArgumentNullException(parameter.Name);
    }
}

A sample class

public class ClassToWeave
{
    public void Check([NotNull] string param)
    {
    }
}

The following code is working

{
    var classToWeave_L = new ClassToWeave();
    try
    {
        classToWeave_L.Check(null);
        Assert.Fail("Must fail");
    }
    catch (ArgumentNullException e)
    {
        Assert.AreEqual("param", e.ParamName);
    }
    classToWeave_L.Check("");
}

Trim

The aspect code

public class TrimAttribute : Attribute
{
    public bool NetAspectAttribute = true;
    public void BeforeMethodForParameter(ref string parameterValue)
    {
        if (string.IsNullOrEmpty(parameterValue))
            return;
        parameterValue = parameterValue.Trim();
    }
}

A sample class

public class ClassToWeave
{
    public string TrimWithNetAspect([Trim] string param)
    {
        return param;
    }
}

The following code is working

{
    var classToWeave_L = new ClassToWeave();
    Assert.AreEqual("ToTrim", classToWeave_L.TrimWithNetAspect("ToTrim    "));
}