DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Dynamically adding RaisePropertyChanged to MVVM Light ViewModels with IoC

Pieter De Rycke user avatar by
Pieter De Rycke
·
Dec. 27, 11 · Interview
Like (1)
Save
Tweet
Share
5.12K Views

Join the DZone community and get the full member experience.

Join For Free

in the past, i already wrote two articles about dynamically adding raisepropertychanged support to view models. today, i will explain how this can be accomplished with the ioc container: microsoft unity . unity has out of the box support for aop. using unity is much easier than my two previous do-it-yourselves ways. for real applications, i recommend using unity or similar frameworks!

like in my previous articles, i want to write view models using the following code pattern:

[raisepropertychanged]
public virtual string someproperty { get; set; }

instead of the standard pattern:

private string someproperty;

public string someproperty
{
    get
    {
        return someproperty;
    }
    set
    {
        someproperty = value;
        raisepropertychanged("someproperty");
    }
}

implementation

in order to get started, the first thing we have to do is install both unity and mvvm light. to do this, i recommend using nuget. in the official nuget repository, unity is split into two packages. for the aop support, both packages need to be added to the visual studio project.

nuget_unity

now that we have all the required libraries, we can start coding. first, lets define our raisepropertychangedattribute class.

[attributeusage(attributetargets.property, allowmultiple = false)]
public class raisepropertychangedattribute : handlerattribute
{
    public override icallhandler createhandler(iunitycontainer container)
    {
        return new raisepropertychangedcallhandler();
    }
}

because we want unity to intercept calls, we must extend our attribute from handlerattribute . a custom handlerattribute must create and return a custom icallhandler instance. we will return an instance of our custom raisepropertychangedcallhandler that we will create next.

before writing our icallhander, we must first look at the msil code generated by the c# compiler in order to understand .net properties better. when a property is defined, 2 hidden methods are automatically created by the compiler; a get method and a set method. these methods have by default the name get_<property name> and set_<property name>.

ildasm_property

with this knowledge in mind, the custom icallhandler implementation is straightforward. we ask unity to call the original set method and after its invocation we extract the name of property out of the name of the set method and we invoke the raisepropertychanged method of mvvm light using reflection.

class raisepropertychangedcallhandler : icallhandler
{
    public imethodreturn invoke(imethodinvocation input, getnexthandlerdelegate getnext)
    {
        imethodreturn methodreturn = getnext()(input, getnext);

        string propertyname = input.methodbase.name.substring(4);

        methodinfo raisepropertychangedmethod = input.target.gettype().getmethod("raisepropertychanged",
            bindingflags.instance | bindingflags.nonpublic, null, new type[] {typeof(string)}, null);
        raisepropertychangedmethod.invoke(input.target, new object[] { propertyname });

        return methodreturn;
    }

    public int order { get; set; }
}

configuring unity

all that now remains is configuring unity for our view model and activating virtual method aop support for it. in code, we can define this using the following code sample:

iunitycontainer container = new unitycontainer();
container.addnewextension<interception>();
container.registertype<sampleviewmodel>().
    configure<interception>().
    setinterceptorfor<sampleviewmodel>(new virtualmethodinterceptor());

if we now use unity for creating instances of our sample view model class, all the properties annotated with “raisepropertychanged” will atomically be “property change”-aware.

sampleviewmodel vm = container.resolve<sampleviewmodel>();
please note: this will not work in silverlight, because of limitations in its runtime. (silverlight does not allow the invocation of private methods using reflection for security reasons)

source: http://pieterderycke.wordpress.com/2011/07/28/dynamically-adding-raisepropertychanged-to-mvvm-light-viewmodels-using-microsoft-unity/


Light (web browser) unity Game engine

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?
  • Front-End Troubleshooting Using OpenTelemetry
  • The Power of Zero-Knowledge Proofs: Exploring the New ConsenSys zkEVM
  • Building the Next-Generation Data Lakehouse: 10X Performance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: