建模NHibernate的查询 [英] Modeling NHibernate queries
问题描述
通常我会把我的规定 - / HQL查询,在库/ DAL类相关的实体,但最近I'be一直在想补充说,重新presents查询是另一种抽象的,这会给我加入共同的行为在一个基类,等所有的查询(如分页)的可能性。
Usually I'd put my criterias/hql queries in a repository/dal class related to the entity, but lately I'be been thinking of adding another abstraction that represents what a query is, this would give me the possibility of adding common behavior to all queries (e.g. pagination) in a base class, etc.
所以这些都是我的组件现在;
so these are my components now;
不相关的NHibernate的通用接口:
generic interface not related to nhibernate:
public interface IQuery<T>
{
IList<T> List();
T Single();
}
例实施基于标准的查询,类似的事情可能与HQL语句来完成,或NHibernate的,LINQ查询
Example implementation of a Criteria based query, something similar could be done with an Hql query, or a nhibernate-linq query
public abstract class CriteriaQuery<T>: IQuery<T>
{
[Inject]
public ISessionFactory SessionFactory { protected get; set; }
protected ISession Session
{
get { return SessionFactory.GetCurrentSession(); }
}
protected abstract ICriteria Configure(ICriteria criteria);
[Transaction]
public virtual IList<T> List()
{
var criteria = Session.CreateCriteria(typeof (T));
return Configure(criteria)
.List<T>();
}
[Transaction]
public virtual T Single()
{
return Configure(Session.CreateCriteria(typeof(T)))
.UniqueResult<T>();
}
}
和这里的域特定的查询将如下所示:
and here a domain specific query would look like:
public interface IGetVideosQuery: IQuery<Video>
{
IGetVideosQuery Page(int index);
IGetVideosQuery PageSize(int pageSize);
IGetVideosQuery AllTime { get; }
IGetVideosQuery Today { get; }
IGetVideosQuery LastWeek { get; }
}
对此有何想法?你看到的可能出现的问题我可能会遇到? 谢谢!
any thoughts on this? possible problems you see I might come across? Thanks!
推荐答案
我选择了另一条路,那的 CQS 。这里做的事情是,它从我的查询逻辑分开我的不同诱变逻辑。
I took a different path, that of CQS. What this does is that it separates my mutating logic from my query logic.
现在,对于如何实现这个不同的想法,而我选择这一个:
Now, there are different ideas on how to implement this, and I choose this one:
-
我所有的变异逻辑是使用命令如
DeactivateUser
并启动ChangeRelationAddress
。对于这个业务逻辑,我有正常的存储库像你描述;
All my mutation logic is activated using command like
DeactivateUser
andChangeRelationAddress
. For this business logic, I have normal repositories like you describe;
有关数据显示,我用一个完全管理的系统。有了这个系统,我形容像规范模式查询。我介绍了基表和字段。该查询系统会自动创建联接为我和我提供的过滤器过滤器的定义。
For displaying data, I use a completely managed system. With this system, I describe queries like the Specification Pattern. I describes the base table and the fields. The query system automatically creates the joins for me and with filter definitions I provide filters.
这个系统可以让我保持我的仓库复杂了,因为我没有去想过滤器,用户可设置或 ORDER BY
的。使用标准,这个数据会自动显示系统创建过滤器
和处理分页我。
This system allows me to keep the complexity of my repositories down because I do not have to think about filters a user may set or ORDER BY
's. The system that displays this data automatically creates the filters using Criteria
and handles paging for me.
也许这样的系统可以为你工作。
Maybe such a system can work for you.
这篇关于建模NHibernate的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!