PageRenderTime 71ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/DataService/service/basic/BaseServiceImpl.cs

https://github.com/panmingzhi815/SchoolManagementSystem
C# | 85 lines | 78 code | 7 blank | 0 comment | 3 complexity | e422e124f8bc6a8ed2de8ed924f5f243 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NHibernate;
  6. using DataService.util;
  7. using Domain.Entities;
  8. using NHibernate.Criterion;
  9. using System.Reflection;
  10. namespace DataService.service.basic
  11. {
  12. public abstract class BaseServiceImpl : BaseService
  13. {
  14. #region BaseService Members
  15. public Object get(Type t, string id)
  16. {
  17. using (ISession session = getSession())
  18. {
  19. return session.Get(t, id);
  20. }
  21. }
  22. public void save(object obj)
  23. {
  24. using (ISession session = getSession())
  25. {
  26. ITransaction tx = null;
  27. try
  28. {
  29. tx = session.BeginTransaction();
  30. PropertyInfo property = obj.GetType().GetProperty("Id");
  31. Object Id = property.GetValue(obj, null);
  32. if (Id == null || Id.Equals(""))
  33. {
  34. session.Save(obj);
  35. }
  36. else
  37. {
  38. session.Update(obj);
  39. }
  40. tx.Commit();
  41. }
  42. catch (Exception e)
  43. {
  44. throw new ServiceException("保存失败!" + e.Message);
  45. tx.Rollback();
  46. }
  47. }
  48. }
  49. public void del(object obj)
  50. {
  51. using (ISession session = getSession())
  52. {
  53. ITransaction tx = null;
  54. try
  55. {
  56. tx = session.BeginTransaction();
  57. session.Delete(obj);
  58. tx.Commit();
  59. }
  60. catch (Exception e)
  61. {
  62. tx.Rollback();
  63. throw new ServiceException("删除失败!" + e.Message);
  64. }
  65. }
  66. }
  67. public ISession getSession()
  68. {
  69. return NHibernateHelper.GetSession();
  70. }
  71. public int getCount(ICriteria c)
  72. {
  73. c.SetProjection(Projections.RowCount());
  74. return (int)c.UniqueResult();
  75. }
  76. #endregion
  77. }
  78. }