开发者

How would I do this in with in nhibernate? Automatically changing fields?

开发者 https://www.devze.com 2023-02-09 16:21 出处:网络
I linq to sql I could make a partial view and do something like this public partial class User { partial void OnNameChanged()

I linq to sql I could make a partial view and do something like this

public partial class User
    {

        partial void OnNameChanged()
        {
            UserName = StringFormatter.ToTitleCase(UserName.Trim());
        }

        partial void OnEmailChanged()
        {
            Email = Email.ToLower().Trim();
        }

    }

can I do something like this in nhibe开发者_运维知识库rnate. Also if you can. Can you do something like that OnQuery or OnSave?


The best way to do that with NHibernate would be to use the property accessors directly:

public class User
{
   ...
   private string userName;
   public virtual string UserName
   {
       get{return StringFormatter.ToTitleCase(userName.Trim());}
       set{userName = StringFormatter.ToTitleCase(value.Trim());}
   }
   private string email
   public virtual string Email
   {
       get{return email.Trim().ToLower();}
       set{email= value.Trim().ToLower();}
   }
   ...
}

Since NHibernate doesn't generate the DAO, but instead just uses your existing domain object, you have complete control over get/set logic. No need for a partial (though you could set it up that way to, if you really wanted).

0

精彩评论

暂无评论...
验证码 换一张
取 消