Xamarin Forms and CRUDMethod

Abstract

In this tutorial we'll learn about importance and utility of CRUDMethod for invoking CRUDController service methods along side CRUDL methods.

Keywords: WindnTrees, CRUDView, Xamarin Forms, CRUD2CRUD, CRUDS
  • Introduction

    CRUDMethod enables additional API calls at CRUD service controller with target methods without writing additional lines of code. Both CRUDClient and CRUDService communicate data and establish interface neutral communication. CRUDMethod interface is composed of following methods:

    1. Method(string target)
    2. MethodObject(object contentObject, string target)

    CRUDView = CRUDController

    CRUDView requests data against CRUDController service actions in following well defined method calls:

    1. CRUDView.Create
    2. CRUDView.Read
    3. CRUDView.Update
    4. CRUDView.Delete
    5. CRUDView.List
    6. CRUDView.Method
    7. CRUDView.MethodObject

    CRUDController serve client requests using following service actions:

    1. CRUDController.Create
    2. CRUDController.Read
    3. CRUDController.Update
    4. CRUDController.Delete
    5. CRUDController.List
    6. CRUDController.MethodAction1
    7. CRUDController.MethodAction2
    8. CRUDController.MethodAction3

    ASP .NET CRUD controllers are left open for CRUDMethod implementations letting user customize controller actions with attributes and action behaviors. Comparatively, WCF ServiceControllers (ServiceRepositories) support CRUDMethod on server side and there is no need to define MethodActions in ServiceControllers.

    CRUDS Tutorials - WindnTrees Windows (invincibletec.com)

  • CRUDMethod Requests and Actions

    ServiceClient make CRUDMethod requests in following manner, for example

    1. CRUDView.Method("ListAllCategories")

    Invokes ListAllCategories service action method and gets list of all categories from remote service where as

    1. CRUDView.MethodObject(newCategory, "CreateCategory")

    Invokes CreateCategory service action method with newCategory object argument and return newly created object. Additional method invocations can be achieved in same manner without writing custom code.

    Xamarin Form Implementation with CRUDMethod

    Xamarin form views interact with WindnTrees ObserverObject view model via BindingContext that allow user interactions with ViewModel commands and properties.

    For example following view invokes "ListCommand" command of CRUDView that invokes "List" service action of server side CRUDController.

    <RefreshView IsRefreshing="{Binding ListProcessing, Mode=TwoWay}" Command="{Binding ListCommand}">
            <CollectionView x:Name="ItemsCollectionView"
                    ItemsSource="{Binding ContentsListModel}">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        ...
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </RefreshView>
    </ContentPage>
    

    By default CRUDView ViewModel commands include:

    1. CreateCommand
    2. ReadCommand
    3. UpdateCommand
    4. DeleteCommand
    5. ListCommand

    CreateCommand invokes "create", ReadCommand invokes "read", UpdateCommand invokes "update", DeleteCommand invokes "delete" and ListCommand invokes "list" on server side CRUDController. Additional server side actions can be invoked from view ViewModel using target method passed through command parameter. For example following will load collection view with top 5 categories instead of default "list" server side action response.

    <RefreshView IsRefreshing="{Binding ListProcessing, Mode=TwoWay}" Command="{Binding ListCommand}" CommandParameter="ListCategories">
            <CollectionView x:Name="ItemsCollectionView"
                    ItemsSource="{Binding ContentsListModel}">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        ....
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </RefreshView>
    </ContentPage>
    

    CRUDView ViewModel commands with CommandParameter argument allows CRUDView with alternative server side actions Or programmer may invoke command with view argument and then make CRUDView.Method or CRUDView.MethodObject function calls.

  • CRUDController Implementation of CRUDMethod

    CRUDController implementation of CRUDMethod are simple ASP .NET controller actions that are invoked by using following interface methods:

    1. Method(target)
    2. MethodObject(object, target)

    In current application example following methods are invoked by CRUDView from client side.

    1. CreateCategory
    2. ListCategories
    3. ListAllCategories

    and their example invocations include:

    1. Method("ListAllCategories")
    2. MethodObject(SearchInput searchInput, "ListCategories")
    3. MethodObject(Category contentObject, "CreateCategory")

    CRUDMethod and ResponseTypes

    CRUDMethod can result in either object or list response and to support appropriate generalized type casting programmer must initialize response type list with expected output object and method name.

    CRUDView.ResponseTypes.Add(new ResponseType { TargetMethod = "ListCategories", IsListResponse = true });
    

    Example

    Tutorial example is extended from WindnTrees CRUDView, Xamarin Forms and WindnTrees.Core Web API and elaborates CRUDMethod implementation.

    Summary

    CRUDMethod generalize remote method invocations with simple interfacing technique. Download project code here. Make sure your computer and mobile IP settings are correct while running example code.