Question about initializing a Sharplite Repository from a Sharplite Newb

15 views
Skip to first unread message

Matt Maranda

unread,
Jan 21, 2015, 4:32:04 PM1/21/15
to sharp...@googlegroups.com
Hi all,
I’ve run into a major stumbling block with a project I inherited that is using S#arp Lite. The problem seems to be that all the repositories I create have a null session. Here is an example of how I am creating the session factory, the repository, and the sample call that breaks everything.

Create Factory:
NHibernate.ISessionFactory factory = NHibernateInitializer.Initialize().BuildSessionFactory();

Because this is an ISessionFectory being created the session is null. Calling factory.OpenSession() will return a new session but does not alter the session on the factory.

Create Repository:
Repository<Person> personRepository = new Repository<Person>(factory);
I’ve included the repository at the end of this post, but it doesn’t do much of anything at this time. Most of the mapping has been handled by fluent.nhibernate and castle windsor.

Call that fails
var getAllResult = personRepository .GetAll();

What I can see by digging into S#arp Lite’s documentation on GIT is that the the Session is being set to the SessionsFactory’s current session.
SharpLite.NHibernateProvider
{
public class Repository<T> : RepositoryWithTypedId<T, int>, IRepository<T> where T : class
{
public Repository(ISessionFactory sessionFactory) : base(sessionFactory) { }
}
public class RepositoryWithTypedId<T, TId> : IRepositoryWithTypedId<T, TId> where T : class
{
public RepositoryWithTypedId(ISessionFactory sessionFactory) {
if (sessionFactory == null) throw new ArgumentNullException("sessionFactory may not be null");
_sessionFactory = sessionFactory;
}
protected virtual ISession Session {
get {
return _sessionFactory.GetCurrentSession();
}
}

Because this is an ISessionFactory without a session created at the time of initialization the session is always null. What I don’t know how to do is initialize an ISessionFactory that has an actual session at the time of initialization.

Repository
public class Repository<T> : SharpLite.NHibernateProvider.Repository<T>, IRepository<T> where T : class
{
public Repository(ISessionFactory sessionFactory) : base(sessionFactory)
{

}
}

Thank you for any help or insight you can impart

Billy McCafferty

unread,
Jan 23, 2015, 3:37:55 PM1/23/15
to sharp...@googlegroups.com
Have you tried creating a new S#arp Lite project using the Templify project at https://github.com/codai/Sharp-Lite/tree/master/Template ?

That should give you a fully working project to compare to.

Billy McCafferty


--
You received this message because you are subscribed to the Google Groups "S#arp Lite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sharp-lite+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages