DAL/NHibernateSample.cs

(完整代码) 1KB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//添加以下命名空间
using DomainModel.Entities;
using NHibernate;
using NHibernate.Cfg;

namespace DAL
{
    public class NHibernateSample
    {
        private ISession _session;
        public ISession Session
        {
            set
            {
                _session = value;
            }
        }
        public NHibernateSample(ISession session)
        {
            _session = session;
        }
        private static ISession GetSession()
        {
            ISessionFactory sessionFactory = (new Configuration()).Configure().BuildSessionFactory();
            ISession session = sessionFactory.OpenSession();
            return session;
        }

        public Customer GetCustomerById(int customerId)
        {
            //Configuration config = new Configuration();
            //config.Configure();
            //ISessionFactory sessionFactory = config.BuildSessionFactory();
            //ISession session = sessionFactory.OpenSession();
            //return (Customer)session.Get(typeof(Customer), customerId);
            return _session.Get<Customer>(customerId);
        }
        public Customer GetCustomerById2(int customerId)
        {
            ISessionFactory sessionFactory = (new Configuration()).Configure().BuildSessionFactory();
            ISession session = sessionFactory.OpenSession();
            return session.Get<Customer>(customerId);
        }
        //标准写法
        public Customer GetCustomerById3(int customerId)
        {
            return _session.Get<Customer>(customerId);
        }
    }
}

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。