2

我有一个 CSLA 对象,它可以很好地从数据库返回数据,但是当我更改对象上的任何属性时,该对象仍然显示 IsDirty =“false”。虽然当我创建一个新对象时它报告 IsDirty =“true”。我确信它只是我的代码中缺少的一些简单的东西。以下是我的对象:

    [Serializable()]
[ObjectFactory( FactoryNames.SiteFactoryName )]
public class Site : BusinessBase<Site>
{
    #region Business Methods

    public static PropertyInfo<int> IdProperty = RegisterProperty<int>( p => p.Id );
    public int Id 
    {
        get { return GetProperty( IdProperty ); }
        set { LoadProperty( IdProperty, value ); }
    }

    public static PropertyInfo<string> NameProperty = RegisterProperty<string>( p => p.Name );
    public string Name
    {
        get { return GetProperty( NameProperty ); }
        set { LoadProperty( NameProperty, value ); }
    }

    public static PropertyInfo<string> CodeProperty = RegisterProperty<string>( p => p.Code );
    public string Code
    {
        get { return GetProperty( CodeProperty ); }
        set { LoadProperty( CodeProperty, value ); }
    }

    public static PropertyInfo<string> AliasProperty = RegisterProperty<string>( p => p.Alias );
    public string Alias
    {
        get { return GetProperty( AliasProperty ); }
        set { LoadProperty( AliasProperty, value ); }
    }

    public static PropertyInfo<string> AddressProperty = RegisterProperty<string>( p => p.Address );
    public string Address
    {
        get { return GetProperty( AddressProperty ); }
        set { LoadProperty( AddressProperty, value ); }
    }

    public static PropertyInfo<string> PostCodeProperty = RegisterProperty<string>( p => p.PostCode );
    public string PostCode
    {
        get { return GetProperty( PostCodeProperty ); }
        set { LoadProperty( PostCodeProperty, value ); }
    }

    public static PropertyInfo<string> InfoProperty = RegisterProperty<string>( p => p.Info );
    public string Info
    {
        get { return GetProperty( InfoProperty ); }
        set { LoadProperty( InfoProperty, value ); }
    }

    private static PropertyInfo<int> CustomerLinkProperty = RegisterProperty<int>( c => c.CustomerLink );
    public int CustomerLink
    {
        get { return GetProperty( CustomerLinkProperty ); }
        set { SetProperty( CustomerLinkProperty, value ); }
    }

    private static PropertyInfo<Accounts> AccountsProperty = RegisterProperty<Accounts>( c => c.Accounts );
    public Accounts Accounts
    {
        get { return GetProperty( AccountsProperty ); }
        set { SetProperty( AccountsProperty, value ); }
    }

    #endregion

    #region Business Rules

    protected override void AddBusinessRules()
    {
    }

    #endregion

    #region Authorization Rules

    protected override void AddAuthorizationRules()
    {
        // add AuthorizationRules here
        //AuthorizationRules.AllowWrite( NameProperty, "ProjectManager" );
        //AuthorizationRules.AllowWrite( StartedProperty, "ProjectManager" );
        //AuthorizationRules.AllowWrite( EndedProperty, "ProjectManager" );
        //AuthorizationRules.AllowWrite( DescriptionProperty, "ProjectManager" );
    }

    protected static void AddObjectAuthorizationRules()
    {
    }

    public override bool CanWriteProperty( string propertyName )
    {
        return true;
    }

    #endregion

    #region Facotry Methods

    public static Site NewSite()
    {
        return DataPortal.Create<Site>();
    }

    public static Site GetSite( int id )
    {
        return DataPortal.Fetch<Site>( new SingleCriteria<Site, int>( id ) );
    }

    public static Site GetSite( SiteCriteria siteCriteria )
    {
        return DataPortal.Fetch<Site>( siteCriteria );
    }

    public static void DeleteSite( int id )
    {
        DataPortal.Delete( new SingleCriteria<Site, int>( id ) );
    }

    private Site()
    { /* require use of factory methods */ }

    #endregion

    public class SiteCriteria
    {
        public int Id { get; private set; }

        public SiteCriteria( int id )
        {
            Id = id;
        }
    }
}

任何帮助将非常感激。

谢谢

4

1 回答 1

6

属性设置器应使用 SetProperty 而不是 LoadProperty

前任:

public static PropertyInfo<string> NameProperty = RegisterProperty<string>( p => p.Name );
public string Name
{
    get { return GetProperty( NameProperty ); }
    set { SetProperty( NameProperty, value ); } <--- use SetProperty instead of LoadProperty
}
于 2010-02-16T04:37:19.423 回答