Back to previous

Using Generic Method for Standard Add/ Delete in ORM

Telerik ORM (Open Access Mapping) has been a very useful tool, for mapping database structures into object oriented classes, so called Persistent Classes. This saves a lot of efforts in writing up these classes manually. What's more we could add on to it is to use generic methods on standard adding and deleting transaction.

        /// <summary>
        /// A generic method for adding an object
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ObjectRef"></param>
        /// <returns></returns>
       public static int Add<T>(ref T ObjectRef)
        {
            IObjectScope scope = ObjectScopeProvider.ObjectScope();
            scope.Transaction.Begin();
            scope.Add(ObjectRef);
            scope.Transaction.Commit();
            return Convert.ToInt16(scope.GetObjectId(ObjectRef).ToString());
        }

        /// <summary>
        /// A generic method for deleting an object
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ObjectRef"></param>
        public static void Delete<T>(ref T ObjectRef)
        {
            IObjectScope scope = ObjectScopeProvider.ObjectScope();
            scope.Transaction.Begin();
            scope.Remove(ObjectRef);
            scope.Transaction.Commit();
        }
 

ShareThis

If you think this post is useful, please recommend me at the bottom of the page. ;)

Discussion