zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }


  • 相关阅读:
    span里设置高度
    Tensorflow2.0语法
    Python-OpenCV中的filter2D()函数
    解决安装tensorflow等速度慢,超时
    keras绘图之pydot和graphviz的导入( pydot` failed to call GraphViz)
    天天向上
    time库
    PythonDraw及turtle库
    TempComvert及-eval的用法
    百分号字符串拼接
  • 原文地址:https://www.cnblogs.com/hubcarl/p/1706397.html
Copyright © 2011-2022 走看看