One of the most sought after ORM functionality, is passing objects over the wire (Webservices, Sockets etc). ORM is not complete, if you cannot disconnect an entity from the session, for it to be passed across application tiers.

Rapid entity maintains two different types of entities.

1] Stateful Entities : This kind of entity does not go out of the scope of the EntityManager class (The class responsible for managing entities). Stateful entities are connected to the Rapid session/context, they are attached to the session, and they cannot be passed over web services. Changes made to statefull entites can be tracked by the Rapid Runtime Engine (RRE) .

2] Stateless Entities : This kind of entities can be detach and re-attached back to Rapid context/session. Stateless can be wired over the web services, and any changes made to stateless is not tracked by Rapid Runtime Engine (RRE) . Although you can attach the stateless back to RRE via the EntityManager. This will enable the object to join the RRE back for change tracking. Changes can also be resolved when you try to save/update a stateless entity (only new and updated values will persisted).

We can now send our entities over messaging protocols like WCF, ASMX etc. Rapid Entity does not have any relationships between any protocols, but it has is detach functionality that is used to detached entities from the session/context. Using Rapid with WCF wouldn't have been better without the Detach functionality.

Given the following Service contract

    [ServiceContract]
    public interface IRapidService
    {
        [OperationContract]
        Audittrail GetAllAudit();
    }

Here is the implementation of our GetAllAudit() method
        public Audittrail GetAllAudit()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["MySql"].ConnectionString;
            IConfiguration configurationFactory = ConfigurationFactory.GetInstance(connectionString);

            configurationFactory.CacheSettings(10, 1000);
            configurationFactory.ProviderDialect = ProviderDialect.MySQLProvider;

            using(EntityManager manager = new EntityManager())
            {
                manager.OpenDatabaseSession();

                Audittrail trail = new Audittrail();
                trail.Action = "Save Entity";
                trail.Message = "Hello WCF";
                trail.MethodName = MethodBase.GetCurrentMethod().Name;
                trail.UserName = "Ahmed Salako";
                trail.StackTraces = new List<StackTrace>();
                trail.StackTraces.Add(new StackTrace { Audittrail = trail, Message = "Current Trace" });

                manager.CreateNewEntity<Audittrail>(ref trail);

                return manager.Detach<Audittrail>(trail)
                                            .With<StackTrace>()
                                            .Detach();
            }
        }


The following are the definitions of the entities that we are passing via the WCF.

AuditTrail Class
    [Entity("audittrail")][DataContract(IsReference = true)]
    public partial class Audittrail {
        
        private string message;
        
        private string methodname;
        
        private string action;
        
        private string username;
        
        private int persistentkey1;
        
        private IList<StackTrace> stacktraces;
        
        [Field("Message")][DataMember]
        public virtual string Message {
            get {
                return message;
            }
            set {
                this.message = value;
            }
        }
        
        [Field("MethodName")][DataMember]
        public virtual string MethodName {
            get {
                return methodname;
            }
            set {
                this.methodname = value;
            }
        }
        
        [Field("Action")][DataMember]
        public virtual string Action {
            get {
                return action;
            }
            set {
                this.action = value;
            }
        }
        
        [Field("UserName")][DataMember]
        public virtual string UserName {
            get {
                return username;
            }
            set {
                this.username = value;
            }
        }
        
        [Key("PersistentKey1", AutoKey=true)][DataMember]
        public virtual int PersistentKey1 {
            get {
                return persistentkey1;
            }
            set {
                this.persistentkey1 = value;
            }
        }
        
        [OneToMany(typeof(StackTrace), RelationColumn = "Audittrail_Id1", Cascade = Cascade.ALL)][DataMember]
        public virtual IList<StackTrace> StackTraces {
            get {
                return stacktraces;
            }
            set {
                this.stacktraces = value;
            }
        }
    }

Stack Trace class

Last edited Feb 23, 2010 at 2:03 PM by ahmedsalako, version 9

Comments

No comments yet.