Nitbits in code

Nitbits in code

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


Cine posted on Tuesday, April 01, 2008

Removed redundant type qualifiers, removed redundant event creators, remove redundant initial values. Fix some code to use new get;set; Kill some uncalled code.
Index: BusinessBase.cs
===================================================================
--- BusinessBase.cs    (revision 2235)
+++ BusinessBase.cs    (working copy)
@@ -236,7 +236,7 @@
     /// </returns>
     protected static PropertyInfo<P> RegisterProperty<P>(PropertyInfo<P> info)
     {
-      return Core.FieldManager.PropertyInfoManager.RegisterProperty<P>(typeof(T), info);
+      return Core.FieldManager.PropertyInfoManager.RegisterProperty(typeof(T), info);
     }
 
     #endregion
Index: BusinessListBase.cs
===================================================================
--- BusinessListBase.cs    (revision 2235)
+++ BusinessListBase.cs    (working copy)
@@ -512,7 +512,7 @@
       DeleteChild(child);
       INotifyPropertyChanged c = child as INotifyPropertyChanged;
       if (c != null)
-        c.PropertyChanged -= new PropertyChangedEventHandler(Child_PropertyChanged);
+        c.PropertyChanged -= Child_PropertyChanged;
     }
 
     /// <summary>
@@ -543,7 +543,7 @@
     protected override void SetItem(int index, C item)
     {
       C child = default(C);
-      if (!(ReferenceEquals((C)(this[index]), item)))
+      if (!(ReferenceEquals(this[index], item)))
         child = this[index];
       // replace the original object with this new
       // object
@@ -568,7 +568,7 @@
       {
         this.RaiseListChangedEvents = oldRaiseListChangedEvents;
       }
-      if (child != null)
+      if (!child.Equals(default(C)))
         CopyToDeletedList(child);
       if (RaiseListChangedEvents)
         OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
@@ -652,12 +652,6 @@
       _indexSet.RemoveItem(item);
     }
 
-    private void RemoveIndexItem(C item, string property)
-    {
-      DeferredLoadIndexIfNotLoaded();
-      _indexSet.RemoveItem(item, property);
-    }
-
     private void ReIndexItem(C item, string property)
     {
       DeferredLoadIndexIfNotLoaded();
@@ -710,13 +704,6 @@
       _positionMap.InsertIntoMap(child, position);
     }
 
-    private void AddToMap(C child)
-    {
-      DeferredLoadPositionMapIfNotLoaded();
-      _positionMap.AddToMap(child);
-     
-    }
-
     /// <summary>
     /// Determines the position of a specific item from the list
     /// </summary>
@@ -747,10 +734,10 @@
       }
       else
       {
-        IEnumerable<C> sourceEnum = this.AsEnumerable<C>();
+        IEnumerable<C> sourceEnum = this.AsEnumerable();
         //IQueryable<C> sourceQuery = this.AsQueryable<C>();
         //var result = sourceQuery.Where<C>(expr.Compile());
-        var result = sourceEnum.Where<C>(expr.Compile());
+        var result = sourceEnum.Where(expr.Compile());
         foreach (C item in result)
           yield return item;
       }
@@ -785,7 +772,7 @@
     #region IsChild
 
     [NotUndoable()]
-    private bool _isChild = false;
+    private bool _isChild;
 
     /// <summary>
     /// Indicates whether this collection object is a child object.
@@ -877,7 +864,7 @@
 
     private static PropertyDescriptorCollection _propertyDescriptors;
 
-    private PropertyDescriptor GetPropertyDescriptor(string propertyName)
+    private static PropertyDescriptor GetPropertyDescriptor(string propertyName)
     {
       if (_propertyDescriptors == null)
         _propertyDescriptors = TypeDescriptor.GetProperties(typeof(C));
@@ -904,7 +891,7 @@
         child.SetParent(this);
         INotifyPropertyChanged c = child as INotifyPropertyChanged;
         if (c != null)
-          c.PropertyChanged += new PropertyChangedEventHandler(Child_PropertyChanged);
+          c.PropertyChanged += Child_PropertyChanged;
       }
       foreach (Core.IEditableBusinessObject child in DeletedList)
         child.SetParent(this);
@@ -1265,9 +1252,6 @@
     #region LinqIntegration
 
     [NonSerialized]
-    IQueryProvider _queryProvider;
-
-    [NonSerialized]
     Expression _expression;
 
     internal void SetCurrentExpression(Expression ex)
@@ -1275,11 +1259,6 @@
       _expression = ex;
     }
 
-    private void LoadProviderIfNotLoaded()
-    {
-      _queryProvider = new Csla.Linq.CslaQueryProvider<T, C>(this);
-    }
-
     #region IQueryable Members
 
     /// <summary>
Index: Core/BusinessBase.cs
===================================================================
--- Core/BusinessBase.cs    (revision 2235)
+++ Core/BusinessBase.cs    (working copy)
@@ -497,7 +497,7 @@
     [EditorBrowsable(EditorBrowsableState.Advanced)]
     public virtual bool CanReadProperty(string propertyName)
     {
-      bool result = true;
+      bool result;
 
       VerifyAuthorizationCache();
 
@@ -602,7 +602,7 @@
     [EditorBrowsable(EditorBrowsableState.Advanced)]
     public virtual bool CanWriteProperty(string propertyName)
     {
-      bool result = true;
+      bool result;
 
       VerifyAuthorizationCache();
 
@@ -730,7 +730,7 @@
     public virtual bool CanExecuteMethod(string methodName)
     {
 
-      bool result = true;
+      bool result;
 
       VerifyAuthorizationCache();
 
@@ -1490,7 +1490,7 @@
     /// </returns>
     protected static PropertyInfo<P> RegisterProperty<P>(Type objectType, PropertyInfo<P> info)
     {
-      return Core.FieldManager.PropertyInfoManager.RegisterProperty<P>(objectType, info);
+      return Core.FieldManager.PropertyInfoManager.RegisterProperty(objectType, info);
     }
 
     #endregion
@@ -1517,7 +1517,7 @@
     /// </remarks>
     protected P GetProperty<P>(string propertyName, P field, P defaultValue)
     {
-      return GetProperty<P>(propertyName, field, defaultValue, Security.NoAccessBehavior.SuppressException);
+      return GetProperty(propertyName, field, defaultValue, Security.NoAccessBehavior.SuppressException);
     }
 
     /// <summary>
@@ -1561,7 +1561,7 @@
     /// </remarks>
     protected P GetProperty<P>(PropertyInfo<P> propertyInfo, P field)
     {
-      return GetProperty<P>(propertyInfo.Name, field, propertyInfo.DefaultValue, Security.NoAccessBehavior.SuppressException);
+      return GetProperty(propertyInfo.Name, field, propertyInfo.DefaultValue, Security.NoAccessBehavior.SuppressException);
     }
 
     /// <summary>
@@ -1585,7 +1585,7 @@
     /// </remarks>
     protected P GetProperty<F, P>(PropertyInfo<F> propertyInfo, F field)
     {
-      return Utilities.CoerceValue<P>(typeof(F), null, GetProperty<F>(propertyInfo.Name, field, propertyInfo.DefaultValue, Security.NoAccessBehavior.SuppressException));
+      return Utilities.CoerceValue<P>(typeof(F), null, GetProperty(propertyInfo.Name, field, propertyInfo.DefaultValue, Security.NoAccessBehavior.SuppressException));
     }
 
     /// <summary>
@@ -1612,7 +1612,7 @@
     /// </remarks>
     protected P GetProperty<F, P>(PropertyInfo<F> propertyInfo, F field, Security.NoAccessBehavior noAccess)
     {
-      return Utilities.CoerceValue<P>(typeof(F), null, GetProperty<F>(propertyInfo.Name, field, propertyInfo.DefaultValue, noAccess));
+      return Utilities.CoerceValue<P>(typeof(F), null, GetProperty(propertyInfo.Name, field, propertyInfo.DefaultValue, noAccess));
     }
 
     /// <summary>
@@ -1631,7 +1631,7 @@
     /// </remarks>
     protected P GetProperty<P>(PropertyInfo<P> propertyInfo)
     {
-      return GetProperty<P>(propertyInfo, Security.NoAccessBehavior.SuppressException);
+      return GetProperty(propertyInfo, Security.NoAccessBehavior.SuppressException);
     }
 
     /// <summary>
@@ -1654,7 +1654,7 @@
     /// </remarks>
     protected P GetProperty<F, P>(PropertyInfo<F> propertyInfo)
     {
-      return Utilities.CoerceValue<P>(typeof(F), null, GetProperty<F>(propertyInfo, Security.NoAccessBehavior.SuppressException));
+      return Utilities.CoerceValue<P>(typeof(F), null, GetProperty(propertyInfo, Security.NoAccessBehavior.SuppressException));
     }
 
     /// <summary>
@@ -1680,7 +1680,7 @@
     /// </remarks>
     protected P GetProperty<F, P>(PropertyInfo<F> propertyInfo, Security.NoAccessBehavior noAccess)
     {
-      return Utilities.CoerceValue<P>(typeof(F), null, GetProperty<F>(propertyInfo, noAccess));
+      return Utilities.CoerceValue<P>(typeof(F), null, GetProperty(propertyInfo, noAccess));
     }
 
     /// <summary>
@@ -1702,9 +1702,9 @@
     /// </remarks>
     protected P GetProperty<P>(PropertyInfo<P> propertyInfo, Security.NoAccessBehavior noAccess)
     {
-      P result = default(P);
+      P result;
       if (CanReadProperty(propertyInfo.Name, noAccess == Csla.Security.NoAccessBehavior.ThrowException))
-        result = ReadProperty<P>(propertyInfo);
+        result = ReadProperty(propertyInfo);
       else
         result = propertyInfo.DefaultValue;
       return result;
@@ -1755,7 +1755,7 @@
     /// PropertyInfo object containing property metadata.</param>
     protected P ReadProperty<F, P>(PropertyInfo<F> propertyInfo)
     {
-      return Utilities.CoerceValue<P>(typeof(F), null, ReadProperty<F>(propertyInfo));
+      return Utilities.CoerceValue<P>(typeof(F), null, ReadProperty(propertyInfo));
     }
 
     /// <summary>
@@ -1768,7 +1768,7 @@
     /// PropertyInfo object containing property metadata.</param>
     protected P ReadProperty<P>(PropertyInfo<P> propertyInfo)
     {
-      P result = default(P);
+      P result;
       FieldManager.IFieldData data = FieldManager.GetFieldData(propertyInfo);
       if (data != null)
       {
@@ -1781,7 +1781,7 @@
       else
       {
         result = propertyInfo.DefaultValue;
-        FieldManager.LoadFieldData<P>(propertyInfo, result);
+        FieldManager.LoadFieldData(propertyInfo, result);
       }
       return result;
     }
@@ -1867,7 +1867,7 @@
     /// </remarks>
     protected void SetProperty<P, V>(PropertyInfo<P> propertyInfo, ref P field, V newValue)
     {
-      SetProperty<P, V>(propertyInfo, ref field, newValue, Security.NoAccessBehavior.ThrowException);
+      SetProperty(propertyInfo, ref field, newValue, Security.NoAccessBehavior.ThrowException);
     }
 
     /// <summary>
@@ -1896,7 +1896,7 @@
     /// </remarks>
     protected void SetProperty<P, V>(PropertyInfo<P> propertyInfo, ref P field, V newValue, Security.NoAccessBehavior noAccess)
     {
-      SetProperty<P, V>(propertyInfo.Name, ref field, newValue, noAccess);
+      SetProperty(propertyInfo.Name, ref field, newValue, noAccess);
     }
 
     /// <summary>
@@ -1919,9 +1919,9 @@
       {
         try
         {
-          if (field == null)
+          if (field.Equals(default(P)))
           {
-            if (newValue != null)
+            if (!newValue.Equals(default(P)))
             {
               OnPropertyChanging(propertyName);
               field = newValue;
@@ -1930,7 +1930,7 @@
           }
           else if (!(field.Equals(newValue)))
           {
-            if (newValue is string && newValue == null)
+            if (newValue is string && newValue.Equals(default(P)))
               newValue = Utilities.CoerceValue<P>(typeof(string), field, string.Empty);
             OnPropertyChanging(propertyName);
             field = newValue;
@@ -1974,9 +1974,9 @@
       {
         try
         {
-          if (field == null)
+          if (field.Equals(default(P)))
           {
-            if (newValue != null)
+            if (!newValue.Equals(default(P)))
             {
               OnPropertyChanging(propertyName);
               field = Utilities.CoerceValue<P>(typeof(V), field, newValue);
@@ -1985,7 +1985,7 @@
           }
           else if (!(field.Equals(newValue)))
           {
-            if (newValue is string && newValue == null)
+            if (newValue is string && newValue.Equals(default(P)))
               newValue = Utilities.CoerceValue<V>(typeof(string), null, string.Empty);
             OnPropertyChanging(propertyName);
             field = Utilities.CoerceValue<P>(typeof(V), field, newValue);
@@ -2032,7 +2032,7 @@
     /// </remarks>
     protected void SetProperty<P, F>(PropertyInfo<P> propertyInfo, F newValue)
     {
-      SetProperty<P, F>(propertyInfo, newValue, Security.NoAccessBehavior.ThrowException);
+      SetProperty(propertyInfo, newValue, Security.NoAccessBehavior.ThrowException);
     }
 
     /// <summary>
@@ -2053,12 +2053,12 @@
       {
         try
         {
-          P oldValue = default(P);
+          P oldValue;
           var fieldData = FieldManager.GetFieldData(propertyInfo);
           if (fieldData == null)
           {
             oldValue = propertyInfo.DefaultValue;
-            fieldData = FieldManager.LoadFieldData<P>(propertyInfo, oldValue);
+            FieldManager.LoadFieldData(propertyInfo, oldValue);
           }
           else
           {
@@ -2068,7 +2068,7 @@
             else
               oldValue = (P)fieldData.Value;
           }
-          LoadPropertyValue<P>(propertyInfo, oldValue, Utilities.CoerceValue<P>(typeof(F), oldValue, newValue), true);
+          LoadPropertyValue(propertyInfo, oldValue, Utilities.CoerceValue<P>(typeof(F), oldValue, newValue), true);
         }
         catch (Exception ex)
         {
@@ -2098,12 +2098,12 @@
       {
         try
         {
-          P oldValue = default(P);
+          P oldValue;
           var fieldData = FieldManager.GetFieldData(propertyInfo);
           if (fieldData == null)
           {
             oldValue = propertyInfo.DefaultValue;
-            fieldData = FieldManager.LoadFieldData<P>(propertyInfo, oldValue);
+            FieldManager.LoadFieldData(propertyInfo, oldValue);
           }
           else
           {
@@ -2113,7 +2113,7 @@
             else
               oldValue = (P)fieldData.Value;
           }
-          LoadPropertyValue<P>(propertyInfo, oldValue, newValue, true);
+          LoadPropertyValue(propertyInfo, oldValue, newValue, true);
         }
         catch (Exception ex)
         {
@@ -2163,12 +2163,12 @@
     {
       try
       {
-        P oldValue = default(P);
+        P oldValue;
         var fieldData = FieldManager.GetFieldData(propertyInfo);
         if (fieldData == null)
         {
           oldValue = propertyInfo.DefaultValue;
-          fieldData = FieldManager.LoadFieldData<P>(propertyInfo, oldValue);
+          FieldManager.LoadFieldData<P>(propertyInfo, oldValue);
         }
         else
         {
@@ -2178,7 +2178,7 @@
           else
             oldValue = (P)fieldData.Value;
         }
-        LoadPropertyValue<P>(propertyInfo, oldValue, Utilities.CoerceValue<P>(typeof(F), oldValue, newValue), false);
+        LoadPropertyValue(propertyInfo, oldValue, Utilities.CoerceValue<P>(typeof(F), oldValue, newValue), false);
       }
       catch (Exception ex)
       {
@@ -2208,12 +2208,12 @@
     {
       try
       {
-        P oldValue = default(P);
+        P oldValue;
         var fieldData = FieldManager.GetFieldData(propertyInfo);
         if (fieldData == null)
         {
           oldValue = propertyInfo.DefaultValue;
-          fieldData = FieldManager.LoadFieldData<P>(propertyInfo, oldValue);
+          FieldManager.LoadFieldData(propertyInfo, oldValue);
         }
         else
         {
@@ -2223,7 +2223,7 @@
           else
             oldValue = (P)fieldData.Value;
         }
-        LoadPropertyValue<P>(propertyInfo, oldValue, newValue, false);
+        LoadPropertyValue(propertyInfo, oldValue, newValue, false);
       }
       catch (Exception ex)
       {
@@ -2233,9 +2233,9 @@
 
     private void LoadPropertyValue<P>(PropertyInfo<P> propertyInfo, P oldValue, P newValue, bool markDirty)
     {
-      var valuesDiffer = false;
-      if (oldValue == null)
-        valuesDiffer = newValue != null;
+      bool valuesDiffer;
+      if (oldValue.Equals(default(P)))
+        valuesDiffer = !newValue.Equals(default(P));
       else
         valuesDiffer = !(oldValue.Equals(newValue));
 
@@ -2244,20 +2244,20 @@
         if (typeof(IEditableBusinessObject).IsAssignableFrom(propertyInfo.Type))
         {
           // remove old event hook
-          if (oldValue != null)
+          if (!oldValue.Equals(default(P)))
           {
             INotifyPropertyChanged pc = (INotifyPropertyChanged)oldValue;
-            pc.PropertyChanged -= new PropertyChangedEventHandler(Child_PropertyChanged);
+            pc.PropertyChanged -= Child_PropertyChanged;
           }
           if (markDirty)
           {
             OnPropertyChanging(propertyInfo.Name);
-            FieldManager.SetFieldData<P>(propertyInfo, newValue);
+            FieldManager.SetFieldData(propertyInfo, newValue);
             PropertyHasChanged(propertyInfo.Name);
           }
           else
           {
-            FieldManager.LoadFieldData<P>(propertyInfo, newValue);
+            FieldManager.LoadFieldData(propertyInfo, newValue);
           }
           IEditableBusinessObject child = (IEditableBusinessObject)newValue;
           if (child != null)
@@ -2269,26 +2269,26 @@
             child.EditLevelAdded = this.EditLevel;
             // hook child event
             INotifyPropertyChanged pc = (INotifyPropertyChanged)newValue;
-            pc.PropertyChanged += new PropertyChangedEventHandler(Child_PropertyChanged);
+            pc.PropertyChanged += Child_PropertyChanged;
           }
         }
         else if (typeof(IEditableCollection).IsAssignableFrom(propertyInfo.Type))
         {
           // remove old event hooks
-          if (oldValue != null)
+          if (!oldValue.Equals(default(P)))
           {
             IBindingList pc = (IBindingList)oldValue;
-            pc.ListChanged -= new ListChangedEventHandler(Child_ListChanged);
+            pc.ListChanged -= Child_ListChanged;
           }
           if (markDirty)
           {
             OnPropertyChanging(propertyInfo.Name);
-            FieldManager.SetFieldData<P>(propertyInfo, newValue);
+            FieldManager.SetFieldData(propertyInfo, newValue);
             PropertyHasChanged(propertyInfo.Name);
           }
           else
           {
-            FieldManager.LoadFieldData<P>(propertyInfo, newValue);
+            FieldManager.LoadFieldData(propertyInfo, newValue);
           }
           IEditableCollection child = (IEditableCollection)newValue;
           if (child != null)
@@ -2301,7 +2301,7 @@
               UndoableBase.ResetChildEditLevel(undoChild, this.EditLevel, this.BindingEdit);
             }
             IBindingList pc = (IBindingList)newValue;
-            pc.ListChanged += new ListChangedEventHandler(Child_ListChanged);
+            pc.ListChanged += Child_ListChanged;
           }
         }
         else
@@ -2309,12 +2309,12 @@
           if (markDirty)
           {
             OnPropertyChanging(propertyInfo.Name);
-            FieldManager.SetFieldData<P>(propertyInfo, newValue);
+            FieldManager.SetFieldData(propertyInfo, newValue);
             PropertyHasChanged(propertyInfo.Name);
           }
           else
           {
-            FieldManager.LoadFieldData<P>(propertyInfo, newValue);
+            FieldManager.LoadFieldData(propertyInfo, newValue);
           }
         }
       }
@@ -2384,7 +2384,7 @@
         {
           eo.SetParent(this);
           INotifyPropertyChanged pc = (INotifyPropertyChanged)item;
-          pc.PropertyChanged += new PropertyChangedEventHandler(Child_PropertyChanged);
+          pc.PropertyChanged += Child_PropertyChanged;
         }
         else
         {
@@ -2393,7 +2393,7 @@
           {
             el.SetParent(this);
             IBindingList bl = (IBindingList)item;
-            bl.ListChanged += new ListChangedEventHandler(Child_ListChanged);
+            bl.ListChanged += Child_ListChanged;
           }
         }
       }
Index: DataPortal/Client/DataPortal.cs
===================================================================
--- DataPortal/Client/DataPortal.cs    (revision 2235)
+++ DataPortal/Client/DataPortal.cs    (working copy)
@@ -1,5 +1,6 @@
 using System;
 using System.ComponentModel;
+using Csla.DataPortalClient;
 using Csla.Reflection;
 using Csla.Properties;
 
@@ -482,8 +483,7 @@
         var method = Server.DataPortalMethodCache.GetMethodInfo(
           objectType, "DataPortal_Delete", criteria);
 
-        DataPortalClient.IDataPortalProxy proxy;
-        proxy = GetDataPortalProxy(method.RunLocal);
+        IDataPortalProxy proxy = GetDataPortalProxy(method.RunLocal);
 
         dpContext = new Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote);
 
Index: DataPortal/Hosts/EnterpriseServicesPortal.cs
===================================================================
--- DataPortal/Hosts/EnterpriseServicesPortal.cs    (revision 2235)
+++ DataPortal/Hosts/EnterpriseServicesPortal.cs    (working copy)
@@ -89,8 +89,7 @@
       // this is a workaround for a bug in the .NET runtime
       AppDomain currentDomain = AppDomain.CurrentDomain;
 
-      currentDomain.AssemblyResolve +=
-        new ResolveEventHandler(ResolveEventHandler);
+      currentDomain.AssemblyResolve += ResolveEventHandler;
     }
 
     private static Assembly ResolveEventHandler(
Index: EditableRootListBase.cs
===================================================================
--- EditableRootListBase.cs    (revision 2235)
+++ EditableRootListBase.cs    (working copy)
@@ -151,7 +151,7 @@
       System.ComponentModel.INotifyPropertyChanged c = item as System.ComponentModel.INotifyPropertyChanged;
       if (c != null)
       {
-        c.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Child_PropertyChanged);
+        c.PropertyChanged -= Child_PropertyChanged;
       }
 
       base.RemoveItem(index);
@@ -238,7 +238,7 @@
         System.ComponentModel.INotifyPropertyChanged c = child as System.ComponentModel.INotifyPropertyChanged;
         if (c != null)
         {
-          c.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Child_PropertyChanged);
+          c.PropertyChanged += Child_PropertyChanged;
         }
       }
 
Index: FilteredBindingList.cs
===================================================================
--- FilteredBindingList.cs    (revision 2235)
+++ FilteredBindingList.cs    (working copy)
@@ -12,7 +12,7 @@
   /// <typeparam name="T">The type of the objects contained
   /// in the original list.</typeparam>
   public class FilteredBindingList<T> :
-    IList<T>, IBindingList, IEnumerable<T>,
+    IList<T>, IBindingList,
     ICancelAddNew
   {
 
@@ -20,24 +20,14 @@
 
     private class ListItem
     {
-      private object _key;
-      private int _baseIndex;
+      public object Key { get; private set; }
 
-      public object Key
-      {
-        get { return _key; }
-      }
+      public int BaseIndex { get; set; }
 
-      public int BaseIndex
-      {
-        get { return _baseIndex; }
-        set { _baseIndex = value; }
-      }
-
       public ListItem(object key, int baseIndex)
       {
-        _key = key;
-        _baseIndex = baseIndex;
+        Key = key;
+        BaseIndex = baseIndex;
       }
 
       public override string ToString()
@@ -94,7 +84,7 @@
 
       #region IDisposable Support
 
-      private bool _disposedValue = false; // To detect redundant calls.
+      private bool _disposedValue; // To detect redundant calls.
 
       // IDisposable
       protected virtual void Dispose(bool disposing)
@@ -679,7 +669,7 @@
     private bool _filtered;
     private PropertyDescriptor _filterBy;
     private object _filter;
-    FilterProvider _provider = null;
+    FilterProvider _provider;
     private List<ListItem> _filterIndex =
       new List<ListItem>();
 
@@ -695,8 +685,7 @@
       {
         _supportsBinding = true;
         _bindingList = (IBindingList)_list;
-        _bindingList.ListChanged +=
-          new ListChangedEventHandler(SourceChanged);
+        _bindingList.ListChanged += SourceChanged;
       }
     }
 
@@ -826,7 +815,7 @@
       if (_filtered)
       {
         int listIndex;
-        int filteredIndex = -1;
+        int filteredIndex;
         T newItem;
         object newKey;
         switch (e.ListChangedType)
@@ -835,10 +824,7 @@
             listIndex = e.NewIndex;
             // add new value to index
             newItem = _list[listIndex];
-            if (_filterBy != null)
-              newKey = _filterBy.GetValue(newItem);
-            else
-              newKey = newItem;
+            newKey = _filterBy != null ? _filterBy.GetValue(newItem) : newItem;
             _filterIndex.Add(
               new ListItem(newKey, listIndex));
             filteredIndex = _filterIndex.Count - 1;
@@ -855,10 +841,7 @@
             if (filteredIndex != -1)
             {
               newItem = _list[listIndex];
-              if (_filterBy != null)
-                newKey = _filterBy.GetValue(newItem);
-              else
-                newKey = newItem;
+              newKey = _filterBy != null ? _filterBy.GetValue(newItem) : newItem;
               _filterIndex[filteredIndex] =
                 new ListItem(newKey, listIndex);
             }
Index: Linq/LinqBindingList.cs
===================================================================
--- Linq/LinqBindingList.cs    (revision 2235)
+++ Linq/LinqBindingList.cs    (working copy)
@@ -15,7 +15,7 @@
   /// <typeparam name="T">The type of the objects contained
   /// in the original list.</typeparam>
   public class LinqBindingList<T> :
-    IList<T>, IBindingList, IEnumerable<T>,
+    IList<T>, IBindingList,
     ICancelAddNew, IQueryable<T>
   {
 
@@ -23,24 +23,14 @@
 
     private class ListItem
     {
-      private object _key;
-      private int _baseIndex;
+      public object Key { get; private set; }
 
-      public object Key
-      {
-        get { return _key; }
-      }
+      public int BaseIndex { get; set; }
 
-      public int BaseIndex
-      {
-        get { return _baseIndex; }
-        set { _baseIndex = value; }
-      }
-
       public ListItem(object key, int baseIndex)
       {
-        _key = key;
-        _baseIndex = baseIndex;
+        Key = key;
+        BaseIndex = baseIndex;
       }
 
       public override string ToString()
@@ -546,8 +536,7 @@
       {
         _supportsBinding = true;
         _bindingList = (IBindingList)_list;
-        _bindingList.ListChanged +=
-          new ListChangedEventHandler(SourceChanged);
+        _bindingList.ListChanged += SourceChanged;
       }
     }
 
@@ -590,7 +579,7 @@
       object sender, ListChangedEventArgs e)
     {
       int listIndex;
-      int filteredIndex = -1;
+      int filteredIndex;
       T newItem;
       object newKey;
       switch (e.ListChangedType)
@@ -599,10 +588,7 @@
           listIndex = e.NewIndex;
           // add new value to index
           newItem = _list[listIndex];
-          if (_filterBy != null)
-            newKey = _filterBy.GetValue(newItem);
-          else
-            newKey = newItem;
+          newKey = _filterBy != null ? _filterBy.GetValue(newItem) : newItem;
 
           //check to see if it is in the filter
           if (ItemShouldBeInList(newItem))
@@ -622,10 +608,7 @@
           if (filteredIndex != -1)
           {
             newItem = _list[listIndex];
-            if (_filterBy != null)
-              newKey = _filterBy.GetValue(newItem);
-            else
-              newKey = newItem;
+            newKey = _filterBy != null ? _filterBy.GetValue(newItem) : newItem;
             if (ItemShouldBeInList(newItem))
               _filterIndex[filteredIndex] =
                 new ListItem(newKey, listIndex);
Index: NameValueListBase.cs
===================================================================
--- NameValueListBase.cs    (revision 2235)
+++ NameValueListBase.cs    (working copy)
@@ -16,7 +16,7 @@
   [Serializable()]
   public abstract class NameValueListBase<K, V> :
     Core.ReadOnlyBindingList<NameValueListBase<K, V>.NameValuePair>,
-    ICloneable, Core.IBusinessObject, Server.IDataPortalTarget
+    ICloneable, Server.IDataPortalTarget
   {
 
     #region Core Implementation
@@ -152,34 +152,25 @@
     [Serializable()]
     public class NameValuePair
     {
-      private K _key;
-      private V _value;
-
       /// <summary>
       /// The Key or Name value.
       /// </summary>
-      public K Key
-      {
-        get { return _key; }
-      }
+      public K Key { get; private set; }
 
       /// <summary>
       /// The Value corresponding to the key/name.
       /// </summary>
-      public V Value
-      {
-        get { return _value; }
-      }
+      public V Value { get; private set; }
 
-      /// <summary>
+       /// <summary>
       /// Creates an instance of the object.
       /// </summary>
       /// <param name="key">The key.</param>
       /// <param name="value">The value.</param>
       public NameValuePair(K key, V value)
       {
-        _key = key;
-        _value = value;
+        Key = key;
+        Value = value;
       }
 
       /// <summary>
@@ -188,7 +179,7 @@
       /// </summary>
       public override string ToString()
       {
-        return _value.ToString();
+        return Value.ToString();
       }
     }
 
Index: SortedBindingList.cs
===================================================================
--- SortedBindingList.cs    (revision 2235)
+++ SortedBindingList.cs    (working copy)
@@ -14,32 +14,21 @@
   /// the original list or collection.
   /// </typeparam>
   public class SortedBindingList<T> :
-    IList<T>, IBindingList, IEnumerable<T>, ICancelAddNew
+    IList<T>, IBindingList, ICancelAddNew
   {
 
     #region ListItem class
 
     private class ListItem : IComparable<ListItem>
     {
+      public object Key { get; private set; }
 
-      private object _key;
-      private int _baseIndex;
+      public int BaseIndex { get; set; }
 
-      public object Key
-      {
-        get { return _key; }
-      }
-
-      public int BaseIndex
-      {
-        get { return _baseIndex; }
-        set { _baseIndex = value; }
-      }
-
       public ListItem(object key, int baseIndex)
       {
-        _key = key;
-        _baseIndex = baseIndex;
+        Key = key;
+        BaseIndex = baseIndex;
       }
 
       public int CompareTo(ListItem other)
@@ -49,20 +38,14 @@
         if (Key is IComparable)
           return ((IComparable)Key).CompareTo(target);
 
-        else
-        {
-          if (Key == null)
-          {
+         if (Key == null)
             if (target == null)
-              return 0;
+               return 0;
             else
-              return -1;
-          }
-          else if (Key.Equals(target))
+               return -1;
+         if (Key.Equals(target))
             return 0;
-          else
-            return Key.ToString().CompareTo(target.ToString());
-        }
+         return Key.ToString().CompareTo(target.ToString());
       }
 
       public override string ToString()
@@ -138,7 +121,7 @@
 
       #region IDisposable Support
 
-      private bool _disposedValue = false; // To detect redundant calls.
+      private bool _disposedValue; // To detect redundant calls.
 
       // IDisposable
       protected virtual void Dispose(bool disposing)
@@ -743,8 +726,7 @@
       {
         _supportsBinding = true;
         _bindingList = (IBindingList)_list;
-        _bindingList.ListChanged +=
-          new ListChangedEventHandler(SourceChanged);
+        _bindingList.ListChanged += SourceChanged;
       }
     }
 
@@ -760,10 +742,7 @@
             if (e.NewIndex == _list.Count - 1)
             {
               object newKey;
-              if (_sortBy != null)
-                newKey = _sortBy.GetValue(newItem);
-              else
-                newKey = newItem;
+              newKey = _sortBy != null ? _sortBy.GetValue(newItem) : newItem;
 
               if (_sortOrder == ListSortDirection.Ascending)
                 _sortIndex.Add(
Index: Windows/BindingSourceRefresh.cs
===================================================================
--- Windows/BindingSourceRefresh.cs    (revision 2235)
+++ Windows/BindingSourceRefresh.cs    (working copy)
@@ -30,8 +30,8 @@
 
     #region Property Fields
 
-    private Exception _exception = null;
-    private Binding _binding = null;
+    private Exception _exception;
+    private Binding _binding;
 
     #endregion
 
@@ -96,7 +96,7 @@
 
     #region Property Fields
 
-    private ContainerControl _host = null;
+    private ContainerControl _host;
 
     #endregion
 
@@ -169,7 +169,7 @@
     /// <summary>
     /// Required designer variable.
     /// </summary>
-    private System.ComponentModel.IContainer components = null;
+    private System.ComponentModel.IContainer components;
 
     /// <summary>
     /// Clean up any resources being used.
@@ -254,16 +254,16 @@
       // If we are to register the events the do so.
       if (register)
       {
-        container.DataBindings.CollectionChanged += new CollectionChangeEventHandler(DataBindings_CollectionChanged);
-        container.ControlAdded += new ControlEventHandler(Container_ControlAdded);
-        container.ControlRemoved += new ControlEventHandler(Container_ControlRemoved);
+        container.DataBindings.CollectionChanged += DataBindings_CollectionChanged;
+        container.ControlAdded += Container_ControlAdded;
+        container.ControlRemoved += Container_ControlRemoved;
       }
       // Else unregister them.
       else
       {
-        container.DataBindings.CollectionChanged -= new CollectionChangeEventHandler(DataBindings_CollectionChanged);
-        container.ControlAdded -= new ControlEventHandler(Container_ControlAdded);
-        container.ControlRemoved -= new ControlEventHandler(Container_ControlRemoved);
+        container.DataBindings.CollectionChanged -= DataBindings_CollectionChanged;
+        container.ControlAdded -= Container_ControlAdded;
+        container.ControlRemoved -= Container_ControlRemoved;
       }
 
       // Reigster the binding complete events for the control.
@@ -281,17 +281,17 @@
     /// </summary>
     /// <param name="control">The control whose binding complete events are to be registered.</param>
     /// <param name="register">True to register the events, false to unregister them.</param>
-    private void RegisterBindingEvents(Control control, bool register)
+    private void RegisterBindingEvents(IBindableComponent control, bool register)
     {
       foreach (Binding binding in control.DataBindings)
       {
         if (register)
         {
-          binding.BindingComplete += new BindingCompleteEventHandler(Control_BindingComplete);
+          binding.BindingComplete += Control_BindingComplete;
         }
         else
         {
-          binding.BindingComplete -= new BindingCompleteEventHandler(Control_BindingComplete);
+          binding.BindingComplete -= Control_BindingComplete;
         }
       }
     }
@@ -379,7 +379,7 @@
 
     #region ISupportInitialize Interface
 
-    private bool _isInitialising = false;
+    private bool _isInitialising;
 
     /// <summary>
     /// BeginInit() is called when the component is starting to be initialised. BeginInit() simply sets the initialisation flag to true.
Index: Windows/ReadWriteAuthorization.cs
===================================================================
--- Windows/ReadWriteAuthorization.cs    (revision 2235)
+++ Windows/ReadWriteAuthorization.cs    (working copy)
@@ -138,15 +138,13 @@
       {
         bool couldRead = ctl.Enabled;
         ctl.Enabled = true;
-        binding.Format -=
-          new ConvertEventHandler(ReturnEmpty);
+        binding.Format -= ReturnEmpty;
         if (!couldRead) binding.ReadValue();
       }
       else
       {
         ctl.Enabled = false;
-        binding.Format +=
-          new ConvertEventHandler(ReturnEmpty);
+        binding.Format += ReturnEmpty;
 
         // clear the value displayed by the control
         PropertyInfo propertyInfo =
@@ -213,13 +211,10 @@
     private static bool IsPropertyImplemented(
       object obj, string propertyName)
     {
-      if (obj.GetType().GetProperty(propertyName,
-        BindingFlags.FlattenHierarchy |
-        BindingFlags.Instance |
-        BindingFlags.Public) != null)
-        return true;
-      else
-        return false;
+      return obj.GetType().GetProperty(propertyName,
+                                       BindingFlags.FlattenHierarchy |
+                                       BindingFlags.Instance |
+                                       BindingFlags.Public) != null;
     }
   }
 }
Index: Wpf/Authorizer.cs
===================================================================
--- Wpf/Authorizer.cs    (revision 2235)
+++ Wpf/Authorizer.cs    (working copy)
@@ -24,7 +24,7 @@
         typeof(VisibilityMode),
         typeof(Authorizer),
         new FrameworkPropertyMetadata(VisibilityMode.Collapsed),
-        new ValidateValueCallback(IsValidVisibilityMode));
+        IsValidVisibilityMode);
 
     // Define method to validate the value
     private static bool IsValidVisibilityMode(object o)
@@ -85,8 +85,8 @@
     /// <param name="prop">The data bound DependencyProperty.</param>
     protected override void FoundBinding(Binding bnd, FrameworkElement control, DependencyProperty prop)
     {
-      SetRead(bnd, (UIElement)control, _source);
-      SetWrite(bnd, (UIElement)control, _source);
+      SetRead(bnd, control, _source);
+      SetWrite(bnd, control, _source);
     }
 
     private void SetWrite(Binding bnd, UIElement ctl, IAuthorizeReadWrite source)
Index: Wpf/CslaDataProvider.cs
===================================================================
--- Wpf/CslaDataProvider.cs    (revision 2235)
+++ Wpf/CslaDataProvider.cs    (working copy)
@@ -24,8 +24,7 @@
     {
       _commandManager = new CslaDataProviderCommandManager(this);
       _factoryParameters = new ObservableCollection<object>();
-      _factoryParameters.CollectionChanged +=
-        new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_factoryParameters_CollectionChanged);
+      _factoryParameters.CollectionChanged += _factoryParameters_CollectionChanged;
     }
 
     void _factoryParameters_CollectionChanged(
@@ -36,7 +35,7 @@
 
     #region Properties
 
-    private Type _objectType = null;
+    private Type _objectType;
     private bool _manageLifetime;
     private string _factoryMethod = string.Empty;
     private ObservableCollection<object> _factoryParameters;
@@ -157,13 +156,15 @@
           return;
       }
 
-      QueryRequest request = new QueryRequest();
-      request.ObjectType = _objectType;
-      request.FactoryMethod = _factoryMethod;
-      request.FactoryParameters = _factoryParameters;
-      request.ManageObjectLifetime = _manageLifetime;
+      QueryRequest request = new QueryRequest
+                                {
+                                   ObjectType = _objectType,
+                                   FactoryMethod = _factoryMethod,
+                                   FactoryParameters = _factoryParameters,
+                                   ManageObjectLifetime = _manageLifetime
+                                };
 
-      if (IsAsynchronous)
+       if (IsAsynchronous)
         System.Threading.ThreadPool.QueueUserWorkItem(DoQuery, request);
       else
         DoQuery(request);
@@ -179,7 +180,7 @@
       try
       {
         // get factory method info
-        BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy;
+        const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy;
         MethodInfo factory = request.ObjectType.GetMethod(
           request.FactoryMethod, flags, null,
           MethodCaller.GetParameterTypes(parameters), null);
@@ -241,22 +242,10 @@
 
     private class QueryRequest
     {
-      private Type _objectType;
+      public Type ObjectType { get; set; }
 
-      public Type ObjectType
-      {
-        get { return _objectType; }
-        set { _objectType = value; }
-      }
+      public string FactoryMethod { get; set; }
 
-      private string _factoryMethod;
-
-      public string FactoryMethod
-      {
-        get { return _factoryMethod; }
-        set { _factoryMethod = value; }
-      }
-
       private ObservableCollection<object> _factoryParameters;
 
       public ObservableCollection<object> FactoryParameters
@@ -265,14 +254,8 @@
         set { _factoryParameters =
           new ObservableCollection<object>(new List<object>(value)); }
       }
-      private bool _manageLifetime;
 
-      public bool ManageObjectLifetime
-      {
-        get { return _manageLifetime; }
-        set { _manageLifetime = value; }
-      }
-   
+      public bool ManageObjectLifetime { get; set; }
     }
 
     #endregion
Index: Wpf/DataDecoratorBase.cs
===================================================================
--- Wpf/DataDecoratorBase.cs    (revision 2235)
+++ Wpf/DataDecoratorBase.cs    (working copy)
@@ -85,8 +85,8 @@
     /// </summary>
     public DataDecoratorBase()
     {
-      this.DataContextChanged += new DependencyPropertyChangedEventHandler(Panel_DataContextChanged);
-      this.Loaded += new RoutedEventHandler(Panel_Loaded);
+      this.DataContextChanged += Panel_DataContextChanged;
+      this.Loaded += Panel_Loaded;
     }
 
     /// <summary>
@@ -132,7 +132,7 @@
     private void UnHookDataContextEvents(object oldValue)
     {
       // unhook any old event handling
-      object oldContext = null;
+      object oldContext;
 
       DataSourceProvider provider = oldValue as DataSourceProvider;
       if (provider == null)
@@ -141,7 +141,7 @@
       }
       else
       {
-        provider.DataChanged -= new EventHandler(DataProvider_DataChanged);
+        provider.DataChanged -= DataProvider_DataChanged;
         oldContext = provider.Data;
       }
       UnHookPropertyChanged(oldContext as INotifyPropertyChanged);
@@ -155,7 +155,7 @@
     private void HookDataContextEvents(object newValue)
     {
       // hook any new event
-      object newContext = null;
+      object newContext;
 
       DataSourceProvider provider = newValue as DataSourceProvider;
       if (provider == null)
@@ -164,7 +164,7 @@
       }
       else
       {
-        provider.DataChanged += new EventHandler(DataProvider_DataChanged);
+        provider.DataChanged += DataProvider_DataChanged;
         newContext = provider.Data;
       }
       HookPropertyChanged(newContext as INotifyPropertyChanged);
@@ -178,39 +178,37 @@
     private void UnHookPropertyChanged(INotifyPropertyChanged oldContext)
     {
       if (oldContext != null)
-        oldContext.PropertyChanged -= new PropertyChangedEventHandler(DataObject_PropertyChanged);
+        oldContext.PropertyChanged -= DataObject_PropertyChanged;
     }
 
     private void HookPropertyChanged(INotifyPropertyChanged newContext)
     {
       if (newContext != null)
-        newContext.PropertyChanged += new PropertyChangedEventHandler(DataObject_PropertyChanged);
+        newContext.PropertyChanged += DataObject_PropertyChanged;
     }
 
     private void UnHookBindingListChanged(IBindingList oldContext)
     {
       if (oldContext != null)
-        oldContext.ListChanged -= new ListChangedEventHandler(DataObject_ListChanged);
+        oldContext.ListChanged -= DataObject_ListChanged;
     }
 
     private void HookBindingListChanged(IBindingList newContext)
     {
       if (newContext != null)
-        newContext.ListChanged += new ListChangedEventHandler(DataObject_ListChanged);
+        newContext.ListChanged += DataObject_ListChanged;
     }
 
     private void UnHookObservableListChanged(INotifyCollectionChanged oldContext)
     {
       if (oldContext != null)
-        oldContext.CollectionChanged -=
-          new NotifyCollectionChangedEventHandler(DataObject_CollectionChanged);
+        oldContext.CollectionChanged -= DataObject_CollectionChanged;
     }
 
     private void HookObservableListChanged(INotifyCollectionChanged newContext)
     {
       if (newContext != null)
-        newContext.CollectionChanged +=
-          new NotifyCollectionChangedEventHandler(DataObject_CollectionChanged);
+        newContext.CollectionChanged += DataObject_CollectionChanged;
     }
 
     private void Panel_Loaded(object sender, RoutedEventArgs e)

Skafa replied on Tuesday, April 01, 2008


This won't work:

-      if (child != null)
+      if (!child.Equals(default(C)))

and

-          if (field == null)
+          if (field.Equals(default(P)))
           {
-            if (newValue != null)
+            if (!newValue.Equals(default(P)))

etc.

if child/field or newValue IS null, a call to Equals() will fail with a NullReferenceException.

dpetrancuri replied on Tuesday, April 01, 2008

What is all of this? And why is it posted in such a 'cryptic' manner?

The innately curious are dying to know.

Thank you.

RockfordLhotka replied on Wednesday, April 02, 2008

He used a unified diff program to display the changes between files.

But it is important to remember that CSLA .NET is now around 8 years old, and has evolved rather a lot over that time. While I do dearly love to have clean, consistent code, it is also true that changing perfectly working code just to look better is an expensive proposition that comes with the risk of accidentally introducing bugs into code that has otherwise been working fine for years (and thus breaking people using CSLA .NET too).

So I clean code when I'm working with it, but I don't plan to do any wholesale cleaning/updating of code throughout the code base. The risk/expense is simply too high.

For example, the poster kindly showed the C# changes, but what about the VB changes? And as was pointed out, the changes would have introduced a bug. And I didn't look that closely, but I'm not sure the changes include consistent use of type inference, which I now believe to be a preferred coding approach.

I'm sure I'll do a bunch of code cleanup as I work through the code for the Expert 2008 Business Objects book, but even then it won't be gratuitous, because of the risk/expense.

Copyright (c) Marimer LLC