Csla.Data.DataMapper.Map & CanWriteProperty

Csla.Data.DataMapper.Map & CanWriteProperty

Old forum URL: forums.lhotka.net/forums/t/4343.aspx


JoOfMetL posted on Wednesday, February 13, 2008


Hello,

In the method "Map", I propose to call CanWriteProperty to verify that the user can change the value.

public static void Map(
      System.Collections.IDictionary source,
      object target, bool suppressExceptions,
      params string[] ignoreList)
    {
      List<string> ignore = new List<string>(ignoreList);
      Csla.Core.BusinessBase b = target as Csla.Core.BusinessBase;
      foreach (string propertyName in source.Keys)
      {
        if (!ignore.Contains(propertyName))
        {
          try
          {
              if (b == null)
                SetPropertyValue(target, propertyName, source[propertyName]);
              else if (b.CanWriteProperty(propertyName))
                  SetPropertyValue(target, propertyName, source[propertyName]);  
               
          }
          catch (Exception ex)
          {
            if (!suppressExceptions)
              throw new ArgumentException(
                String.Format("{0} ({1})",
                Resources.PropertyCopyFailed, propertyName), ex);
          }
        }
      }
    }

ajj3085 replied on Wednesday, February 13, 2008

Any reason why?  Seems to me that would silently hide an error if CanWriteProperty returned false, and you would be left wondering why the field didn't map.

JoOfMetL replied on Thursday, February 14, 2008


 Because, in my website, I have a page which allow to update data.
A user can update all the properties, and a another can update only 1 property, but he can see all the properties. So I disabled the property that he can't update (enabled = false).

But it's the same Code, in DataSource_Update Event, I use the 'Map" Method to update the data of my object. So I have a exception, because the map method want to push my property(wich have enabled = false)

So I have modify the code of the Map Method.

Copyright (c) Marimer LLC