Home Contact

Igor Milovanović

.NET, cats and more...

News

Article Categories

Archives

Post Categories

Image Galleries

Blogs I read

Communities

Links

Syndication:

AOP meets .NET - Yet another persistence Framework

As the ObjectSpaces are running late, the development of o/r mapping tools and persistence frameworks seems to be the popular thing to do among .NET developers. Somebody counted over 30 projects and the number is daily increasing. (Ok. I admit it. I wrote one too. But I have an excuse :). I am playing with EOS  (an aspect-oriented extension for C#) at the moment, and I thought I could show a quick implementation of a persistence aspect.

Let's take a look at the following example:

using System;

using ObjectStorage;
using PersistentObjects;

namespace PersistentObjects
{
    class Cat
    {
        string _name;
        int _lives;
        public string Name
        {
            get {return _name;}
            set {_name = value;}
        }
        
        public int Lives
        {
            get {return _lives;}
            set {_lives = value;}
        }
    }

    class Dog
    {
        string _name;
        
        public string Name
        {
            get {return _name;}
            set {_name = value;}
        }
    }
}
    
namespace Main
{
    class MainClass
    {
        [STAThread]
        static public void Main (string[] args)
        {
            Cat cat = new Cat ();
            cat.Name = "Garfield";
            cat.Lives = 9;
            
            Dog dog = new Dog ();
            dog.Name = "Oddie";
            
            ObjectSpace.Dump ();
            ObjectSpace.Store ();
        }
    }
}

I would like the classes Cat and Dog  from the namespace PersistentObjects to register themselves "magically" in the ObjectSpace so that they can be stored as soon as they change. I also want to assign a unique id to every instance, in order to be able to find them later. This is not easily accomplished in standard CSharp, especially if you don't want to touch the original code. Some persistence tools modify the IL-Code (code enhancement) in order to intercept the field gets and sets. In Java AOP tools like AspectJ we would define a field pointcut to intercept the field calls, and the unique ID would be introduced. This can be done in EOS as well:

using System;
using System.Collections;
using System.IO;
using System.Reflection;

namespace ObjectStorage
{
    public aspect ObjectSpace
    {
        static BindingFlags InstanceFields = BindingFlags.Default |
        BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
        static Hashtable changedObjects = new Hashtable ();
    
        // Every object in a object space should have a unique id
        introduce in PersistentObjects.any
        {
            private Guid _guid = System.Guid.NewGuid();
            
            public Guid ID
            {
                get {return _guid;}
                set {_guid = value;}
            }
        }
        
        // keep track of changed objects from the persistenceobjects namespace
        after():fset (any PersistentObjects.any.any)
        {
            changedObjects[thisJoinPoint.getTarget()] = "changed";
        }
        // dump changed objects
        public static void Dump ()
        {
            foreach (DictionaryEntry entry in changedObjects)
            {
                Console.WriteLine ("{0}, {1}", entry.Key.ToString (), entry.Value.ToString());
            }
        }
        
        // store all changed objects to persistence.
        public static void Store ()
        {
            #region this code writes the changed objects to xml file
            StreamWriter writer = File.CreateText ("persistence.xml");
            using (writer)
            {
                writer.WriteLine ("<Persistence>");
                
                foreach (DictionaryEntry entry in changedObjects)
                {
                    writer.WriteLine ("<" + entry.Key.GetType () + ">");
                    foreach (FieldInfo fieldInfo in entry.Key.GetType ().GetFields (InstanceFields))
                    {
                        writer.WriteLine ("<Field Name=\"" + fieldInfo.Name + "\" type =\"" + fieldInfo.FieldType +"\">");
                        writer.WriteLine (fieldInfo.GetValue (entry.Key));
                        writer.WriteLine ("</Field>");
                    }
                    writer.WriteLine ("</" + entry.Key.GetType () + ">");
                }
                writer.WriteLine ("</Persistence>");
            }
            #endregion
        }
    }
}

If you run this code after compiling it with eos compiler (note that we are not in csharp-pure world anymore!) you will get a file persistence.xml with stored cats and dogs, each having its own id:

<Persistence>
  <PersistentObjects.Dog>
     <Field Name="_name_Eos_Original" type ="System.String">
       Oddie
     </Field>
    <Field Name="_guid_Eos_Original" type ="System.Guid">
       99571520-e2b3-48a4-8ccc-90cd55d212fd
    </Field>
  </PersistentObjects.Dog>
  <PersistentObjects.Cat>
     <Field Name="_name_Eos_Original" type ="System.String">
       Garfield
    </Field>
    <Field Name="_lives_Eos_Original" type ="System.Int32">
       9
    </Field>
    <Field Name="_guid_Eos_Original" type ="System.Guid">
       801f3149-b825-4ff1-88bd-cb80a107ee21
    </Field>
  </PersistentObjects.Cat>
</Persistence>

I hope that this (rather simple) example gives you the feeling what it is like to work with an aop tool with a rich pointcut model in programming language. It would be nice to see eos in a more complete  (almost no documentation at all) and  open source version soon, or even better to get something like AspectJ or eos in .NET framework itself.


 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Thursday, September 23, 2004 11:48 AM

Feedback

# re: AOP meets .NET - Yet another persistence Framework

Hi,
I went through your sample and explaination. I am evaluating EOS. I wish to insert a piece of code inside an existing catch block. I guess i need to use "pointcut hanlder". But can u help me out with samples. thanks.
Regards,
clara 6/30/2005 11:43 AM | Clara

# re: AOP meets .NET - Yet another persistence Framework

Very Smart 11/13/2006 12:56 PM | Love

# re: AOP meets .NET - Yet another persistence Framework

thanx reality description 7/14/2008 1:23 AM | avatarlar

# re: AOP meets .NET - Yet another persistence Framework

I believe the examples explaining persistence framework are well-explained…yet as I have little knowledge about programming…I could comprehend a little.but I am sure from the comments posted here that this would be an exclusive for many programmers. 11/23/2010 7:00 PM | earn money with hotfile

# re: AOP meets .NET - Yet another persistence Framework

It's not only important to understand the uses and strategies for implementing your solutions using it, but also how interception and AOP works deep down in .NET. Instead of a long, drawn out post, I think I'll just include some articles and posts that do a very good job of explaining some of the ideas behind it.best weight loss pills 12/6/2010 10:57 PM | Rohan

# re: AOP meets .NET - Yet another persistence Framework

Hi, Thanks for this coding . I am sure this coding is perform well .

yellow cab Mountain View 2/7/2011 6:49 AM | yellow cab Mountain View

# re: AOP meets .NET - Yet another persistence Framework

i will tray this coding... look preety practice :) it's aHot Report On The Net 2/7/2011 8:44 PM | lavistayk

# re: AOP meets .NET - Yet another persistence Framework

It's not only important to understand the uses and strategies for implementing your solutions using it,Flour Milling Machinery but also how interception and AOP works deep down in .NET. 3/9/2011 11:20 AM | flour mill

# re: AOP meets .NET - Yet another persistence Framework

Wow, nice post,there are many person searching about that now they will find enough resources by your post.Thank you for sharing to us.Please one more post about that. Affiliate Script

4/2/2011 1:58 PM | Genius

# movers

Packing and Moving is a nightmare for almost everyone. It requires you to sort unwanted stuff from the absolutely necessary. Taking everything along with you to a new place is just not possible nor is it logical. Leaving behind or giving away some of the things can as it is be emotionally draining for any person. chicago movers

4/2/2011 2:08 PM | jasonlayer

# re: AOP meets .NET - Yet another persistence Framework

Thanks for sharing! I've been looking for a solution for a while, this is perfect! I will post back when I've tried this code. Footbal Uniforms 7/6/2011 9:03 PM | Football Uniforms

# re: AOP meets .NET - Yet another persistence Framework

Thanks for sharing! This code is what i've been searching for. Hopefully this works! 8/1/2011 1:29 PM | FBI Museum

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: