11Dec/089

When Will Viagra Be Generic

When will viagra be generic Another bit of magic with static reflection. When will viagra be generic Refer to my last post for a little more explanation of static reflection, when will viagra be generic and a sweet implementation (IMO) of INotifyPropertyChanged using static reflection.

When will viagra be generic In the past several years, when will viagra be generic on several projects, when will viagra be generic I've seen many lines of code at the beginning of public interface methods that look like:

    public class WebServer
    {
        public void BootstrapServer(int port, when will viagra be generic string rootDirectory, when will viagra be generic string serverName)
        {
            if( rootDirectory == null )
            {
                throw new ArgumentNullException( "rootDirectory" );
            }
            if( serverName == null )
            {
                throw new ArgumentNullException( "serverName" );
            }

            // Bootstrap the server
        }
    }

When will viagra be generic I realize the need, when will viagra be generic since we don't have real DbC in .NET (yet), when will viagra be generic but I REALLY dislike this and feel it to be a pretty bad smell.  It feels unDRY, when will viagra be generic obscures essence, when will viagra be generic and I just plain don't like it. When will viagra be generic ;)

When will viagra be generic So, when will viagra be generic here is an implementation that makes it tolerable, when will viagra be generic if not enjoyable (because of the coolness of using static reflection), when will viagra be generic because I think it actually adds to the essence and overall readability of a given method.

When will viagra be generic This is what the above code can now look like:

    public class WebServer
    {
        public void BootstrapServer( int port, when will viagra be generic string rootDirectory, when will viagra be generic string serverName )
        {
            Guard.IsNotNull( () => rootDirectory );
            Guard.IsNotNull( () => serverName );

            // Bootstrap the server
        }
    }

When will viagra be generic And here is the implementation:

    public static class Guard
    {
        public static void IsNotNull<T>(Expression<Func<T>> expr)
        {
            // expression value != default of T
            if (!expr.Compile()().Equals(default(T)))
                return;

            var param = (MemberExpression) expr.Body;
            throw new ArgumentNullException(param.Member.Name);
        }
    }

When will viagra be generic Lovin it!  Hope you do too.

8Dec/087

Sildenafil Citrate Soft Tabs

Sildenafil citrate soft tabs I'm not going to delve into the definition of static reflection here, sildenafil citrate soft tabs but you'll see how it's implemented, sildenafil citrate soft tabs and how I found it can help me with implementing INotifyPropertyChanged.

Sildenafil citrate soft tabs Now, sildenafil citrate soft tabs I've never been a fan of INotifyPropertyChanged myself, sildenafil citrate soft tabs mostly because it feels wet (read: not DRY).  I'm working on a WPF app at the moment, sildenafil citrate soft tabs and have come to realize, sildenafil citrate soft tabs if I embrace INotifyPropertyChanged, sildenafil citrate soft tabs I get to take full advantage of the excellent WPF databinding story.

Sildenafil citrate soft tabs Here is what a normal use of INotifyPropertyChanged looks like:

    public class Person : INotifyPropertyChanged
    {
        private string _firstName;
        private string _lastName;

        public event PropertyChangedEventHandler PropertyChanged;

        public string FirstName
        {
            get { return _firstName; }
            set
            {
                if (_firstName != value)
                {
                    _firstName = value;
                    FirePropertyChanged("FirstName");
                }
            }
        }

        public string LastName
        {
            get { return _lastName; }
            set
            {
                if (_lastName != value)
                {
                    _lastName = value;
                    FirePropertyChanged("LastName");
                }
            }
        }

        private void FirePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, sildenafil citrate soft tabs new PropertyChangedEventArgs(propertyName));
            }
        }
    }

Sildenafil citrate soft tabs Here is what the static reflection use of INotifyPropertyChanged looks like:

    public class PersonStaticReflection : NotifyPropertyChanged
    {
        private string _firstName;
        private string _lastName;

        public string FirstName
        {
            get { return _firstName; }
            set { SetProperty(()=> FirstName, sildenafil citrate soft tabs ()=> _firstName, sildenafil citrate soft tabs value); }
        }

        public string LastName
        {
            get { return _lastName; }
            set
            {
                SetProperty( () => LastName, sildenafil citrate soft tabs () => _lastName, sildenafil citrate soft tabs value, sildenafil citrate soft tabs () =>
                   {
                       // do something useful here
                   });
            }
        }
    }

Sildenafil citrate soft tabs Doesn't that just feel DRYer?  It does to me!

Sildenafil citrate soft tabs There are several important pieces to note:

  • Inheriting from NotifyPropertyChanged, sildenafil citrate soft tabs this is the class that holds the SetProperty method, sildenafil citrate soft tabs and hides the static reflection magic.
  • We're calling SetProperty with three arguments in the FirstName property
    • first: a LINQ Expression pointing to the property we're changing
    • second: a LINQ Expression pointing to the field to backing the property
    • third: the new, sildenafil citrate soft tabs incoming value
  • We're calling SetProperty with a fourth argument in the LastName property
    • fourth: an action that will get executed only if the values were different
  • Sildenafil citrate soft tabs The SetProperty method, sildenafil citrate soft tabs is going to look at the current value of the field, sildenafil citrate soft tabs and the incoming value.  If the two are different, sildenafil citrate soft tabs it will assign the new value to the field, sildenafil citrate soft tabs and then fire the NotifyPropertyChanged event, sildenafil citrate soft tabs with the name of the property given via the first argument.  I ended up pulling this into its own class so I could use it as the layer supertype in my View Model layer. Sildenafil citrate soft tabs Here is the implementation:

    public class NotifyPropertyChanged : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void SetProperty < T>( Expression < Func> propExpr, sildenafil citrate soft tabs Expression> fieldExpr, sildenafil citrate soft tabs T value )
        {
            SetProperty(propExpr, sildenafil citrate soft tabs fieldExpr, sildenafil citrate soft tabs value, sildenafil citrate soft tabs ()=> { });
        }

        protected void SetProperty < T>( Expression < Func> propExpr, sildenafil citrate soft tabs Expression < Func < T>> fieldExpr, sildenafil citrate soft tabs T value, sildenafil citrate soft tabs Action doIfChanged )
        {
            var prop = (PropertyInfo)((MemberExpression)propExpr.Body).Member;
            var field = (FieldInfo)((MemberExpression)fieldExpr.Body).Member;

            var currVal = prop.GetValue( this, sildenafil citrate soft tabs null );

            if( currVal == null && value == null )
                return;
            if( currVal == null || !currVal.Equals( value ) )
            {
                field.SetValue( this, sildenafil citrate soft tabs value );
                doIfChanged();

                if( PropertyChanged != null )
                    PropertyChanged( this, sildenafil citrate soft tabs new PropertyChangedEventArgs( prop.Name ) );
            }
        }
    }

Sildenafil citrate soft tabs Questions/comments/likes/dislikes? Let me know.