Rajnish Noonia

Category: C#

  • ViewModel First – ViewCaching

    With View model first approach, view are usually created via DataTemplate, and each time a view model is injected in the content control, the corresponding view is recreated.

    If you have complex view where it take bit time to create view, you may see the performance hits. To avoid the performance hit you may want to cache the view and use the cached view when available. This can be done via creating CacheContentControl and you choice of view factory.

    The basic idea is to wrap the view inside CacheContentControl and delegate the view creation logic to your view factory. The attached sample uses weakreference view factory,say if GC has not collected the view, you may use the cached view instead of creating view each time. This will work with controls like tab control docking controls etc.

    The CacheContentControl  code

    public class CacheContentControl : ContentControl
        {
            public CacheContentControl()
            {
                Unloaded += ViewCache_Unloaded;
                ViewFactory = WeakReferenceViewFactory.Instance;
            }
    
            void ViewCache_Unloaded(object sender, RoutedEventArgs e)
            {
                Content = null;
            }
    
            private Type _contentType;
            public Type ContentType
            {
                get { return _contentType; }
                set
                {
                    _contentType = value;
                   //  use you favorite factory
                    Content = ViewFactory.GetView(value);
                }
            }
    
            public IViewFactory ViewFactory { get; private set; }
        }
    

    In the DataTemplate, use the ViewCache, pass the type of the real view you want to use:

    <DataTemplate DataType=”{x:Type moduleB:Panel3Vm}”>

    <Border Background=”Green”>

    <local:CacheContentControl ContentType=”{x:Type moduleB:Pane3 }” Margin=”5″/>

    </Border>

    </DataTemplate>

    The example uses standard data template (red option) to demonstrate each time you navigate to panes the corresponding views are recreated with new hash code.

    If you select CacheContentControl (green option) and navigate the view is only created once. If you click on button to collected the GC then view are created on navigation if they are claimed by GC.

    Why ViewModel first ?

    I prefer to use view model first approach. For many reasons:

    • Vms are your application containing most of logic apart from glue code in form of behaviors or triggers.
    • If you creates views then you are responsible for its life and cleanup code. You have to deal with threading and other issues which are difficult to test. On the other hand of you create vms and leave the view creation logic with WPF via data template.. you don’t have to worry about threading issue. And there will be better separation of concerns.
    • With vm first approach zero code behind.
    • With a project level isolation for view and vms you can restrict developers using view specific things like dispatcher in the view model leaving more cleaner and testable code base. I.e view project sprojec to vm. And vm project should not refer to any presentation lib.
    • If there is clear boundary between view and vm. Both can evolve and will be less fragile.
    • Allows more complete testing of logic to open new Views and ViewModels
    • Tends to be DRYer (don’t repeat yourself ) as applications get larger
    • View and ViewModel are more independent and can be worked on separately more easily.

    For more details see attached source code (download and rename docx to zip)

    Viewcache Source Code

  • RadDocking With Prism + Mvvm

    Features

    • Support MVVM with Prism
    • Supports ViewModel First or ViewFirst Approach
    • Supports styling RadPane in xaml rather than DockingPansFactory
    • Code RadDocking – SourceCode

    The XAML

    • Defiine the region names at RadPaneGroup level
    • Set Rad Panel Style via ItemContainerStyle
    • Bind Header property of RadPane with Title Property in View Model
     <Window.Resources>
     <Style x:Key="RadPaneFactoryStyle" TargetType="{x:Type telerik:RadPane}">
     <Setter Property="Header" Value="{Binding Title}"/>
     </Style>
     </Window.Resources>
     <Grid>
     <telerikDocking:RadDocking>
     <telerikDocking:RadDocking.DocumentHost>
     <telerikDocking:RadSplitContainer >
     <telerikDocking:RadPaneGroup regions:RegionManager.RegionName="MainRegion"
     ItemContainerStyle="{StaticResource RadPaneFactoryStyle}"
     >
     
     </telerikDocking:RadPaneGroup>
     </telerikDocking:RadSplitContainer>
     </telerikDocking:RadDocking.DocumentHost>
    
     <telerikDocking:RadSplitContainer InitialPosition="DockedRight">
     <telerikDocking:RadPaneGroup regions:RegionManager.RegionName="MainRegion1" >
     
     </telerikDocking:RadPaneGroup>
     </telerikDocking:RadSplitContainer>
     
     </telerikDocking:RadDocking> 
     </Grid>
    

    Configure Region Adapter Mappings in Bootstrap

    protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
     {
     var mappings = base.ConfigureRegionAdapterMappings();
    
     mappings.RegisterMapping(typeof(RadPaneGroup), this.Container.Resolve<RadPaneGroupRegionAdapter>());
    
     return mappings;
     }
    

    Define the RadPaneGroup RegionAdapter

     
    Behavior
    public class RadPaneGroupRegionAdapter : RegionAdapterBase<RadPaneGroup>
     {
     public RadPaneGroupRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
     : base(regionBehaviorFactory)
     {
     }
    
     protected override void Adapt(IRegion region, RadPaneGroup regionTarget)
     {
     var docking = regionTarget.ParentOfType<RadDocking>();
     if (docking.DockingPanesFactory == null)
     {
     docking.DockingPanesFactory = new CustomDockingPanesFactory();
     }
    
     var store = DocumentsStoreAttachment.GetPanesStore(docking);
     if (store == null)
     {
     store = new PanesStore();
     DocumentsStoreAttachment.SetPanesStore(docking, store);
     BindingOperations.SetBinding(
     docking,
     RadDocking.PanesSourceProperty,
     new Binding("PanesSource") { Source = store });
     }
     }
    
     protected override IRegion CreateRegion()
     {
     return new SingleActiveRegion();
     }
    
     protected override void AttachBehaviors(IRegion region, RadPaneGroup regionTarget)
     {
     if (region == null)
     throw new System.ArgumentNullException("region");
    
     var docking = regionTarget.ParentOfType<RadDocking>();
     
     // Add the behavior that syncs the items source items with the rest of the items
     region.Behaviors.Add(RadPaneGroupSyncBehavior.BehaviorKey, new RadPaneGroupSyncBehavior()
     {
     HostControl = regionTarget,
     Docking = docking
     });
    
     base.AttachBehaviors(region, regionTarget);
     }
    
     private class PanesStore
     {
     public readonly ObservableCollection<object> Panes = new ObservableCollection<object>();
     ReadOnlyObservableCollection<object> _readonlyDocumentsList;
     public ReadOnlyObservableCollection<object> PanesSource
     {
     get
     {
     return _readonlyDocumentsList ??
     (_readonlyDocumentsList = new ReadOnlyObservableCollection<object>(Panes));
     }
     }
     }
    
     private static class DocumentsStoreAttachment
     {
     public static readonly DependencyProperty PanesStoreProperty = DependencyProperty.RegisterAttached(
     "PanesStore",
     typeof(PanesStore),
     typeof(DocumentsStoreAttachment),
     new PropertyMetadata(null)
     );
     public static void SetPanesStore(UIElement element, PanesStore value)
     {
     element.SetValue(PanesStoreProperty, value);
     }
     public static PanesStore GetPanesStore(UIElement element)
     {
     return (PanesStore)element.GetValue(PanesStoreProperty);
     }
     }
    
     private class CustomDockingPanesFactory : DockingPanesFactory
     {
     public RadPaneGroup ActiveGroup { get; internal set; }
    
     protected override void AddPane(RadDocking radDocking, RadPane pane)
     {
     var group = ActiveGroup;
    
     pane.SetValue(RadPane.HeaderProperty, DependencyProperty.UnsetValue);
    
     var style = group.ItemContainerStyle;
     if (style != null)
     {
     pane.SetValue(RadPane.StyleProperty, style);
     }
     group?.Items.Add(pane);
     }
     }
    
     private class RadPaneGroupSyncBehavior : RegionBehavior, IHostAwareRegionBehavior
     {
     public static readonly string BehaviorKey = "RadPaneGroupSyncBehavior";
     private bool _updatingActiveViewsInManagerActiveContentChanged;
     private RadPaneGroup _radPaneGroup;
     private PanesStore _documentStore;
    
     public DependencyObject HostControl
     {
     get
     {
     return _radPaneGroup;
     }
    
     set
     {
     _radPaneGroup = value as RadPaneGroup;
     }
     }
    
    
    
     public RadDocking Docking { get; internal set; }
    
     /// <summary>
     /// Starts to monitor the <see cref="IRegion"/> to keep it in synch with the items of the <see cref="HostControl"/>.
     /// </summary>
     protected override void OnAttach()
     {
     bool itemsSourceIsSet = _radPaneGroup.ItemsSource != null;
    
    
     if (itemsSourceIsSet)
     {
     throw new InvalidOperationException();
     }
    
     SynchronizeItems();
    
     Docking.ActivePaneChanged += ManagerActiveContentChanged;
     Region.ActiveViews.CollectionChanged += ActiveViews_CollectionChanged;
     Region.Views.CollectionChanged += Views_CollectionChanged;
     Docking.Close += _dockingManager_DocumentClosed;
    
     }
    
     private void _dockingManager_DocumentClosed(object sender, StateChangeEventArgs e)
     {
     foreach (var pane in e.Panes)
     {
     if (Region.Views.Contains(pane.Content))
     {
     Region.Remove(pane.Content);
     }
     }
    
     }
    
    
     private void Views_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
     {
     if (e.Action == NotifyCollectionChangedAction.Add)
     {
     int startIndex = e.NewStartingIndex;
    
     foreach (var newItem in e.NewItems)
     {
     var panesFactory = ((CustomDockingPanesFactory)Docking.DockingPanesFactory);
     panesFactory.ActiveGroup = _radPaneGroup;
     _documentStore.Panes.Insert(0, newItem);
     }
    
     }
     else if (e.Action == NotifyCollectionChangedAction.Remove)
     {
     foreach (object oldItem in e.OldItems)
     {
     _documentStore.Panes.Remove(oldItem);
     }
     }
     }
    
     private void SynchronizeItems()
     {
     var store = DocumentsStoreAttachment.GetPanesStore(Docking);
    
    
    
     _documentStore = store;
     foreach (object view in Region.Views)
     {
     ((CustomDockingPanesFactory)Docking.DockingPanesFactory).ActiveGroup = _radPaneGroup;
     _documentStore.Panes.Insert(0, view);
    
     }
     }
    
     private void ActiveViews_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
     {
     if (_updatingActiveViewsInManagerActiveContentChanged)
     {
     return;
     }
    
     if (e.Action == NotifyCollectionChangedAction.Add)
     {
     if (Docking.ActivePane != null
     && Docking.ActivePane != e.NewItems[0]
     && Region.ActiveViews.Contains(Docking.ActivePane.Content))
     {
     Region.Deactivate(Docking.ActivePane.Content);
     }
    
     var pane = Docking.Panes.FirstOrDefault(x => x.Content.Equals(e.NewItems[0]));
     Docking.ActivePane = pane;
     }
     else if (e.Action == NotifyCollectionChangedAction.Remove &&
     e.OldItems.Contains(Docking.ActivePane))
     {
     Docking.ActivePane = null;
     }
     }
    
     private void ManagerActiveContentChanged(object sender, EventArgs e)
     {
     try
     {
     _updatingActiveViewsInManagerActiveContentChanged = true;
    
     if (Docking.Equals(sender))
     {
     var activePane = Docking.ActivePane;
     if (activePane != null)
     {
     var activeViews = Region.ActiveViews.Where(it => it != activePane.Content).ToList();
     foreach (var item in activeViews)
     {
     Region.Deactivate(item);
     }
    
     if (Region.Views.Contains(activePane.Content) && !Region.ActiveViews.Contains(activePane.Content))
     {
     Region.Activate(activePane.Content);
     }
     }
    
    
    
     }
     }
     finally
     {
     _updatingActiveViewsInManagerActiveContentChanged = false;
     }
     }
    
     }
     }
  • Cross AppDomain Tasks

    As a part of plugin system, I build a framework that allows you to host plugins in current app domain, new app domain or external process host. The next channel is to run an long running task in new app domain and control it from any side of wire and here is the simple API with help of two classes..

    public class RemoteTask : MarshalByRefObject
     {
     private readonly EventWaitHandle _completionEvent;
     
     public bool IsCanceled { get; protected set; }
    
     public bool IsCompleted { get; protected set; }
    
     public Exception Exception { get; protected set; }
    
     public bool IsFaulted { get; protected set; }
    
     public string Id { get; private set; }
    
     
     protected RemoteTask(CancellationToken cancellationToken)
     {
     Id = Guid.NewGuid().ToString();
     _completionEvent = new EventWaitHandle(false, EventResetMode.ManualReset, Id);
    
     cancellationToken.Register(() =>
     {
     IsCompleted = false;
     IsCanceled = true;
     Finished();
     });
     }
    
     public RemoteTask(CancellationToken cancellationToken, TaskAwaiter awaiter) : this(cancellationToken)
     {
     awaiter.OnCompleted(() =>
     {
     try
     {
     awaiter.GetResult();
     IsFaulted = false;
     IsCompleted = awaiter.IsCompleted;
     }
     catch (Exception exception)
     {
     Exception = exception;
     IsFaulted = true;
     }
     Finished();
     });
     }
    
     protected void Finished()
     {
     _completionEvent.Set();
     }
    
     public static void WaitWorker(RemoteTask worker)
     {
     var waiter = new EventWaitHandle(false, EventResetMode.ManualReset, worker.Id);
     waiter.WaitOne();
     }
     }

    And class RemoteTaskOfT if you need results out from your task

    public class RemoteTask : RemoteTask
        {
            public RemoteTask(CancellationToken cancellationToken, TaskAwaiter awaiter):base(cancellationToken)
            {
                awaiter.OnCompleted(() =>
                {
                    try
                    {
                        Result = awaiter.GetResult();
                        IsFaulted = false;
                        IsCompleted = awaiter.IsCompleted;
                    }
                    catch (Exception exception)
                    {
                        Exception = exception;
                        IsFaulted = true;
                    }
                    Finished();
                });
            }
    
            public T Result { get; private set; }
        }
    

    to use it from inside the new app domain
    just return new RemoteTask(cancellationToken, task.GetAwaiter())
    and on client side use
    RemoteTask.WaitWorker(worker);
    int result = worker.Result;
    Exception ex = worker.Exception;

  • Asp.Net MVC flavours

    The main purpose of the MVC (Model, View and Controller) architecture is to make separation of the business layer (logic) and the application layer (data)  from the presentation layer to the user. Model-View-Controller is a software pattern for achieving isolation between different application components. Its always desirable for software applications (especially web-based applications) that there must be clear separation between business logic and the user interface. A model represents the state of a particular aspect of the application. A controller handles interactions and updates the model to reflect a change in state of the application, and then passes information to the view. A view accepts necessary information from the controller and renders a user interface to display that information. Over past few years many version of ASP.Net MVC were release moving towards more and more mature framework for web development.Below is brief comparison of features release in each version

    MVC 5 – October 2013

    • Bootstrap replaced the default MVC template.
    • ASP.NET Identity for authentication and identity management.
    • Authentication Filters for authenticating user by custom or third-party authentication provider.
    • With the help of Filter overrides, we can now override filters on a method or controller.
    • Attribute Routing is now integrated into MVC 5

    MVC 4 – August 2012

    • ASP.NET Web API, a framework that simplifies the creation of HTTP services and serving a wide range of clients.
    • Follow to create your first ASP.NET Web API service
    • Adaptive rendering and other look-n-feel improvements to Default Project Templates.
    • A truly Empty Project Template.
    • MVC4 also uses Razor View Engine as a default view engine with some new features like condition attribute and ‘Tilde slash’
    • Based on jQuery Mobile, new Mobile Project Template introduced.
    • Support for adding controller to other project folders also.
    • Task Support for Asynchronous Controllers.
    • Controlling Bundling and Minification through web.config.
    • Support for OAuth and OpenID logins using DotNetOpenAuth library.
    • Support for Windows Azure SDK 1.6 and new releases.
    • MVC4 provides better support for Jquery like Jquery Mobile
    • Client side validation, Jquery validation and enhanced support for asynchronous methods
    • Supports many new features for mobile apps and also provides new mobile project template and default templates are refreshed and modernized

    MVC 3 – January 2011

    • New Project Templates having support for HTML 5 and CSS 3.
    • Improved Model validation.
    • Razor View Engine (.cshtml for c# and .vbhtml for Visual Basic) introduced apart from Web Forms view engine (.aspx)
    • Having support for Multiple View Engines i.e. Web Forms view engine, Razor or open source.
    • Controller improvements like ViewBag property and ActionResults Types etc.
    • Unobtrusive JavaScript approach, Ajax and Client side Validation, Jquery Validation and JSON binding support
    • Chart, WebGrid, Crypto,WebImage, WebMail Controls
    • Improved Dependency Injection with new IDependencyResolver.It provides powerful hooks with Dependency Injection and Global Action Filters
    • Partial page output caching.
    • TempData, ViewData ,ViewBag
    • Supports not only Master Page but also Layout Page

    MVC 2 – March 2010

    • MVC 2 uses only Web Forms view engine (.aspx) as a default View Engine.
    • (HTML Syntax) Web Forms view engine syntax: <%=Html code %>
    • TempData, ViewData
    • Jquery support is Good
    • Supports only Master Page
    • Client-side Validation and Asynchronous controllers
    • support controllers to process requests asynchronously
    • Supports validations using the RangeAttribute, RequiredAttribute, StringLengthAttribute, and RegexAttribute attributes.

    MVC 1 – March 2009

    MVC CTP – December 2007 

    * Notes

    • View Engine is responsible for rendering of the HTML code from your views to the browser.
    • In ViewData, dictionary of objects are accessible via strings as keys
    • ViewBag was added in the C# 4.0 which uses the dynamic feature that allows to add properties of an object dynamically . We can say that ViewBag = ViewData + dynamic feature around the ViewData dictionary
  • Prism Navigation – ViewModel first

    Navigating to view using prism navigation service is straight forward and there are lot of examples on web around this.With MVVM i always found ViewModel first approach as best because of lot of reasons, some of them are

    • With MVVM, ViewModels is your application
    • No threading issues as you will always create view models which can be created on any thread.
    • More responsive , you are not dealing with dispatcher context to create views and let WPF system decide when to create views.
    • No Memory leaks, The life cycle of views is controlled by WPF system in most efficient way
    • More testable application, Since you app lives in view models and they don’t have UI beasts,
    • And lot more..

    This post cover a small trick to implement the ViewModel first navigation using Prism 5.0 for WPF.

    1. What ever IoC you are using (the code is based on StructuralMap) expose the RegisterForNavigation<T> method and from module initialization, register interface of viewModels participating in navigation process. The RegisterForNavigation<T> will look like

    public static void RegisterTypeForNavigation<T>(this ConfigurationExpression reg)
    {
    reg.For<object>().Use(() => ServiceLocator.Current.GetInstance(typeof(T)))
    .Named(typeof(T).FullName);
    }
    

    And some where in module initialization you call this method

    configurationExpression
    .RegisterTypeForNavigation<ICustomerDetailViewModel>();
    

    And to navigate to this view model just use

    var parameters = new NavigationParameters { { "Activity", SelectedActivity }, { "ColumnName", columnName } };
    var uri = new Uri(typeof(ICustomerDetailViewModel).FullName, UriKind.RelativeOrAbsolute);
    regionManager.RequestNavigate(ShellRegions.Workspace, uri, parameters);
    

    And obviously some where in you data template definition there should a (DataTemplate) mapping which WPF uses to swap the view model with corresponding view (and set data context too)..