The reason I chose NHibernate.Mapping.Attributes was that I planned on
decorating my domain objects with validation attributes from the
NHibernate.Validator.  Figured if I was going to put in those attributes I
might as well skip the .hbm.xml files and just utilize the attributes. But
I'm also having problems with the Validator; not sure if I am
misunderstanding something, or if my project needs to be set up differently
(as a single project, rather than a project for the core [Domain objects]
and a project for the business logic). I thought that the validator could
dynamically modify the config so that the hbm2ddl SchemaExport would take
into account things like not-null and max lengths. Am I doing something
wrong here? How do I get the validator to inject the property restraints
into the schema export?

I picked up the configuration example from
http://www.mail-archive.com/[email protected]/msg00952.html

===========================
My SessionManager Constructor
===========================
static NHibernateSessionManager()
        {
            if (config == null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    config = new Configuration().Configure();

                    //NHibernate.Mapping.Attributes
                    HbmSerializer.Default.HbmNamespace =
"MyProj.Core.Domain";
                    HbmSerializer.Default.HbmAssembly = "MyProj.Core";
                    HbmSerializer.Default.Validate = true;
                    HbmSerializer.Default.Serialize(stream,
typeof(MyProj.Core.Domain.User).Assembly);
                    stream.Position = 0;
                    config.AddInputStream(stream, "Domain.hbm.xml");

                    //NHibernate.Validator

NHibernate.Validator.Cfg.ValidatorInitializer.Initialize(config);

NHibernate.Validator.Cfg.Environment.SharedEngineProvider = new
NHibernate.Validator.Event.NHibernateSharedEngineProvider();

                    sessionFactory = config.BuildSessionFactory();
                }
            }
        }



On Wed, Jan 20, 2010 at 11:43 AM, Diego Mijelshon <[email protected]>wrote:

> Someone might have a better answer, but my suggestion is that you switch to
> HBM or Fluent mapping.
> Almost nobody uses NHibernate.Mapping.Attributes, so it's not being
> maintained a lot.
> Neither HBM nor Fluent have that problem, and they are just as easy to use.
>
>    Diego
>
>
> On Wed, Jan 20, 2010 at 14:31, Sam Kimmel <[email protected]> wrote:
>
>> After a couple days of tinkering around I was able to get
>> NHibernate.Mapping.Attributes (2.1 GA) to work. Does anyone know why I
>> need to explicitly specify the Class name or the type of the ID in the
>> attributes?  It is smart enough to figure out the names and types of
>> the properties without explicitly setting them, but can't pick up the
>> class name or id type for some reason.
>>
>> I've supplied the working bits of code below...maybe there is
>> something I am missing:
>>
>> ==================================================================
>> User Class (located in "MyProject.Core" assembly,
>> "MyProject.Core.Domain" namespace)
>> ==================================================================
>>        [Serializable()]
>>        [Class(Name="User", Table="tw_users")]
>>        public class User
>>        {
>>                [Id(UnsavedValue="0", Type="int"), Generator(Class =
>> "native")]
>>                public virtual int UserId { get; set; }
>>
>>                [Property(Length = 50, NotNull = true)]
>>                public virtual string Name { get; set; }
>>
>>                [Property(Length = 128, NotNull = true, Unique = true)]
>>                public virtual string Email { get; set; }
>>
>>                [Property(Length = 40, NotNull = true)]
>>                public virtual string Password { get; set; }
>>
>>                [Property(NotNull = true)]
>>                public virtual bool Approved { get; set; }
>>
>>                [Property(NotNull = true)]
>>                public virtual DateTime CreatedOn { get; set; }
>>        }
>>
>> =======================================
>> Session Helper (located in MyProject.Data assembly)
>> =======================================
>>        public sealed class NHibernateSessionManager
>>        {
>>                private static Configuration config = null;
>>                private static ISessionFactory sessionFactory = null;
>>
>>                static NHibernateSessionManager()
>>                {
>>                        if (config == null)
>>                        {
>>                                using (MemoryStream stream = new
>> MemoryStream())
>>                                {
>>                                        HbmSerializer.Default.HbmNamespace
>> = "MyProject.Core.Domain";
>>                                        HbmSerializer.Default.HbmAssembly =
>> "MyProject.Core";
>>                                        HbmSerializer.Default.Validate =
>> true;
>>
>>  HbmSerializer.Default.Serialize(stream, typeof
>> (Tectonic.Core.Domain.User).Assembly);
>>                                        stream.Position = 0;
>>                                        config = new
>> Configuration().Configure();
>>                                        config.AddInputStream(stream,
>> "Domain.hbm.xml");
>>                                        sessionFactory =
>> config.BuildSessionFactory();
>>                                }
>>                        }
>>                }
>>
>>                public static ISession OpenSession()
>>                {
>>                        return sessionFactory.OpenSession();
>>                }
>>
>>                public static IStatelessSession OpenStatelessSession()
>>                {
>>                        return sessionFactory.OpenStatelessSession();
>>                }
>>
>>                // Remove from release -- completely destroys and rebuilds
>> db from
>> domain model
>>                public static void BuildDb()
>>                {
>>                        new
>> NHibernate.Tool.hbm2ddl.SchemaExport(config).Execute(false,
>> true, false);
>>                }
>>        }
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "nhusers" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<nhusers%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/nhusers?hl=en.
>>
>>
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

Reply via email to