简体   繁体   English

NHibernate 存储库

[英]NHibernate Repository

Does anybody has proper and simplified write up on NHibernate Repository?是否有人在 NHibernate 存储库上进行了适当和简化的编写? I've used Java, Hibernate, LCDS DataService Repositories with FlexBuilder (using rtmp channelling) and want to implement the exact fundamental with C#.NET.我已经使用了 Java、Hibernate、LCDS DataService Repositories 和 FlexBuilder(使用 rtmp 通道),并希望使用 C#.NET 实现确切的基础。

I've gone through lots of online documentation but nothing was reflecting the exact use like with FlexBuilder.我浏览了很多在线文档,但没有什么能像 FlexBuilder 那样反映确切的用途。

If anybody has a small example application then do share.如果有人有一个小的示例应用程序,那么请分享。 That would be much helpful.那会很有帮助。

Regards Nitin问候尼丁

See these:见这些:

First Create an interface IRepository :首先创建一个接口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
}

The implementation of the ActiveSessionManager and SessionProvider is very simple and you can find it in the previous links. ActiveSessionManagerSessionProvider的实现非常简单,可以在前面的链接中找到。

You can expose your methods as following:您可以按如下方式公开您的方法:

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

Then:然后:

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.这是此模式的基本实现,但您可以根据您的设计驱动来装饰它。

You can go further, I recommend, after understanding these pattern and implement it, checking out NHibernate and the Unit of Work Pattern您可以进一步了解 go,我建议在了解这些模式并实施后,查看NHibernate 和工作单元模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM