These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Daring Session Architecture: 1 Session, multiple Assembly's
PostPosted: Fri Aug 31, 2007 5:29 am 
Beginner
Beginner

Joined: Mon Mar 26, 2007 5:47 am
Posts: 22
Hi,

I would like to have multiple Assembly's use the same Session (or something like that).

I'm having like this situation:
ChildAssembly and ParentAssembly: Each containing their own objects and own mappingfiles. ParentAssembly uses ChildAssembly and its objects.

Sometimes ParentAssembly points to the same Database, but sometimes not.

So what I would like is: Putting the whole Session-stuff in some Framework that is used by both Assemblys, in such a way that:
- ParentAssemly and ChildAssembly both ask to this Framework the Session to use, based on a connectionstring and dialect.
- If the point to the same database, I would like them to use the same Session! So one Assembly isn't locking out the other Assembly...

Is this possible?


I was thinking about having a shared SessionHolder-object in my Framework which contains a list of sessions, and creates the session if it doesn't exist yet (based on connectionstring and dialect), and returns it to the Assembly asking for it. Only problem: When creating the Factory, it needs an assembly... Will it work when I create the Factory/Session in the ParentAssembly (with itself as Assembly), and pass this session to the ChildAssembly? Or will the ChildAssembly always need a Factory with itself as ChildAssembly? So will different Assemblies always need different Factory/Session?


Any help will be really appreciated...


Thansk a lot in advance,


Pieter


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 03, 2007 12:15 pm 
Contributor
Contributor

Joined: Sun Jun 26, 2005 5:03 am
Posts: 51
Location: London, UK
Can't easily do this if there's the possibility of different databases, but I have something for when you have the same database.

Here's a couple of classes...

Code:
    /// <summary>
    /// Allows us to load mapping assembly information from a config file.
    /// </summary>
    public class MappingAssemblyConfigHandler : IConfigurationSectionHandler
    {
        #region IConfigurationSectionHandler Members

        /// <summary>
        /// Reads the config file to create a MappingAssemblies object
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="configContext"></param>
        /// <param name="section"></param>
        /// <returns></returns>
        public object Create(object parent, object configContext, XmlNode section)
        {
            MappingAssemblies assemblies = new MappingAssemblies();

            foreach (XmlNode node in section.SelectNodes("assembly"))
                assemblies.Names.Add(node.SelectSingleNode("@name").Value);

            return assemblies;
        }

        #endregion
    }

    /// <summary>
    /// An collection of assemblies that contains persistence mapping information
    /// </summary>
    public class MappingAssemblies
    {
        private static IList names;

        public MappingAssemblies()
        {
            names = new ArrayList();
        }

        /// <summary>
        /// The names of the assemblies
        /// </summary>
        public IList Names
        {
            get { return names; }
        }
    }


Then in your session manager you have a snippet like the following

Code:
        /// <summary>
        /// Creates the configuration from the config parameters
        /// </summary>
        /// <returns></returns>
        private Configuration InitializeConfiguration()
        {
            Configuration cfg = new Configuration();

            // The following makes sure the the web.config contains a declaration for the persistenceMappingAssemblies config section
            MappingAssemblies assemblies =
                (MappingAssemblies) ConfigurationManager.GetSection("persistenceMappingAssemblies");
            if (assemblies == null)
                throw new ConfigurationErrorsException(
                    "NHibernateSessionManager.InitializeConfiguration: \"persistenceMappingAssemblies\" must be " +
                    "provided as a section within your config file. \"persistenceMappingAssemblies\" informs NHibernate which assembly(s) " +
                    "contains the HBM files. It is assumed that the HBM files are embedded resources. An example config " +
                    "declaration is <persistenceMappingAssemblies><assembly name=\"MyProject.Core\" /></persistenceMappingAssemblies>");

            try
            {
                foreach (string name in assemblies.Names)
                    cfg.AddAssembly(name);

                return cfg;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw;
            }
        }


And your web config looks has a section like this...

<persistenceMappingAssemblies>
<assembly name="MyLib.Core" />
<assembly name="ClientProject.Core" />
</persistenceMappingAssemblies>

Hope that helps a bit

_________________
Paul Hatcher
NHibernate Team


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.