Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处理ISession之外的延迟加载?_C#_Nhibernate - Fatal编程技术网

C# 处理ISession之外的延迟加载?

C# 处理ISession之外的延迟加载?,c#,nhibernate,C#,Nhibernate,我正在使用NHibernate在我的应用程序中进行数据库访问。我的ISessions没有持久性,我对此很满意,因为它使我更容易将应用程序划分为不同的层。唯一的困难是以一种好的方式处理延迟加载 我有一个模型类,如下所示: public class User { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual Country CountryO

我正在使用NHibernate在我的应用程序中进行数据库访问。我的
ISession
s没有持久性,我对此很满意,因为它使我更容易将应用程序划分为不同的层。唯一的困难是以一种好的方式处理延迟加载

我有一个模型类,如下所示:

public class User {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Country CountryOfBirth { get; set }
    public virtual Country CountryOfResidence {get; set; }
}
    Country countryOfBirth;
    public virtual Country CountryOfBirth{
        get
        {
            if (country is INHibernateProxy)
                countryOfBirth = CountryRepository.GetById(countryOfBirth.Id);
            return countryOfBirth;
        }
        set { countryOfBirth = value; }
    }
目前,我已将
CountryOfBirth
CountryOfResidence
设置为
fetch=“join”
。但是,由于我数据库中的国家列表大多是静态的,因此我希望缓存这些值。我将
CountryOfBirth
属性更改为如下所示:

public class User {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Country CountryOfBirth { get; set }
    public virtual Country CountryOfResidence {get; set; }
}
    Country countryOfBirth;
    public virtual Country CountryOfBirth{
        get
        {
            if (country is INHibernateProxy)
                countryOfBirth = CountryRepository.GetById(countryOfBirth.Id);
            return countryOfBirth;
        }
        set { countryOfBirth = value; }
    }
然而,它要求我的模型类知道它正被NHibernate使用,这破坏了封装

有没有更好的方法来实现这一点?例如,如果NHibernate尝试加载代理且会话已过期,是否有办法让它自动遍历我的存储库类


或者我应该使用其他方法吗?

如果您想添加缓存功能,请查看NHibernate二级缓存。检查本教程并搜索此主题。通过使用缓存,您不会使用任何NH代理、存储库等来污染您的模型