uNHAddins.Examples.SessionManagement with Ninject

179 views
Skip to first unread message

Diego Jancic

unread,
Mar 16, 2010, 3:19:54 PM3/16/10
to uNhAddIns
Hi People!,

I'm trying to use the example available in
http://unhaddins.googlecode.com/svn/trunk/Examples/uNHAddins.Examples.SessionManagement/,
but the problem is that the configurations in http://bit.ly/ajUilx are
hard to replace.

I'm successfully creating the SessionFactories using the
MultiSessionFactoryProvider, and also I've the NH configs with the
session_context_class configured:

<property
name="current_session_context_class">uNhAddIns.SessionEasier.Conversations.ThreadLocalConversationalSessionContext,
uNhAddIns</property>

After that, I've marked a class with [PersistenceConversational] and a
method with [PersistenceConversation], but it throws:

uNhAddIns.SessionEasier.Conversations.ConversationException: No
current conversation available. Create a conversation and bind it to
the container.

Of course!, I'm missing all the configuration the example has in the
above link (http://bit.ly/ajUilx). Can anyone tell how are those
options for and when they are injected??

Context: WinForms .NET 3.5
NH: 2.1.2GA
uNhAddins: Trunk recompiled for NH 2.1.2GA


Thanks anyone!,
Diego

José F. Romaniello

unread,
Mar 16, 2010, 3:28:57 PM3/16/10
to unha...@googlegroups.com
are you resolving the persistence class throught the container? :P


2010/3/16 Diego Jancic <jan...@gmail.com>

José F. Romaniello

unread,
Mar 16, 2010, 3:30:14 PM3/16/10
to unha...@googlegroups.com
I use the configure nhibernate and conversations thing fluently as in my example:


This is all related the configuration stuff related to nhibernate and cpbt.

2010/3/16 José F. Romaniello <jfroma...@gmail.com>

Gustavo Ringel

unread,
Mar 16, 2010, 3:42:30 PM3/16/10
to unha...@googlegroups.com
Diego, that is the "old way" for configuring.
Do you need all the XML or do you prefer fluent configuration?

I use this:

var container = new WindsorContainer()
.AddFacility<FactorySupportFacility>()
.AddFacility<PersistenceConversationFacility>()
.Register(Component.For<ISessionFactoryProvider>().ImplementedBy<SessionFactoryProvider>())
.Register(Component.For<ISessionFactory>().UsingFactoryMethod(
          () => IoC.Resolve<ISessionFactoryProvider>().GetFactory(null)))
.Register(Component.For<ISessionWrapper>().ImplementedBy<SessionWrapper>())
.Register(Component.For<IConversationFactory>().ImplementedBy<DefaultConversationFactory>())
.Register(Component.For<IConversationsContainerAccessor>().ImplementedBy<NhConversationsContainerAccessor>());

In the hibernate.cfg.xml you should use the proper context:

<property name="current_session_context_class">
uNhAddIns.SessionEasier.Conversations.ThreadLocalConversationalSessionContext, uNhAddIns
</property>

And that's all...

Gustavo Ringel

unread,
Mar 16, 2010, 3:44:32 PM3/16/10
to unha...@googlegroups.com
Ah, if you want to see an example with the multisession fluent configuration i created one in:


Gustavo.

José F. Romaniello

unread,
Mar 16, 2010, 8:36:41 PM3/16/10
to unha...@googlegroups.com
I didn't see the "ninject" world in the subject, sorry.
There is not an example of how you can do to use CpBT with ninject. Because in our examples we use castle IOC and AOP compatibility.
However, you can use something as a factory in the container to create cpbt proxies by hand...



2010/3/16 Gustavo Ringel <gustavo...@gmail.com>

Diego Jancic

unread,
Mar 16, 2010, 11:12:50 PM3/16/10
to unha...@googlegroups.com
I took a quick look at the examples you sent, and it looks like there's always the Windsor Container. Anyway, I'll look them further tomorrow.

Jose, I guess I don't completely understand when you say "something as a factory in the container"... Which factory are you talking about and where should it be??

Thanks!

José F. Romaniello

unread,
Mar 17, 2010, 6:57:21 AM3/17/10
to unha...@googlegroups.com
var proxyGenerator = new ProxyGenerator();  //Proxy generator is from castle.DynamicProxy since ninject has not aop.

new InlineModule(
   m => m.Bind<IMiModelo>().ToFactoryMethod( () => proxyGenerator.CreateInterfaceProxyBlaBlaBla(the interface, the class, the interceptoer, the donno what));
)


will be really hard......................

Can you use PostSharp 1.5 in your project?, because I'm working on an adapter, and will be fairly more easy.

You only have to setup the attribute and there is not any container requirement.


contact me via gtalk




2010/3/17 Diego Jancic <jan...@gmail.com>

Diego Jancic

unread,
Mar 17, 2010, 9:04:24 AM3/17/10
to unha...@googlegroups.com
Thanks Jose,
Don't worry, I thought it would be easy. I'm using PostSharp, but I also could use Castle. It's just I'm using NH with LinFu and Ninject, I didn't want to setup another container.

I'll keep it simple.

Thanks both of you guys!
Diego

Diego Jancic

unread,
Mar 17, 2010, 10:38:36 AM3/17/10
to unha...@googlegroups.com
Not, it's not easy since I'm not using the Castle ByteCode.
Is it possible to set it up using the LinFu ? PostSharp is also OK.

José F. Romaniello

unread,
Mar 17, 2010, 10:54:47 AM3/17/10
to unha...@googlegroups.com
There are three implementations of Conversation per Business Transaction, in the trunk:

-Castle (need castle dynamicproxy and castle windsor)
-Spring ( need spring.ioc and spring.aop)
-Postsharp (container agnostic): You need to setup the same thing as we mention before on any kind of container, and then setup the container as a servicelocator.


2010/3/17 Diego Jancic <jan...@gmail.com>

José F. Romaniello

unread,
Mar 17, 2010, 10:56:31 AM3/17/10
to unha...@googlegroups.com
For postsharp implementation, instead of using "PersistenceConversation" you have to use PsPersistenceConversation.

2010/3/17 José F. Romaniello <jfroma...@gmail.com>

Diego Jancic

unread,
Mar 17, 2010, 3:06:36 PM3/17/10
to unha...@googlegroups.com
Well.. I'm stuck.
I'm trying to set it up with PostSharp, Ninject and LinFu.

Attached is the Ninject configuration.

Sample repository:

    public class ModelRepository : IModelRepository
    {
        private readonly ISessionFactory sessionFactory;

        public ModelRepository(ISessionFactory sessionFactory)
        {
            this.sessionFactory = sessionFactory;
        }

        public T Get<T>(Guid id)
        {
            return Session.Get<T>(id);
        }
// Other methods removed...
        private ISession Session
        {
            get { return sessionFactory.GetCurrentSession();}
        }
    }


And here is how it's used:

    [PsPersistenceConversational]
    public class TheModel
    {
        [PersistenceConversation]
        public void SavesEntities()
        {
            var student = new Student { FirstName = "John", LastName = "Williams", PersonNumber = "admin" };
            ServiceLocator.Get<IModelRepository>()
                .Save(student);
            // More....
        }
    }

At the end of the method TheModel.SavesEntities, it throws an exception (in the PostSharp injected code):


uNhAddIns.SessionEasier.Conversations.ConversationException: No current conversation available. Create a conversation and bind it to the container.
at uNhAddIns.SessionEasier.Conversations.ThreadLocalConversationalSessionContext.CurrentSession()
    in D:\Projects\uNHAddins-Trunk\uNhAddIns\uNhAddIns\SessionEasier\Conversations\ThreadLocalConversationalSessionContext.cs: line 28
at NHibernate.Impl.SessionFactoryImpl.GetCurrentSession()
at AccuDataX.Repositories.NHibernate.Impl.ModelRepository.get_Session() in ModelRepository.cs: line 46
at AccuDataX.Repositories.NHibernate.Impl.ModelRepository.Save(IEntity entity) in ModelRepository.cs: line 30
at AccuDataX.Tests.Repositories.TheModel.SavesEntities() in ModelRepositoryFixture.cs: line 47
at AccuDataX.Tests.Repositories.ModelRepositoryFixture.SavesEntities() in ModelRepositoryFixture.cs: line 19



Thanks for any tip
Diego
InjectionModule.cs

José F. Romaniello

unread,
Mar 17, 2010, 3:43:34 PM3/17/10
to unha...@googlegroups.com
What hapen if you use a SessionFactoryProvider a ISessionFactoryProvider (instead the multisessionfactoryprovider):

new SessionFactoryProvider(new DefaultSessionFactoryConfigurationProvider())

Something like this?

Otherwise could you send me a small project to see?

2010/3/17 Diego Jancic <jan...@gmail.com>

Diego Jancic

unread,
Mar 17, 2010, 4:40:22 PM3/17/10
to unha...@googlegroups.com
I need to use multiple session factories, but I should start trying to
set only one.
I'll see and if it doesn't work I'll send the proyect.

Thanks!

José F. Romaniello

unread,
Mar 17, 2010, 5:43:05 PM3/17/10
to unha...@googlegroups.com
Diego, I found a bug in one of my last commits... regarding to ninject.

Ninject can resolve a type that is not currently registered..... I'll fix right now. Let me know if this fix your bug.

2010/3/17 Diego Jancic <jan...@gmail.com>

José F. Romaniello

unread,
Mar 17, 2010, 6:07:11 PM3/17/10
to unha...@googlegroups.com
Done, checkout the last version.

Just in case you want to test the model, without aspect.... you will have to register an instance of NoopConversationalMarker: as
new NoopConversationalMarker{Noop = true} ... in a servicelocator adapter.


You can use a light weight SL as described here:

or the ninject one.

José F. Romaniello

unread,
Mar 18, 2010, 9:05:33 AM3/18/10
to unha...@googlegroups.com
I've made a little example application with your stack:

Diego Jancic

unread,
Mar 18, 2010, 9:44:49 AM3/18/10
to unha...@googlegroups.com
Aaaaaaawesome!! =)

I've checked out the latest version and it works without any modification.

The example was not needed, but thanks anyway for all your work. I really appreciate it!

Abrazo! ;-)
Diego

José F. Romaniello

unread,
Mar 18, 2010, 9:54:27 AM3/18/10
to unha...@googlegroups.com
You're welcome, few things:
-register IConversationsContainerAccessor, IConversationFactory, ISessionFactoryProvider as singleton.
-the postsharp doesn't need IConversationalMetaInfoStore.
-if you set the class just with [PsPersistenceConversational], the default Include Mode = Implicit. This means that all PUBLIC methods (except constructors) are include in conversation, with the default end mode (which default value is Continue).

You can include private members in the conversation.

funny thing is that I started this adapter one week ago.



funny thing, I started the postsharp version one week ago. still work in progress, 

2010/3/18 Diego Jancic <jan...@gmail.com>

Diego Jancic

unread,
Mar 18, 2010, 9:59:16 AM3/18/10
to unha...@googlegroups.com
Changes done. Thanks again.


>> funny thing is that I started this adapter one week ago.

funny thing is that I was delaying the NH configurations in the project because I was lazy to understand CpBT. We're in sync =)

José F. Romaniello

unread,
Mar 23, 2010, 7:25:07 PM3/23/10
to unha...@googlegroups.com
Diego, this is the mandatory first post:

There is a little change in the trunk version, you should only use the [PsPersistenceConversational] attribute with the postsharp version.

There is not a MethodsIncludeMode = Implicit, if you want to include the publics methods implictly you simple mark the class and exclude those methods that you don't want.

If you want to include publics methods explictly don't put the attribute at class level, just in each method.




2010/3/18 Diego Jancic <jan...@gmail.com>

Diego Jancic

unread,
Mar 23, 2010, 7:43:09 PM3/23/10
to unha...@googlegroups.com
Cool, nice post!
I was thinking to write a post about it, using Ninject... Someday I'll do it (I'm lazy)
To unsubscribe from this group, send email to unhaddins+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply all
Reply to author
Forward
0 new messages