Hi

I have multithreaded application which works with many databases. We can 
say that each database is a different context. I use NHibernate's 
ISesssionFactory for database access. When ISessionFactory is initialized a 
new thread is created with just one object resolved within. This object is 
a MassTransit instance for handling message. When a message arrives mass 
transit creates a handler in new/reuse thread. The handler itself holds 
ISessionFactory dependency. As you can see I do not have an explicit 
context peer thread because the threads can be reused. I decided to use 
Scoped lifestyle for my components and the current flow is like that:

1) Session factory is initialized
2) Create a new task/thread; open a new Windsor scope with 
container.BeginScope(); Resolve and instance of AddinContext and set a 
DatabaseName property; Resolve MassTransit instance;
3) When message arrives MassTransit creates the handler.

The ISessionFactory is registerd like this:

Component.For<ISessionFactory>().LifestyleScoped().UsingFactoryMethod(k => 
k.Resolve<IMultiSessionFactoryProvider>().GetFactory(k.Resolve<DatabaseConfiguration>().dbname)),
Component.For<IMultiSessionFactoryProvider>().ImplementedBy<StatefulMultiSessionFactoryProvider>(),
Component.For<AddinContext>().LifestyleScoped(),

I also have subdependencyresolver registered:

public class AddinSettingsResolver : ISubDependencyResolver
    {
        private readonly IKernel kernel;
        public AddinSettingsResolver(IKernel kernel)
        {
            this.kernel = kernel;
        }

        public bool CanResolve(CreationContext context, 
ISubDependencyResolver contextHandlerResolver, ComponentModel model, 
DependencyModel dependency)
        {
            try
            {
                var addinContext = kernel.Resolve<AddinContext>();
                if (addinContext == null || 
String.IsNullOrWhiteSpace(addinContext.Database))
                    return false;

                string databaseName = addinContext.Database;
               //SomeLogicHere based on databaseName 

                return false;
            }
            catch
            {
                return false;
            }
        }

        public object Resolve(CreationContext context, 
ISubDependencyResolver contextHandlerResolver, ComponentModel model, 
DependencyModel dependency)
        {
            string databaseName = kernel.Resolve<AddinContext>().Database;
            return  //SomeLogicHere based on databaseName ;
        }

So, let me explain this again. I do not have an explicit context. So when a 
new scope is opened I create AddinContext instance and set the database 
name property. When ISessionFactory is needed I get it from 
a IMultiSessionFactoryProvider based on database name. That database name 
comes form AddinContext. The subdependency resolver helps me to get 
appropriate settings for each database. There are many settings and 
DatabaseConfiguration is one of them.

THE PROBLEM:
The only problem that I have at the moment is within the 
AddinSettingsResolver because when I want to resolve other components which 
are not in a scope then an exception is thrown.
1) is there any way to check if a scope is opened or not?
2) Better ways of doing this?

Best regards
mynkow
















-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to