| Severity | Error |
| Reported by | PostSharp.Patterns.Model |
| Message | Depends.On cannot be called in method <Method>: call it in the getter of the property, or in a method that the getter calls directly. |
Cause
On declares the dependencies of a property for the NotifyPropertyChangedAttribute aspect, which reads the call when it analyzes the getter at build time. The call has no effect at run time. It replaces the automatic analysis of the method that contains it, so it belongs in the getter of the property.
The aspect accepts the call in the getter and in a method that the getter calls directly. When it finds the call in a method that the getter reaches only through another method, it reports DOM012 on the property, not on the call. <Method> is the method that contains the call.
In the following example, the getter of Total calls ComputeTotal, which calls AddShipping, which calls On.
using System;
using System.ComponentModel;
using PostSharp.Patterns.Model;
namespace Errors.DOM012_Error;
[NotifyPropertyChanged]
public class Order
{
private decimal subtotal;
private decimal shippingCost;
// The getter reaches Depends.On through two method calls.
public decimal Total => this.ComputeTotal();
public void Update( decimal subtotal, decimal shippingCost )
{
this.subtotal = subtotal;
this.shippingCost = shippingCost;
}
private decimal ComputeTotal() => this.AddShipping( this.subtotal );
private decimal AddShipping( decimal amount )
{
Depends.On( this.shippingCost );
return amount + this.shippingCost;
}
}
public static class Program
{
public static void Main()
{
Order order = new Order();
( (INotifyPropertyChanged) order ).PropertyChanged += ( _, e ) => Console.WriteLine( $"{e.PropertyName} changed." );
order.Update( 100, 5 );
Console.WriteLine( $"Total: {order.Total}" );
}
}
error DOM012: DOM012_Error.cs(14,20): Depends.On cannot be called in method Errors.DOM012_Error.Order.AddShipping(System.Decimal): call it in the getter of the property, or in a method that the getter calls directly.
Resolution
Move the call to On into the getter of the property. Because the call replaces the automatic analysis of the getter, including the methods that it calls, list all the dependencies of the property in the call.
In the following example, the getter of Total declares both fields that the property depends on, and the aspect notifies Total when they change.
using System;
using System.ComponentModel;
using PostSharp.Patterns.Model;
namespace Errors.DOM012_Fixed;
[NotifyPropertyChanged]
public class Order
{
private decimal subtotal;
private decimal shippingCost;
public decimal Total
{
get
{
// Depends.On is called in the getter, and lists every dependency of the property.
Depends.On( this.subtotal, this.shippingCost );
return this.ComputeTotal();
}
}
public void Update( decimal subtotal, decimal shippingCost )
{
this.subtotal = subtotal;
this.shippingCost = shippingCost;
}
private decimal ComputeTotal() => this.AddShipping( this.subtotal );
private decimal AddShipping( decimal amount )
{
return amount + this.shippingCost;
}
}
public static class Program
{
public static void Main()
{
Order order = new Order();
( (INotifyPropertyChanged) order ).PropertyChanged += ( _, e ) => Console.WriteLine( $"{e.PropertyName} changed." );
order.Update( 100, 5 );
Console.WriteLine( $"Total: {order.Total}" );
}
}
Total changed.
Total: 105
See Also
Reference
On
NotifyPropertyChangedAttribute
Other Resources