Monday, 25 April 2016

Creating Factory To Use Property Of Class Everywhere - SessionFactory

No comments
SessionFactory.CS -

 public class SessionFactory
    {
        private static SessionFactory _SessionFactory = new SessionFactory();
        private SessionFactory()
        {

        }
        public static SessionFactory Instance
        {
            get
            {
                return _SessionFactory;
            }
        }

      public Customer CurrentCustomer
        {
            get
            {
                if (HttpContext.Current.Session["user"] == null)
                {                  
                    return null;
                }
                return (Customer)HttpContext.Current.Session["user"];
            }
            set
            {
                HttpContext.Current.Session["user"] = value;
            }
        }
}


using SessionFactory.CS in controller
------------------------------------------------
 UnitOfWork uom = new UnitOfWork();
 uom.BeginTransaction();
 var updateCustomer = uom.Repository<SignUpRepository>();
 var resultCustomer = updateCustomer.Get(x => x.CustomerId ==  SessionFactory.Instance.CurrentCustomer.CustomerId);
 resultCustomer.CreditLimit = SessionFactory.Instance.CurrentCustomer.CreditLimit;
 updateCustomer.UpdateCustomer(resultCustomer);
 uom.Commit();