-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{759220DD-3017-4331-8F37-7C0B7F2ACDC7}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>Archetype.Examples</RootNamespace> | ||
<AssemblyName>Archetype.Examples</AssemblyName> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<TargetFrameworkProfile /> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<Prefer32Bit>false</Prefer32Bit> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<Prefer32Bit>false</Prefer32Bit> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="nunit.framework"> | ||
<HintPath>..\Archetype.Tests\bin\Debug\nunit.framework.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="NotifyPropertyChangesExamples.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Archetype\Archetype.csproj"> | ||
<Project>{74AAF77C-A943-489A-A78C-3612FA2CC938}</Project> | ||
<Name>Archetype</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
173 changes: 173 additions & 0 deletions
173
src/Archetype.Examples/NotifyPropertyChangesExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
#region License | ||
|
||
// | ||
// Copyright (c) 2012, Ian Davis | ||
// | ||
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). | ||
// See the file LICENSE.txt for details. | ||
// | ||
|
||
#endregion | ||
|
||
#region Using Directives | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using NUnit.Framework; | ||
|
||
#endregion | ||
|
||
namespace Archetype.Examples | ||
{ | ||
public interface INotifyPropertyChanges : INotifyPropertyChanged, INotifyPropertyChanging | ||
{ | ||
void OnPropertyChanged( string propertyName = "" ); | ||
void OnPropertyChanging( string propertyName = "" ); | ||
} | ||
|
||
public class NotifyPropertyChangesModule : INotifyPropertyChanges | ||
{ | ||
#region INotifyPropertyChanges Members | ||
|
||
public event PropertyChangedEventHandler PropertyChanged; | ||
|
||
public event PropertyChangingEventHandler PropertyChanging; | ||
|
||
public virtual void OnPropertyChanged( string propertyName = "" ) | ||
{ | ||
PropertyChangedEventHandler handler = PropertyChanged; | ||
if ( handler != null ) | ||
{ | ||
handler( this, new PropertyChangedEventArgs( propertyName ) ); | ||
} | ||
} | ||
|
||
public virtual void OnPropertyChanging( string propertyName = "" ) | ||
{ | ||
PropertyChangingEventHandler handler = PropertyChanging; | ||
if ( handler != null ) | ||
{ | ||
handler( this, new PropertyChangingEventArgs( propertyName ) ); | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
|
||
public class Person : DelegatingObject | ||
{ | ||
private int _Age; | ||
private string _Name; | ||
|
||
public Person() | ||
: this( new NotifyPropertyChangesModule() ) | ||
{ | ||
} | ||
|
||
public Person( params object[] modules ) | ||
: base( modules ) | ||
{ | ||
} | ||
|
||
public string Name | ||
{ | ||
get { return _Name; } | ||
set | ||
{ | ||
This.OnPropertyChanging( "Name" ); | ||
if ( _Name != value ) | ||
{ | ||
_Name = value; | ||
This.OnPropertyChanged( "Name" ); | ||
} | ||
} | ||
} | ||
|
||
public int Age | ||
{ | ||
get { return _Age; } | ||
set | ||
{ | ||
Inpc.OnPropertyChanging( "Age" ); | ||
if ( _Age != value ) | ||
{ | ||
_Age = value; | ||
Inpc.OnPropertyChanged( "Age" ); | ||
} | ||
} | ||
} | ||
|
||
private dynamic This | ||
{ | ||
get { return this; } | ||
} | ||
|
||
internal INotifyPropertyChanges Inpc | ||
{ | ||
get { return This; } | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class PersonExamples | ||
{ | ||
[Test] | ||
public void SharedModulesToShareBehavior() | ||
{ | ||
var module = new NotifyPropertyChangesModule(); | ||
module.PropertyChanged += | ||
( sender, args ) => Console.WriteLine( "The field {0} has changed.", args.PropertyName ); | ||
module.PropertyChanging += | ||
( sender, args ) => Console.WriteLine( "The field {0} is changing.", args.PropertyName ); | ||
var inigo = new Person( module ) { Name = "Inigo" }; | ||
var ian = new Person( module ) { Name = "Ian" }; | ||
|
||
inigo.Age = 45; | ||
ian.Age = 30; | ||
} | ||
|
||
[Test] | ||
public void UsingModelObjectAsDynamic() | ||
{ | ||
dynamic person = new Person(); | ||
// The cast to the interface will work | ||
INotifyPropertyChanges inpc = person; | ||
inpc.PropertyChanged += | ||
( sender, args ) => Console.WriteLine( "The field {0} has changed.", args.PropertyName ); | ||
inpc.PropertyChanging += | ||
( sender, args ) => Console.WriteLine( "The field {0} is changing.", args.PropertyName ); | ||
// We have full IntelliSense when working with person. | ||
// but now accessing .Name looses IntelliSense | ||
person.Name = "Inigo Montoya"; | ||
} | ||
|
||
[Test] | ||
public void UsingModelObjectAsStronglyTyped() | ||
{ | ||
var person = new Person(); | ||
// Casting first to dynamic triggers the DelegatingObjects casting system | ||
INotifyPropertyChanges inpc = (dynamic) person; | ||
inpc.PropertyChanged += | ||
( sender, args ) => Console.WriteLine( "The field {0} has changed.", args.PropertyName ); | ||
inpc.PropertyChanging += | ||
( sender, args ) => Console.WriteLine( "The field {0} is changing.", args.PropertyName ); | ||
// We have full IntelliSense when working with person. | ||
// We have full IntelliSense when working with person. | ||
person.Name = "Inigo Montoya"; | ||
} | ||
|
||
[Test] | ||
public void UsingModelWithProxyCastingProperty() | ||
{ | ||
var person = new Person(); | ||
// The cast to the interface will work | ||
person.Inpc.PropertyChanged += | ||
( sender, args ) => Console.WriteLine( "The field {0} has changed.", args.PropertyName ); | ||
person.Inpc.PropertyChanging += | ||
( sender, args ) => Console.WriteLine( "The field {0} is changing.", args.PropertyName ); | ||
// We have full IntelliSense when working with person. | ||
// but now accessing .Name looses IntelliSense | ||
person.Name = "Inigo Montoya"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("Archetype.Examples")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("Archetype.Examples")] | ||
[assembly: AssemblyCopyright("Copyright © 2012")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("31c28bdb-61c1-492f-b083-16babf0fa7a4")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |