I want to had a note that is not exactly about type safe coding, but
more about getting rid of strings in criteria's.

It easy to write a code generator using refly (codedom based), or
erverything else that would make a static method for every property of
an entity class.

eg :
source file :

Public partial class Product
{
       public virtual int Id {get;set}
       public virtual string Name {get;set}
}

do something that will write a second code file like

Public partial class Product
{
       public static string Id
               (get {
                   return "Id";
                      }
               )

       public static string Name
               (get {
                   return "Name";
                      }
               )
}

Then in your Criteria, you could use

ICriteria criteria = Session.CreateCriteria(typeof
(Product),Product.Name)

ICriteria criteria = Session.CreateCriteria(typeof(Product),"Name")

Pro's : refactor friendly.

Con's : Need to recompile before use on neeed field creation.
           Ugly hack.

On 28 fév, 11:40, "Richard \(Google\)" <[email protected]>
wrote:
> Oops, I missed that expression.  I'll add it.
>
> Also, I don't think it need to replace the existing criterias - it could
> live along side them.
>
> Richard
>
> --------------------------------------------------
> From: "Ingo" <[email protected]>
> Sent: Friday, February 27, 2009 7:02 PM
> To: "nhibernate-development" <[email protected]>
> Subject: [nhibernate-development] Re: Type safe criteria
>
>
>
>
>
> > I agree with you. This is so much better when creating criterias.
>
> > However, I have an example that I don't think the current version can
> > handle (to my knowledge anyway)
>
> > ICriteria criteria = Session.CreateCriteria(typeof(UserInRole))
> >                                     .Add(Expression.Eq
> > ("ApplicationName", applicationName))
> >                                     .Add(Expression.Eq("RoleName",
> > roleName))
> >                                     .Add(Expression.InsensitiveLike
> > ("UserName", userToMatch, MatchMode.Anywhere));
>
> > I think it has to be polished a bit before it can completely replace
> > the traditional criterias.
>
> > /Ingo
>
> > On 8 Jan, 10:52, "Ayende Rahien" <[email protected]> wrote:
> >> This is really interesting:
>
> >>http://nhforge.org/blogs/nhibernate/archive/2009/01/07/typesafe-icrit...
>
> >> Expanding the syntax a bit, we can use:
>
> >> session.CreateCriteria<Person>()
> >> .SetFetchMode(p => p.PersonDetail, FetchMode.Eager)
> >> .SetLockMode(LockMode.Upgrade)
> >> .Add(p=>p.Name.Like("foo"))
> >> .List();
>
> >> That would remove a lot of the pressure to build a full fledged linq
> >> provider.
> >> And it seems like it is much easier to do.
>
> >> Thoughts?- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -

Reply via email to