2011-11-21 79 views
1

我正在使用nhibernate構建我自己的會員提供程序。 MembershipUser類是一個令人驚訝的困難的事情,因爲它基本的屬性和方法並不都是虛擬的,所以我覺得主要是因爲我不這麼認爲。我的用戶類的定義爲:流利的NHibernate映射一個沒有虛擬屬性的類

public class nhMembershipUser : MembershipUser 
{ 
    public new virtual Guid ProviderUserKey { get; set; } 
    public new virtual DateTime LastPasswordChangedDate { get; set; } 
    public virtual bool IsEnabled { get; set; } 
    public virtual string FirstName { get; set; } 
    public virtual string LastName { get; set; } 
    public virtual string Application { get; set; } 
    public new virtual string UserName { get; set; } 
    public new virtual bool IsOnline 
    { 
     get 
     { 
      var UnitOfWork = new nhUnitOfWork(); 
      return UnitOfWork.UserIsOnline(this); 
     } 
    } 
    public new virtual bool IsLockedOut { get; set; } 
    public virtual IList<nhRole> Roles { get; set; } 
    public new virtual bool ChangePassword(string oldPassword, string newPassword) 
    { 
     throw new NotSupportedException(); 
    } 
    public new virtual bool ChangePasswordQuestionAndAnswer(string password, string newPasswordQuestion, string newPasswordAnswer) 
    { 
     throw new NotSupportedException(); 
    } 
    public new virtual string ResetPassword() 
    { 
     throw new NotSupportedException(); 
    } 
    public new virtual string ResetPassword(string passwordAnswer) 
    { 
     throw new NotSupportedException(); 
    } 
    public new virtual string GetPassword() 
    { 
     throw new NotSupportedException(); 
    } 
    public new virtual string GetPassword(string passwordAnswer) 
    { 
     throw new NotSupportedException(); 
    } 
} 

當我試圖創建我的映射,我得到這個:

FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete conf 
iguration was used while creating a SessionFactory. Check PotentialReasons colle 
ction, and InnerException for more detail. 

    * Database was not configured through Database method. 
---> NHibernate.InvalidProxyTypeException: The following types may not be used 
as proxies: 
nhApplicationServicesProvider.Entities.nhMembershipUser: method Update should be 
'public/protected virtual' or 'protected internal virtual' 
nhApplicationServicesProvider.Entities.nhMembershipUser: method GetPassword shou 
ld be 'public/protected virtual' or 'protected internal virtual' 
nhApplicationServicesProvider.Entities.nhMembershipUser: method ChangePassword s 
hould be 'public/protected virtual' or 'protected internal virtual' 
nhApplicationServicesProvider.Entities.nhMembershipUser: method ResetPassword sh 
ould be 'public/protected virtual' or 'protected internal virtual' 
nhApplicationServicesProvider.Entities.nhRole: method set_RoleId should be 'publ 
ic/protected virtual' or 'protected internal virtual' 
nhApplicationServicesProvider.Entities.nhRole: method set_Name should be 'public 
/protected virtual' or 'protected internal virtual' 
    at NHibernate.Cfg.Configuration.ValidateEntities() 
    at NHibernate.Cfg.Configuration.Validate() 
    at NHibernate.Cfg.Configuration.BuildSessionFactory() 
    at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in d:\Build 
s\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 227 
    --- End of inner exception stack trace --- 
    at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in d:\Build 
s\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 232 
    at nhApplicationServicesProvider.nhSessionFactory.CreateDatabaseSchema() in C 
:\Users\JHolovacs\Documents\Projects\nhApplicationServicesProvider\nhApplication 
ServicesProvider\nhSessionFactory.cs:line 63 
    at nhApplicationServicesProvider.Program.Main() in C:\Users\JHolovacs\Documen 
ts\Projects\nhApplicationServicesProvider\nhApplicationServicesProvider\Program. 
cs:line 13 

    * Database was not configured through Database method. 

它特別提到getPassword來()ChangePassword(),和ResetPassword (),據我所知,正確地用虛方法覆蓋基類。

爲什麼我會收到這些錯誤,以及如何解決此問題?

回答

2

它特別提到getPassword來(),ChangePassword(),和ResetPassword(),它們是,據我所知,適當地覆蓋具有虛擬方法的基類。

不,你沒有覆蓋這些方法,你只是隱藏它們。

NHibernate要求所有非私有屬性和方法都是虛擬的,否則就沒有機會攔截代理上的調用。

如果你不需要這個類被延遲加載,只需將你的實體映射到lazy="false",你就不再需要虛擬成員了。如果您需要延遲加載,則可以使用代理接口proxy="ProxyInterface"

+0

哇,我正式離開了我的舒適區,但這聽起來像是正確的答案。我會嘗試一下並回復。 –

0

我曾經有過類似的需求,並使用this article on CodeProject作爲模板。它似乎工作得很好。

我認爲這個關鍵不是從您的域對象中的MembershipUser繼承,而是提供一種在您的域用戶和成員資格用戶之間進行轉換的方法(我不相信我改變了這個功能):

public MembershipUser GetMembershipUserFromUser (User usr) { 
     MembershipUser u = new MembershipUser (this.Name, usr.Username, usr.Id, usr.Email, usr.PasswordQuestion, usr.Comment, usr.IsApproved, usr.IsLockedOut, usr.CreationDate, usr.LastLoginDate, 
     usr.LastActivityDate, usr.LastPasswordChangedDate, usr.LastLockedOutDate); 

     return u; 
    } 
+1

blech ......這似乎違反了乾淨編碼的一些原則。你知道嗎?爲什麼?我得到這些錯誤?如果我必須解決它,那就這樣吧,但我真的很想理解這個問題。 –

+0

我不確定它是否確實如此,但我的目標是不讓MS想到MembershipUser泄漏到我的實體中。如果您希望繼承MembershipUser,我認爲cremor的答案會指向您正確的方向。 – AlexCuse