• NHibernate开源框架Cuyahoga学习之数据访问泛型约束的实现


    代码
    //泛型约束接口
    using System;
    using System.Collections.Generic;
    using Cuyahoga.Core.Domain;
    using NHibernate.Criterion;
    namespace Cuyahoga.Core.DataAccess
    {
        
    public interface IContentItemDao<T> where T : IContentItem
        {
            T GetById(
    long id);
            T GetById(Guid id);
            IList
    <T> GetAll();
            IList
    <T> GetBySite(Site site);
            T Save(T entity);
            
    void Delete(T entity);
        }
    }
    //接口实现
    using System;
    using System.Collections.Generic;
    using Cuyahoga.Core.Domain;
    using NHibernate;
    using NHibernate.Criterion;
    using Castle.Facilities.NHibernateIntegration;
    using Castle.Services.Transaction;
    namespace Cuyahoga.Core.DataAccess
    {
        [Transactional]
        
    public class ContentItemDao<T> : IContentItemDao<T> where T : IContentItem
        {
            
    protected readonly ISessionManager SessionManager;
            
    protected readonly Type PersistentType = typeof(T);
            
    public ContentItemDao(ISessionManager sessionManager)
            {
                
    this.SessionManager = sessionManager;
            }
            
    protected ISession GetSession()
            {
                
    return this.SessionManager.OpenSession();
            }
            
    public T GetById(long id)
            {
                
    return this.GetSession().Get<T>(id);
            }
            
    public T GetById(Guid id)
            {
                
    return this.GetSession().Get<T>(id);    
    }
            
    public IList<T> GetAll()
            {
                ICriteria criteria 
    = this.GetSession().CreateCriteria(PersistentType);
                
    return criteria.List<T>();
            }
            
    public IList<T> GetBySite(Site site)
            {
                ICriteria criteria 
    = this.GetSession().CreateCriteria(PersistentType)
                    .CreateCriteria(
    "Section""s")
                        .Add(Expression.Eq(
    "Site", site));
                
    return criteria.List<T>();
            }
            [Transaction(TransactionMode.Requires)]
            
    public T Save(T entity)
            {
                
    this.GetSession().SaveOrUpdate(entity);
                
    return entity;
            }
            [Transaction(TransactionMode.Requires)]
            
    public void Delete(T entity)
            {
                
    this.GetSession().Delete(entity);
            }
        }
    }


  • 相关阅读:
    oracle的优化-----学习笔记
    面试题-------笔记
    HTTP-1.初相识:了解HTTP协议
    Python-Basis-6th
    Python-Basis-5th
    Python-Basis-4th
    Python-Basis-3rd
    Python-Basis-2nd
    Python-Basis-1st
    scala面向对象编程
  • 原文地址:https://www.cnblogs.com/hubcarl/p/1706397.html
Copyright © 2020-2023  润新知