• 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);
            }
        }
    }


  • 相关阅读:
    mybatis 绑定 statement 失败
    JDBC链接Mysql失败
    Mysql 链接数据库时区错误
    mybatis 延迟加载
    C++ 虚函数表解析
    运行错误:error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or
    QComboBox的activated与currentIndexChanged的区别
    QT 文件对话框(QFileDialog)
    VS2010 ERROR:c1xx fatal error c1083
    django 在字符串[str(list)]中精确查找
  • 原文地址:https://www.cnblogs.com/hubcarl/p/1706397.html
Copyright © 2020-2023  润新知