darth_phoenixx
darth_phoenixx

Reputation: 952

Can I pass a parameter to ISessionFactory in NHibernate?

I am very new (i.e. an hour or so) to NHibernate. I've followed a tutorial which gave me the following class:

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();
    }
}

In extending my application, I now have another class for a different object. I'm trying to rewrite the above class so that I can pass in the assembly type as a parameter and return the _sessionFactory. So, for example, I would have a variable passed in to the method called assembly. The code would then be:

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;
        }
    }
}

This is giving the error 'Cannot resolve symbol 'get'' - presumably because I cannot pass any parameters in this way.

I am probably missing something very simple - any ideas?

Upvotes: 0

Views: 990

Answers (2)

Jamie Ide
Jamie Ide

Reputation: 49261

You don't need to make any changes if your other class is in the same assembly as CRMData.Objects.Contact.

But if you want to pass in a parameter you need to convert the SessionFactory property to a method or create a constructor that accepts the parameter.

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;
}

Upvotes: 1

Ashley John
Ashley John

Reputation: 2453

we dont have parameter properties in C# other than indexers.That is the reason you are getting the error.change your code to use a method.

But i still dont understand why you need to pass an assembly to the method.

Upvotes: 0

Related Questions