NHibernate的库 [英] NHibernate Repository

查看:245
本文介绍了NHibernate的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁有适当的和简单的写上NHibernate的存储库? 我用的Java,休眠,液晶显示器的DataService库与弹性生成(使用RTMP窜),并希望实现与C#.NET确切的根本。

Does anybody has proper and simplified write up on NHibernate Repository? I've used Java, Hibernate, LCDS DataService Repositories with FlexBuilder (using rtmp channelling) and want to implement the exact fundamental with C#.NET.

我通过大量的在线文档,但没有被反映喜欢与弹性生成准确的使用了。

I've gone through lots of online documentation but nothing was reflecting the exact use like with FlexBuilder.

如果任何人有一个小的示例应用程序,然后做的份额。这将是没什么太大的帮助。

If anybody has a small example application then do share. That would be much helpful.

问候 尼廷

推荐答案

请参阅以下:

库模式NHibernate的

首先创建一个接口 IRepository

public interface IRepository<T>
{
    int Add(T entity);
    void Delete(T entity);
    void Update(T entity);
    T GetById(int id);
    IEnumerable<T> FindAll(DetachedCriteria criteria);
    ...
    .
    .
    //
}

然后实现这个接口如下:

Then implement this interface as following:

 public class Repository<T> : IRepository<T>
{
    readonly IActiveSessionManager _activeSessionManager;
    protected ISession Session
    {
        get { return _activeSessionManager.GetActiveSession(); }
    }
    public Repository(IActiveSessionManager activeSessionManager)
    {
        _activeSessionManager = activeSessionManager;
    }
    public int Add(T entity)
    {
        int newId = (int)Session.Save(entity);
        Session.Flush();
        return newId;
    }
    .
    .
    // add the remaining implementations
}

ActiveSessionManager SessionProvider 是很简单的实现,你可以找到它在previous链接。

The implementation of the ActiveSessionManager and SessionProvider is very simple and you can find it in the previous links.

您可以公开你的方法如下:

You can expose your methods as following:

 public T FindOne(NHibernate.Criterion.DetachedCriteria criteria)
 {
     return criteria.GetExecutableCriteria(Session).UniqueResult<T>();
 }

然后:

public class EntityRepository : Repository<Entity>
{
    public EntityRepository(IActiveSessionManager activeSessionManger)
        : base(activeSessionManger)
    {
    }
    public Entity GetByName(string name)
    {
        var criteria = NHibernate.Criterion.DetachedCriteria.For<Entity>()
            .Add(Restrictions.Eq("name", name));
        return FindOne(criteria);
    }
    public IList<Entity> returnsomething()
    {
    }
    ....
}

这是一个基本的实现这种模式,但你可以装点它作为你的设计,你的驱动器。

This is a basic implementation for this pattern, but you can decorate it as your design drive you.

您可以走得更远,我建议,了解这些模式后,并执行它,检查出<一href="http://nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx">NHibernate工作模式和单位

You can go further, I recommend, after understanding these pattern and implement it, checking out NHibernate and the Unit of Work Pattern

这篇关于NHibernate的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆