".
Figure 2.1 Attribute programming in C# way to separate concerns".Define custom attributes
In this section, we will use custom attributes to implement AOP concern separation. In order that a class be used as an attribute it should inherit from "System.Attribute". So we will inherit both our cross cut concern classes from "System.Attribute".Lets make a quick glance what steps should be needed to make a class attributed:
[ClsEmail("shiv_koirala@yahoo.com")][ClsPrint("myprinter")]public void Add(){ Console.WriteLine("Add routine called of the customer");}
OK, so below is the paste of all three codes: i.e. two Cross cut concern (Email and Print) and main concern (Customer). Hope you will be able to follow it.
Cross cut Email code
using System;using System.Reflection;namespace CustomerSalesClassProject{ /// <summary> /// Summary description for ClsEmail. /// </summary>
[AttributeUsage(AttributeTargets.All)] public sealed class ClsEmail : System.Attribute { private string strEmail; public ClsEmail(string pstrEmail) { // // TODO: Add constructor logic here // strEmail = pstrEmail; Send(); } public void Send() { // This method sends email Console.WriteLine("Email sent to admin"); Console.ReadLine(); } }}
Email class is inherited from "System.Attribute". I will explain in short about the above code. Cross cut concern Print class:
using System;namespace CustomerSalesClassProject{ /// <summary> /// Summary description for ClsPrint. /// </summary>
[AttributeUsage(AttributeTargets.All)] public class ClsPrint : System.Attribute { private string strPrinterName; public ClsPrint(string pstrPrinterName) { // // TODO: Add constructor logic here // strPrinterName = pstrPrinterName; Print(); } public bool Print() { // This methods prints the sales or the customer data Console.WriteLine("Printed to printer"); return true; } }}
Finally, the customer class which calls both the concerns:
using System;using System.Reflection;namespace CustomerSalesClassProject{ /// <summary> /// Summary description for Class1. /// </summary>
public class ClsCustomer : ContextBoundObject { public ClsCustomer() { // // TODO: Add constructor logic here // }
// Previously tangled code /*(); } */ // Attribute applied code [ClsEmail("shiv_koirala@yahoo.com")] [ClsPrint("myprinter")] public void Add() { Console.WriteLine("Add routine called of the customer"); } }
Attributed programming problems
Following are the problems of using attributed programming to implement AOP:
The main problem with the above defined way is I can not specify my Advice. In short, I cannot specify at what moment should the Cross cut concern code run. You can see that both the Cross cut concerns are firing before the customer is added to the database. The proper way will be to add the customer to the database and then send email and print it on the printer. We will see in the next section how to define when the Cross cut concern code should be processed, using "Context" attributes.
Second, all my property are set using the constructor of the class, which is a very ugly way of doing it. That means all our business logic should run through the constructor also.
The last and the main one I have to make changes to is the main core concern class by specifying the attribute on the method. Would not it be best that I just define and code both classes (Cross cut concern) differently and then specify during compiling to weave these two cross cut concerns with the main concern and generate that combined magic code? Hmmm.. looks like a dream but we will be doing that practically in the next section.
The second problem can not really be removed because that's the way .NET attribute programming works. So in order to remove that wait for .NET to either become AOP compliant or live with it.Context bound objects.Using the EOS compiler.
Bin (This is where the compiler is present, it's an extension to the C# compiler.)
Docs (The documentation.)
Examples (Sample code.)
Once you have got all these things, let's code a sample AOP and compile to see the output using the EOS compiler.Compiling sample code:
Customer class (Main concern).
Printer class (Aspect).
Main method to run and see the output.".
public aspect ClsCrossCutPrinter{ public ClsCrossCutPrinter() { // // TODO: Add constructor logic here // }
Now the second thing is the "introduce" keyword, which says to weave this method (Print) in the customer class.Copyright (C) The Rector and Visitors of the University of Virginia.All rights reserved.
OK, now let's try to run the compiled EXE. You will get some error like this:
Figure 2.2 Error, need to copy "Eos.Runtime.dll" to the EXE folderOK, the error is because you need to copy "Eos.Runtime.dll" where the EXE is present. So just go to the bin directory and copy the file. Now when you run the EXE, you will see the following output.Update Method calledPrint.
View All