我可以在NHibernate中将参数传递给ISessionFactory吗?

时间:2011-08-22 13:35:21

标签: c# nhibernate

我对NHibernate非常新(即一小时左右)。我按照教程给了我以下课程:

public class ContactNHibernateHelper
{
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
            {
                var configuration = new Configuration();
                configuration.Configure();
                configuration.AddAssembly(typeof (CRMData.Objects.Contact).Assembly);
                _sessionFactory = configuration.Configure().BuildSessionFactory();
            }
            return _sessionFactory;
        }
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
}

在扩展我的应用程序时,我现在有另一个类用于不同的对象。我正在尝试重写上面的类,以便我可以将程序集类型作为参数传递并返回_sessionFactory。因此,例如,我将一个变量传递给名为assembly的方法。那么代码就是:

public class GenericNHibernateHelper
{
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory(System.Reflection.Assembly assembly)
    {
        get
        {
            if (_sessionFactory == null)
            {
                var configuration = new Configuration();
                configuration.Configure();
                configuration.AddAssembly(assembly);
                _sessionFactory = configuration.Configure().BuildSessionFactory();
            }
            return _sessionFactory;
        }
    }
}

这是错误'无法解析符号'get'' - 大概是因为我不能以这种方式传递任何参数。

我可能遗漏了一些非常简单的东西 - 任何想法?

2 个答案:

答案 0 :(得分:1)

如果您的其他类与CRMData.Objects.Contact在同一个程序集中,则无需进行任何更改。

但是如果要传递参数,则需要将SessionFactory属性转换为方法或创建接受参数的构造函数。

private static ISessionFactory SessionFactory(System.Reflection.Assembly assembly)
{
    if (_sessionFactory == null)
    {
        var configuration = new Configuration();
        configuration.Configure();
        configuration.AddAssembly(assembly);
        _sessionFactory = configuration.Configure().BuildSessionFactory();
    }
    return _sessionFactory;
}

答案 1 :(得分:0)

我们在C#中没有索引器以外的参数属性。这就是你得到错误的原因。改变你的代码以使用方法。

但是我仍然不明白你为什么需要将程序集传递给方法。

相关问题