using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using DevExpress.Mvvm; using DevExpress.Mvvm.POCO; using DevExpress.Mvvm.DataAnnotations; using DevExpress.DevAV.Common.Utils; using DevExpress.Mvvm.DataModel; namespace DevExpress.DevAV.Common.ViewModel { /// /// A POCO view model exposing a read-only collection of entities of a given type. It is designed for quick navigation between collection views. /// This is a partial class that provides an extension point to add custom properties, commands and override methods without modifying the auto-generated code. /// /// A navigation token type. /// An entity type. /// A primary key value type. /// A unit of work type. public partial class PeekCollectionViewModel : CollectionViewModelBase where TEntity : class where TUnitOfWork : IUnitOfWork { /// /// Creates a new instance of PeekCollectionViewModel as a POCO view model. /// /// Identifies the module that is the navigation target. /// A factory that is used to create a unit of work instance. /// A function that returns a repository representing entities of a given type. /// An optional parameter that provides a LINQ function used to customize a query for entities. The parameter, for example, can be used for sorting data. public static PeekCollectionViewModel Create( TNavigationToken navigationToken, IUnitOfWorkFactory unitOfWorkFactory, Func> getRepositoryFunc, Func, IQueryable> projection = null) { return ViewModelSource.Create(() => new PeekCollectionViewModel(navigationToken, unitOfWorkFactory, getRepositoryFunc, projection)); } TNavigationToken navigationToken; TEntity pickedEntity; /// /// Initializes a new instance of the PeekCollectionViewModel class. /// This constructor is declared protected to avoid an undesired instantiation of the PeekCollectionViewModel type without the POCO proxy factory. /// /// Identifies the module that is the navigation target. /// A factory that is used to create a unit of work instance. /// A function that returns a repository representing entities of a given type. /// An optional parameter that provides a LINQ function used to customize a query for entities. The parameter, for example, can be used for sorting data. protected PeekCollectionViewModel( TNavigationToken navigationToken, IUnitOfWorkFactory unitOfWorkFactory, Func> getRepositoryFunc, Func, IQueryable> projection = null ) : base(unitOfWorkFactory, getRepositoryFunc, projection, null, true) { this.navigationToken = navigationToken; } /// /// Navigates to the corresponding collection view and selects the given entity. /// Since PeekCollectionViewModel is a POCO view model, an instance of this class will also expose the NavigateCommand property that can be used as a binding source in views. /// /// An entity to select within the collection view. [Display(AutoGenerateField = false)] public void Navigate(TEntity projectionEntity) { pickedEntity = projectionEntity; SendSelectEntityMessage(); Messenger.Default.Send(new NavigateMessage(navigationToken), navigationToken); } /// /// Determines if a navigation to corresponding collection view can be performed. /// Since PeekCollectionViewModel is a POCO view model, this method will be used as a CanExecute callback for NavigateCommand. /// /// An entity to select in the collection view. public bool CanNavigate(TEntity projectionEntity) { return projectionEntity != null; } protected override void OnInitializeInRuntime() { base.OnInitializeInRuntime(); Messenger.Default.Register(this, x => SendSelectEntityMessage()); } void SendSelectEntityMessage() { if(IsLoaded && pickedEntity != null) Messenger.Default.Send(new SelectEntityMessage(CreateRepository().GetProjectionPrimaryKey(pickedEntity))); } } }