Click here to Skip to main content
15,892,059 members
Articles / Web Development / ASP.NET

Pretty URLs, Separation of Layers and O/R Mapping in Web Forms ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.47/5 (14 votes)
12 Sep 200732 min read 87K   493   87  
Implementing multi-tier architecture in a web application using ASP.NET 2.0
using System;
using System.Collections.Generic;
using Routing.Module;
using NHibernate;
using NHibernate.MapClasses;
using NHibernate.Mapping;

/// <summary>
/// Summary description for OrderToShip
/// </summary>
namespace MVC.Models
{
    public class OrdersToShip : IOrders
    {

        //----------------------

        public OrdersToShip()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public IList<Order> FindAllToBeShipped()
        {
            ISession session = RoutingClass.NHibernateCurrentSession;
            IQuery query;
            IList<Order> list;
            using (session.BeginTransaction())
            {
                query = session.CreateQuery("from Order ord where ord.Shipped_At is null");
                list = query.List<Order>();
                session.Transaction.Commit();
            }
            return list;
        }

        public IList<Order> FindAllShipped()
        {
            ISession session = RoutingClass.NHibernateCurrentSession;
            IQuery query;
            IList<Order> list;
            using (session.BeginTransaction())
            {
                query = session.CreateQuery("from Order ord where ord.Shipped_At is not  null");
                list = query.List<Order>();
                session.Transaction.Commit();
            }
            return list;
        }

        public IList<QuantityTitle> ShowLineItems(int id)
        {
            ISession session = RoutingClass.NHibernateCurrentSession;
            IList<QuantityTitle> list;
            using (session.BeginTransaction())
            {

                // We have Product, LineItem, Order
                // Order has many LineItems---one-to-many relation
                // Product and LineItems are not related, but 
                //Line items store the Product_Id
                //I am quering here a data that are related and not 
                // at the same time
                //therefore I had to create new type  QuantityTitle
                // which i was forced to place in the hbm file
                // even  if it is not persistent class
                // otherwrise NNhibernate cannot find a class
                // First I get all LineItems  from order
                // for a given :id===used inner join
                // for one-to-many relation
                // then I need  Product.Title for a given
                // LineItem--- I cannot use inner join 
                // because they are not related, I simply say
                // I want P.Tiltle where P.Id is equal something
                // at the and I could get LineItem.Quantity and Product.Title

                IQuery query = session.CreateQuery(@"select new  NHibernate.MapClasses.QuantityTitle(LineItem.Quantity,P.Title)
                                                 from 
                                                    Order O inner join O.LineItems as LineItem,
                                                    Product P
                                                 where         
                                                  O.Id=:id 
                                                  and
                                                  LineItem.Product_Id=P.Id ");


                query.SetParameter("id", id);
                list = query.List<QuantityTitle>();
                session.Transaction.Commit();
            }
            return list;
        }


        public int RemoveOrders(List<int> ListOfIds)
        {
            ISession session = RoutingClass.NHibernateCurrentSession;
            using (session.BeginTransaction())
            {
                // noe need to retrive first--use delete(string query)
                //leftovers from retriving frist technique
                //IQuery query = session.CreateQuery("from Order ord  where ord.Id in(:ListOfIds) ");
                // query.SetParameterList("ListOfIds", ListOfIds);
                try
                {
                    foreach (Int32 SingleId in ListOfIds)
                    {
                        session.Delete("from Order ord where ord.Id =?", SingleId, NHibernateUtil.Int32);
                    }
                    session.Transaction.Commit();
                    return 1;
                }

                catch (TransactionException) { return 0; }
            }
        }


        public int ShipOrders(List<int> ListOfIds)
        {
            //this method was rewritten so we marke
            //shipped orders at one  call to nhibernate
            // previously we did that marking order by order
            // very inefficient

            ISession session = RoutingClass.NHibernateCurrentSession;
            using (session.BeginTransaction())
            {
                IQuery query = session.CreateQuery("from Order ord  where ord.Id in(:ListOfIds) ");
                query.SetParameterList("ListOfIds", ListOfIds);
                IList<Order> ListOfOrdersToShip = query.List<Order>();

                foreach (Order SingleOrder in ListOfOrdersToShip)
                {
                    SingleOrder.Shipped_At = DateTime.Now;
                }

                try
                {
                    session.Transaction.Commit();
                    return 1;
                }
                catch (TransactionException) { return 0; }
            }
        }
    }
}


        


    

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer iPay, Elizabethtown,Ky
United States United States
After defending his PhD thesis in 2005 (computational nuclear physics) at Vanderbilt in Nashville, the author decided to pursue a career in software development. As a long time open source advocate, he started with writing web applications using Linux-Apache-MySql-P (LAMP) framework. After that experience, he decided to embrace Microsoft technologies.
Currently working as a web developer in .NET platform in the online payments company.

Comments and Discussions