Geeks With Blogs
Igor Milovanović .NET, cats and more...

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.


 

Posted on Thursday, September 23, 2004 11:48 AM | Back to top


Comments on this post: AOP meets .NET - Yet another persistence Framework

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
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
Left by Clara on Jun 30, 2005 11:43 AM

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
Very Smart
Left by Love on Nov 13, 2006 12:56 PM

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
thanx reality description
Left by avatarlar on Jul 14, 2008 1:23 AM

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
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.
Left by earn money with hotfile on Nov 23, 2010 7:00 PM

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
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
Left by Rohan on Dec 06, 2010 10:57 PM

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
Hi, Thanks for this coding . I am sure this coding is perform well .

yellow cab Mountain View
Left by yellow cab Mountain View on Feb 07, 2011 6:49 AM

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
i will tray this coding... look preety practice :) it's aHot Report On The Net
Left by lavistayk on Feb 07, 2011 8:44 PM

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
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.
Left by flour mill on Mar 09, 2011 11:20 AM

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
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

Left by Genius on Apr 02, 2011 1:58 PM

# movers
Requesting Gravatar...
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

Left by jasonlayer on Apr 02, 2011 2:08 PM

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
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
Left by Football Uniforms on Jul 06, 2011 9:03 PM

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
Thanks for sharing! This code is what i've been searching for. Hopefully this works!
Left by FBI Museum on Aug 01, 2011 1:29 PM

# re: AOP meets .NET - Yet another persistence Framework
Requesting Gravatar...
I would like to say that this code is awesome that's the one i've been searching for :)

annonces en ligne
Left by Guy on May 28, 2012 11:51 PM

Your comment:
 (will show your gravatar)


Copyright © Igor Milovanovic | Powered by: GeeksWithBlogs.net