Skip to content

Castle.DynamicProxy based library that provides a way to implement INotifyPropertyChanged automatically using AOP style.

License

Notifications You must be signed in to change notification settings

Serg046/AopInpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AopInpc

Build status NuGet version

Castle.DynamicProxy based library that provides a way to implement INotifyPropertyChanged automatically using AOP style.

Declaration:

public class ViewModel : INotifyPropertyChangedCaller
{
    [Inpc]
    public virtual string Property { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(string propertyName)
        => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

Usage:

  • AopInpcFactory
static void Main(string[] args)
{
    var viewModel = AopInpcFactory.Create<ViewModel>();
    //var viewModel = AopInpcFactory.Decorate(new ViewModel());
    viewModel.PropertyChanged += (sender, eventArgs)
        => Console.WriteLine($"The property \"{eventArgs.PropertyName}\" was updated with value \"{viewModel.Property}\".");
    viewModel.Property = "Hello world!";
}
  • DI-container
static void Main(string[] args)
{
    var containerBuilder = new ContainerBuilder();
    containerBuilder.RegisterType<InpcInterceptor>();
    containerBuilder.RegisterType<ViewModel>()
        .EnableClassInterceptors()
        .InterceptedBy(typeof(InpcInterceptor));
    var container = containerBuilder.Build();

    var viewModel = container.Resolve<ViewModel>();
    viewModel.PropertyChanged += (sender, eventArgs)
        => Console.WriteLine($"The property \"{eventArgs.PropertyName}\" was updated with value \"{viewModel.Property}\".");
    viewModel.Property = "Hello world!";
}
  • Castle.DynamicProxy ProxyGenerator
static void Main(string[] args)
{
    var viewModel = new ProxyGenerator().CreateClassProxy<ViewModel>(new InpcInterceptor());
    viewModel.PropertyChanged += (sender, eventArgs)
        => Console.WriteLine($"The property \"{eventArgs.PropertyName}\" was updated with value \"{viewModel.Property}\".");
    viewModel.Property = "Hello world!";
}

Output:

The property "Property" was updated with value "Hello world!".

About

Castle.DynamicProxy based library that provides a way to implement INotifyPropertyChanged automatically using AOP style.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages