27Aug/102

What Is Viagra Used For

What is viagra used for I hate every time I am working on something and I have to implement INotifyPropertyChanged.  My DRY-dey sense tingles.  Not only am I forced to not use auto-properties (1st DRY violation), what is viagra used for I’m forced to fire the event in each setter (2nd DRY violation), what is viagra used for and specify the name of the property that is getting set, what is viagra used for inside of that property’s setter (3rd DRY violation).  That much WET (read: not-DRY), what is viagra used for for something so simple leaves me a little grumpy.

What is viagra used for I’ve been on this quest before, what is viagra used for to simplify this a bit, what is viagra used for but it was still a little hackety, what is viagra used for and limiting.

What is viagra used for This time, what is viagra used for I set out to do it right.

What is viagra used for I’ll spare you most of the technical details, what is viagra used for but it’s backed by Castle’s DynamicProxy project, what is viagra used for and there’s some integration with StructureMap to make it super easy, what is viagra used for though you don’t really have to use StructureMap if you don’t want to.  [note:  I’ll probably add more container support as I find time.  If you have a specific need, what is viagra used for let me know, what is viagra used for or submit a patch.]

What is viagra used for Here are the codez to show it in action:

Basics

Using it for a class with an interface

// note the attribute goes on the interface, what is viagra used for not the class

[AutoNotify]

public interface IFoo

{

    string Value { get; set; }

}

 

public class Foo : IFoo

{

    public string Value { get; set; }

}

 

Using it for a class

[AutoNotify(Fire = FireOptions.OnlyOnChange)]

public class Foo

{

    // note for autonotify to work, what is viagra used for the property must be virtual

    public virtual string Value { get; set; }

}

 

What is viagra used for The previous example shows how to get the event to fire only when the value is different also.  It defaults to always firing, what is viagra used for whether the value changes or not.  It’s also important to note that your properties need to be virtual so the calls to the setter can be intercepted.

Dependent Properties

What is viagra used for Sometimes (usually) you’ve got calculated properties that need to fire the notified event too, what is viagra used for these usually turn into WET mess as well.  We’ve got the problem solved, what is viagra used for and you’ve got a few different options, what is viagra used for hopefully one of them suits you.

Dependency Map – DependsOn

What is viagra used for You specify the type that defines the DependencyMap on the attribute, what is viagra used for and then set up your dependencies in that type’s constructor.  This style is somewhat influenced by the FluentNHibernate API.

[AutoNotify(DependencyMap = typeof(ProjectDependency))]

public class Project

{

    public virtual string Name { get; set; }

    public virtual string[] Files { get; set; }

    public virtual int FileCount { get { return Files.Length; } }

}

 

class ProjectDependency : DependencyMap<Project>

{

    public ProjectDependency()

    {

        Property(x => x.FileCount).DependsOn(x => x.Files);

    }

}

 

Dependency Map – Updates

What is viagra used for If you’d rather express your dependency the other way around, what is viagra used for that’s fine too.  The two are equivalent.

[AutoNotify(DependencyMap = typeof(ProjectDependency))]

public class Project

{

    public virtual string Name { get; set; }

    public virtual string[] Files { get; set; }

    public virtual int FileCount { get { return Files.Length; } }

}

 

class ProjectDependency : DependencyMap<Project>

{

    public ProjectDependency()

    {

        Property(x => x.Files).Updates(x => x.FileCount);

    }

}

 

Dependency Map – UpdatesWith

What is viagra used for If you want to stick with an auto-property, what is viagra used for and leave the calculated property logic somewhere else, what is viagra used for you can hook it in via your dependency map too.  This example, what is viagra used for again, what is viagra used for is equivalent to the previous two.

[AutoNotify(DependencyMap = typeof(ProjectDependency))]

public class Project

{

    public virtual string Name { get; set; }

    public virtual string[] Files { get; set; }

    public virtual int FileCount { get; set; }

}

 

class ProjectDependency : DependencyMap<Project>

{

    public ProjectDependency()

    {

        Property(x => x.Files).Updates(x => x.FileCount).With(p => p.Files.Length);

    }

}

 

DependsOn Attribute

What is viagra used for If you don’t like any of those options and are looking for something a little more simple, what is viagra used for maybe you’ll like this one.  Just specify which things your property depends on in an attribute.  You lose your 100% static typing help, what is viagra used for but it’s more concise.

[AutoNotify]

public class Project

{

    public virtual string Name { get; set; }

    public virtual string[] Files { get; set; }

 

    [DependsOn("Files")]

    public virtual int FileCount { get { return Files.Length; } }

}

 

Containers and otherwise

Hooking it into StructureMap

What is viagra used for There are a couple conventions you can use to hook into StructureMap.  There is the attribute convention (which is what you’re seeing above), what is viagra used for and there is a generic predicate convention that you can use any predicate logic.  Below you can see the attribute one getting hooked in.

var container = new Container(config => config.Scan(scanConfig =>

{

    scanConfig.With(new AutoNotifyAttrConvention());

    scanConfig.TheCallingAssembly();

    scanConfig.WithDefaultConventions();

}));

 

var project = container.GetInstance<Project>();

Using it without StructureMap

What is viagra used for If you’re using another container, what is viagra used for or no container at all, what is viagra used for but want to use some other factory or something, what is viagra used for you can do that too.  This example is for something with an interface.  It’s very similar to do the same for a concrete class… you just don’t instantiate the object first.  You also have an opportunity to hook into the dependent property structure here as well with the DependencyMap parameter.

var foo = new Foo();

var notifiableFoo = Notifiable.MakeForInterface(

    typeof(IFoo), what is viagra used for

    foo, what is viagra used for

    FireOptions.Always, what is viagra used for

    new ProxyGenerator(), what is viagra used for

    DependencyMap.Empty);

 

Assert.That(notifiableFoo is INotifyPropertyChanged);

 

Whew, what is viagra used for done

What is viagra used for So… that’s a lot of ‘how to’, what is viagra used for but hopefully it’ll be somewhat complete introduction to get you working with it.  I really don’t see much of any reason to ever implement INotifyPropertyChanged ever again (unless you are in an environment where you can’t use DynamicProxy).  It can automatically be done for you from now on.

What is viagra used for The code is up on github, what is viagra used for and there is a gem up on rubygems if you’re using nu or noodle+bundler.  Fork it, what is viagra used for send me a patch, what is viagra used for use it, what is viagra used for send feedback, what is viagra used for etc.  I hope you love it!

Comments (2) Trackbacks (0)
  1. Very interesting, and elegant. I’ve been iterating on a ViewModelBase class that handles INPC in a slightly different way. Basically it follows the same approach as Dependency Properties (register a static key for property lookup/retrieval). But my ViewModelProperty is strongly typed. I have the concept of dependent properties as well as property coercion (i.e. MaxValue can never be less than MinValue). Also I have plumbing to simplify sending/receiving messages to /from an Event Aggregator.

    It’s not quite ready for public consumption, but I’ll be sharing more details at a future date.


Leave a comment


No trackbacks yet.