/// <summary> /// /// </summary> /// <param name="invocation"></param> /// <param name="arguments"></param> /// <returns></returns> public object Intercept(IInvocation invocation, params object[] arguments) { Object result = null; #region Logging if (_logger.IsDebugEnabled) { _logger.Debug("Dao Proxy call to " + invocation.Method.Name); } #endregion if (_passthroughMethods.Contains(invocation.Method.Name)) { try { result = invocation.Method.Invoke(_daoImplementation.DaoInstance, arguments); } catch (Exception e) { throw UnWrapException(e, invocation.Method.Name); } } else { DaoManager daoManager = _daoImplementation.DaoManager; if (daoManager.IsDaoSessionStarted()) { try { result = invocation.Method.Invoke(_daoImplementation.DaoInstance, arguments); } catch (Exception e) { throw UnWrapException(e, invocation.Method.Name); } } else { #region Logging if (_logger.IsDebugEnabled) { _logger.Debug("Dao Proxy, Open a connection "); } #endregion // Open a connection try { daoManager.OpenConnection(); result = invocation.Method.Invoke(_daoImplementation.DaoInstance, arguments); } catch (Exception e) { throw UnWrapException(e, invocation.Method.Name); } finally { daoManager.CloseConnection(); } } } #region Logging if (_logger.IsDebugEnabled) { _logger.Debug("End of proxyfied call to " + invocation.Method.Name); } #endregion return(result); }
/// <summary> /// /// </summary> /// <param name="invocation"></param> /// <param name="arguments"></param> /// <returns></returns> public object Intercept(IInvocation invocation, params object[] arguments) { Object result = null; #region Logging if (_logger.IsDebugEnabled) { _logger.Debug("Dao Proxy call to " + invocation.Method.Name); } #endregion bool hasNoDaoSessionAttributeDefined = invocation.Method.IsDefined( typeof(DAOMethodAttribute), true); if (_passthroughMethods.Contains(invocation.Method.Name) || hasNoDaoSessionAttributeDefined) { try { result = invocation.Method.Invoke(_daoImplementation.DaoInstance, arguments); } catch (Exception e) { throw UnWrapException(e, invocation.Method.Name); } } else { DaoManager daoManager = _daoImplementation.DaoManager; if (daoManager.IsDaoSessionStarted()) { try { result = invocation.Method.Invoke(_daoImplementation.DaoInstance, arguments); } catch (Exception e) { throw UnWrapException(e, invocation.Method.Name); } } else { #region Logging if (_logger.IsDebugEnabled) { _logger.Debug("Dao Proxy, Open a connection "); } #endregion // Open a connection try { daoManager.OpenConnection(); result = invocation.Method.Invoke(_daoImplementation.DaoInstance, arguments); } catch (Exception e) { System.Diagnostics.Trace.WriteLine(invocation.Method.Name + ":" + e.Message); throw UnWrapException(e, invocation.Method.Name); } finally { try { //if (daoManager.IsDaoSessionStarted() && // daoManager.LocalDaoSession.Connection !=null) daoManager.CloseConnection(); } catch { } } } } #region Logging if (_logger.IsDebugEnabled) { _logger.Debug("End of proxyfied call to " + invocation.Method.Name); } #endregion return(result); }
public void TestCreateUser() { IUserDao userDao = (IUserDao)daoManager2[typeof(IUserDao)]; User newUser = new User(); newUser.Id = "joe_cool"; newUser.UserName = "******"; newUser.Password = "******"; newUser.EmailAddress = "*****@*****.**"; newUser.LastLogon = DateTime.Now; try { daoManager2.OpenConnection(); userDao.Create(newUser); } catch (Exception e) { // Ignore Console.WriteLine("TestCreateUser, error cause : " + e.Message); } finally { daoManager2.CloseConnection(); } DateTime stamp = DateTime.Now; User joeCool = null; try { // open another session to retrieve the just inserted user daoManager2.OpenConnection(); //The User object you get back is live! joeCool = userDao.Load("joe_cool"); Assert.IsNotNull(joeCool); Assert.AreEqual("Joseph Cool", joeCool.UserName); //Change its properties and it will get persisted to the database on Close. // set Joe Cool's Last Login property joeCool.LastLogon = stamp; } catch (Exception e) { // Ignore Console.WriteLine("TestCreateUser, error cause : " + e.Message); } finally { // flush the changes from the Session to the Database daoManager2.CloseConnection(); } daoManager2.OpenConnection(); //The User object you get back is live! joeCool = userDao.Load("joe_cool"); daoManager2.CloseConnection(); Assert.IsNotNull(joeCool); Assert.AreEqual("Joseph Cool", joeCool.UserName); Assert.AreEqual(stamp.ToString(), joeCool.LastLogon.ToString()); }