Пример #1
0
        /// <summary>Adds mappings to the configuration.</summary>
        /// <param name="cfg">The configuration to add the mappings to.</param>
        /// <param name="name">The resource name of the embedded resource.</param>
        protected virtual void AddMapping(NHibernate.Cfg.Configuration cfg, string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                using (Stream stream = GetStreamFromName(name))
                {
                    if (stream == null) throw new ArgumentException("Could not read stream from embedded resource '" + name + "'", "name");

                    using (StreamReader reader = new StreamReader(stream))
                    {
                        string mappingXml = reader.ReadToEnd();
                        mappingXml = FormatMapping(mappingXml);
                        cfg.AddXml(mappingXml);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>Generates subclasses nhibernate xml configuration as an alternative to NHibernate definition file and adds them to the configuration.</summary>
        /// <param name="cfg">The nhibernate configuration to build.</param>
        protected virtual void GenerateMappings(NHibernate.Cfg.Configuration cfg)
        {
            Debug.Write("Adding");
            StringBuilder mappings = new StringBuilder(mappingStartTag);

            var allTypes = definitions.GetDefinitions()
                .Select(d => d.ItemType)
                .SelectMany(t => Utility.GetBaseTypesAndSelf(t))
                .Distinct()
                .OrderBy(t => Utility.GetBaseTypes(t).Count())
                .Where(t => t.IsSubclassOf(typeof(ContentItem)))
                .ToList();

            foreach(var type in allTypes)
            {
                string discriminator = type.Name;
                var definition = definitions.GetDefinition(type);
                if(definition != null)
                    discriminator = definition.Discriminator ?? discriminator;

                string classMapping = generator.GetMapping(type, type.BaseType, discriminator);
                mappings.Append(classMapping);
            }

            mappings.Append(mappingEndTag);
            cfg.AddXml(FormatMapping(mappings.ToString()));
        }
Пример #3
0
 protected virtual void AddDefaultMapping(NHibernate.Cfg.Configuration cfg)
 {
     using (Stream stream = GetStreamFromName(DefaultMapping))
     using (StreamReader reader = new StreamReader(stream))
     {
         string mappingXml = reader.ReadToEnd();
         mappingXml = FormatMapping(mappingXml);
         cfg.AddXml(mappingXml);
     }
 }