26
2017
09

Photon_对NHibernate增删改查的封装_008


第一步:

使用单例模式封装一个Hibernate的一个工具类,增加对代码的重用。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
namespace YYEdu
{

    class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;
        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    var configuration = new Configuration();
                    configuration.Configure();
                    configuration.AddAssembly("YYEdu");
                    _sessionFactory = configuration.BuildSessionFactory();
                }
                return _sessionFactory;
            }

        }
        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }

    }
}

第二步:

定义一个用户管理类接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YYEdu.Model;
namespace YYEdu.Manager {
    interface IUserManager {
        void Add(User user);
        void Update(User user);
        void Remove(User user);
        User GetById(int id);
        User GetByUsername(string username);
        ICollection<User> GetAllUser();
        bool VerifyUser(string username,string passsword);
    }
}

第三步:

定义一个用户管理类,并集成用户管理类接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YYEdu.Model;
using NHibernate;
using NHibernate.Criterion;

namespace YYEdu.Manager
{
    class UserManager : IUserManager
    {
        public void Add(User user)
        {
            //第一种方式
            //ISession session = NHibernateHelper.OpenSession();
            //session.Save(user);
            //session.Close();

            //第二种方式 不用管理资源的释放
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using(ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(user);
                    transaction.Commit();
                }

            }
        }

        public ICollection<User> GetAllUser()
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                IList<User> users = session.CreateCriteria(typeof(User)).List<User>();

                return users;
            }
        }

        public User GetById(int id)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    User user = session.Get<User>(id);
                    transaction.Commit();
                    return user;
                }

            }
        }

        public User GetByUsername(string username)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ICriteria criteria = session.CreateCriteria(typeof(User));
                criteria.Add(Restrictions.Eq("Username",username));
                User user = criteria.UniqueResult<User>();
                return user;
            }
        }

        public void Remove(User user)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(user);
                    transaction.Commit();
                }

            }
        }

        public void Update(User user)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(user);
                    transaction.Commit();
                }

            }
        }

        //验证用户名是否存在
        public bool VerifyUser(string username, string passsword)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ICriteria criteria = session.CreateCriteria(typeof(User));
                criteria.Add(Restrictions.Eq("Username", username));
                criteria.Add(Restrictions.Eq("Password", passsword));
                User user = criteria.UniqueResult<User>();
                return user == null ? false : true;
            }
        }
    }
}

对以上方法的调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Cfg;
using YYEdu.Model;

using YYEdu.Manager;
namespace YYEdu {
    class Program {
        static void Main(string[] args)
        {
            //添加用户信息
            //User user = new User() { Username ="张三",Password="12456"};
            //IUserManager usermanager = new UserManager();
            //usermanager.Add(user);

            //更新用户的信息
            //User user = new User() {Id = 20, Username = "王五", Password = "12345" };
            //UserManager usermanager = new UserManager();
            //usermanager.Update (user);

            //根据Id号删除用户
            //User user = new User() { Id=20};
            //UserManager usermanager = new UserManager();
            //usermanager.Remove(user);

            //按Id号查询用户的名字
            //IUserManager userManager = new UserManager();
            //User user = userManager.GetById(19);
            //Console.WriteLine(user.Username);
            //Console.WriteLine(user.Password);

            //按用户名查找用户
            //IUserManager usermanager = new UserManager();
            //User user = usermanager.GetByUsername("杨勇");
            //Console.WriteLine(user.Username);
            //Console.WriteLine(user.Password);

            //查询所有的用户
            IUserManager userManager = new UserManager();
            ICollection<User> users = userManager.GetAllUser();
            foreach (User item in users)
            {
                Console.WriteLine(item.Username +" "+ item.Password);
            }

            //验证用户名和密码是否正确
            Console.WriteLine(userManager.VerifyUser("sdfs","123456"));
            Console.WriteLine(userManager.VerifyUser("杨勇","123456"));

        }
    }
}

上一篇:Photon_使用ExecuteReader读取数据库中的数据_003 下一篇:工厂设计模式 - 数据存储的特有方式