Incoding Framework - Model View Dispatcher



Figure 1: 

Beginning

IML: contributed greatly to a simpler way of creating ajax application due to the ability to divide complex.
Veiws: into simple ones remarkably reducing the code and to increasing noteworthily Action in Controller.
MVD: ( model view dispatcher ) is a design pattern to execute a Command/Query without writing an Action For example:

  1. private void btn_load_Click(object sender, EventArgs e)  
  2. public ActionResult Details(GetUserDetailsQuery query)   
  3. {  
  4.     var model = dispatcher.Query(query);  
  5.     return IncView(model);  
  6. }  
The code listing presents a common case when the Action returns View, Query ( GetUserDetailsQuery ) data driven. A Query hides properly most of the logic as it obtains all appropriate database and Event Broker instruments, as well as all the re-usable objects. Refer to the example of the Action calling of View
  1. @Html.When(JqueryBind.InitIncoding)  
  2.       .Do()  
  3.       .AjaxGet(Url.Action("Details","Users"))  
  4.       .OnSuccess(dsl = > dsl.Self().Core().Insert.Html())  
  5.       .AsHtmlAttributes()  
  6.       .ToDiv()  
Over time it requires to produce a Query and pass its View traversal of the Controller, as it is a simple intermediate link to be covered by tests and supported subsequently. The problem is not with creating manyAction, but with their redundancy revealed after having studied the majority of scripts solved within CQRS architecture. The Controller produces a Dispatcher obtaining no advanced logic, with a few exceptions it is a typical mundane code.

Write less, do more

Consider next a new View code

  1. Html.When(JqueryBind.InitIncoding)  
  2.       .Do()  
  3.       .AjaxGet(Url.Dispatcher()  
  4.                   .Query(new GetUserDetailsQuery())  
  5.                   .AsView("~/Admin/Views/Users/Details.cshtml"))  
  6.       .OnSuccess(dsl = > dsl.Self().Core().Insert.Html())  
  7.       .AsHtmlAttributes()  
  8.       .ToDiv()  
Url constructing is changed in the entry, now it is not a common Url.Action, but a specific builder to create a Query-based address and paths to View
  1. Url.Dispatcher()  
  2.    .Query(new GetUserDetailsQuery())  
  3.    .AsView("~/Admin/Views/Users/Details.cshtml")  

The present code will work without writing an Action significantly saving time on development and maintenance.

No More MVC

To answer the above question let us see how to integrate MVD into the project
  • Create a DispatcherController (the name is of importance) inherited from the DispatcherControllerBase(set as default by version 1.1).
  1. public class DispatcherController : DispatcherControllerBase  
  2. {          
  3.     public DispatcherController()  
  4.    : base(typeof(T).Assembly)  
  5.     {    }      
  6. }  
Note: the Assembly comprising your Command/Query as well as other classes implemented in the View can be passed as a parameter to the constructor. DispatcherControllerBase includes the following Action.
  1. Query(string incType, string incGeneric, bool? incValidate)  
  2. Render(string incView, string incType, string incGeneric)  
  3. Push(string incType, string incGeneric)  
  4. Composite(string incTypes)  
  5. QueryToFile(string incType, string incGeneric, string incContentType, string incFileDownloadName)  
One can construct url to perform a Push command.
Url.Action("Push", "Dispatcher", new { incType = typeof(Command).Name } )
One can use the Url.Dispatcher to simplify building of url address
Url.Dispatcher().Push(new Command())

Application

MVD covers most of the frequent scenario in web-development powered by asp.net mvc. Note: Download. the examples

  • Push for a single command
  1. Html.When(JqueryBind.Click)  
  2.     .Do()  
  3.     .AjaxPost(Url.Dispatcher().Push(new AddUserCommand  
  4.                                         {  
  5.                                             Id = "59140B31-8BB2-49BA-AE52-368680D5418A",  
  6.                                             Name = "Vlad"  
  7.                                          }))  
  8.     .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())  
  9.     .AsHtmlAttributes()  
  10.     .ToButton("Run")  

Note: Selector can be used as values for parameters

  1. Html.When(JqueryBind.Click)  
  2.                   .Do()  
  3.                   .AjaxPost(Url.Dispatcher().Push(new AddEntityCommand<T>()))  
  4.                   .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())  
  5.                   .AsHtmlAttributes()  
  6.                   .ToButton("Run")  

Note: push returns the result and there is an array comprised the results of each command for the composite

  • Push for a command composite
  1. Html.When(JqueryBind.Click)  
  2.                   .Do()  
  3.                   .AjaxPost(Url.Dispatcher()  
  4.                                .Push(new AddUserCommand { Id = "1", Name = "Name" })  
  5.                                .Push(new ApproveUserCommand { UserId = "2" }))  
  6.                   .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())  
  7.                   .AsHtmlAttributes()  
  8.                   .ToButton("Run")  

Note: be aware that in case parameter-names match, the value will be the same and from the last, and an extra prefix may be attached to the name in these situations.

  • Query as Json
  1. Html.When(JqueryBind.Click)  
  2.                   .Do()  
  3.                   .AjaxPost(Url.Dispatcher()  
  4.                                .Query(new GetCurrentDtQuery())  
  5.                                .AsJson())  
  6.                   .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())  
  7.                   .AsHtmlAttributes()  
  8.                   .ToButton("Run")  
  1. Html.When(JqueryBind.Click)  
  2.                   .Do()  
  3.                   .AjaxPost(Url.Dispatcher()  
  4.                                .Query(new GetTypeNameQuery<T>())  
  5.                                .AsJson())  
  6.                   .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())  
  7.                   .AsHtmlAttributes()  
  8.                   .ToButton("Run")  
  • Query as View
  1. Html.When(JqueryBind.Click)  
  2.                   .Do()  
  3.                   .AjaxPost(Url.Dispatcher()  
  4.                                .Query(new GetUserQuery())  
  5.                                .AsView("~/Views/Home/User.cshtml"))  
  6.                   .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())  
  7.                   .AsHtmlAttributes()  
  8.                   .ToButton("Run")  

Note: View path is not built under the Controller in asp.net mvc, but the site root directory, to create any folder structure for View storage.

  • Query as File
<a href="@Url.Dispatcher().Query(new GetFileQuery()).AsFile(incFileDownloadName: "framework")">Download</a>

Note: building a File requires the Query to return byte ( byte[] ) array as a result

  • Model as View
  1. Html.When(JqueryBind.Click)  
  2.     .Do()  
  3.     .AjaxPost(Url.Dispatcher()  
  4.                  .Model(new GetUserQuery.Response  
  5.                              {  
  6.                                Id = "2",  
  7.                                Name = "Incoding Framework"  
  8.                               })  
  9.                  .AsView("~/Views/Home/User.cshtml"))  
  10.     .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())  
  11.     .AsHtmlAttributes()  
  12.     .ToButton("Run")  

Note: if the query is not completed via Ajax, it will result in ContentResult rather than JSON

  • View
  1. Html.When(JqueryBind.Click)  
  2.                   .Do()                  
  3.                   .AjaxPost(Url.Dispatcher().AsView("~/Views/Home/Template.cshtml"))  
  4.                   .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())  
  5.                   .AsHtmlAttributes()  
  6.                   .ToButton("Run")  
Note: the present way is well-suited to obtain the template via Ajax
  1. Selector urlTemplate = Url.Dispatcher().AsView("~/Views/Medication/Medication_Table_Row_Tmpl.cshtml").ToAjaxGet();  
  2. dsl.Self().Core().Insert.WithTemplate(urlTemplate).Append();  
There is a very wide range of application for the MVD, the sequel will obtain an Async method support, thus, it can be certainly argued that there are enough capabilities to develop the project without any unified additional Controller.

What has to be done if

Notice that lack of the Controller does not allow using attributes implemented in different scripts. Let us consider the frequent tasks virtually applied for any site, namely, a test for authorization. Within ASP.NET MVC the task is solved using attributes to mark those Actions (or Controller) where validation logic is described, the present task can be solved in different ways in MVD:

  • Mark with a DispatcherController attribute and in an attribute code. This method is convenient if writing CRM system and performing all the actions with authorization.

           Note: the current requested address can be checked in the Allow List in the attribute code i.e. executing the code if the address is not included
           in the List.

  • Use Dispatcher Event capabilities described in the article.

           Note: the Command/Query can be marked with attributes that is similar to the standard ASP.NET MVC.

  • Implement the test for a client.
    1. Html.When(JqueryBind.InitIncoding)    
    2.       .Do()    
    3.       .AjaxGet(Url.Dispatcher()    
    4.                   .Query(new IsAuthorizeDeviceQuery    
    5.                              {    
    6.                                      DeviceId = Selector.Incoding.Cookie(OnAddCookieEvent.DeviceIdKey)    
    7.                              })    
    8.                   .AsJson())    
    9.       .OnSuccess(dsl = >    
    10.                      {    
    11.                          dsl.Self().Core().Break    
    12.                             .If(builder = > builder.Data(r = > r.Value == false));    
    13.                          //some code if authorize ok    
    14.                      })    
    15.       .OnBreak(dsl = > dsl.Self().Core().Trigger.Invoke("SignIn"))    
    16.       .When("SignIn")    
    17.       .Do()    
    18.       .AjaxGet(Url.Dispatcher().AsView("~/Areas/Client/Views/Account/SingIn.cshtml"))    
    19.       .OnSuccess(dsl = > dsl.With(r = > r.Id(dialogId))    
    20.                            .Behaviors(inDsl = >    
    21.                                     {    
    22.                                         inDsl.Core().Insert.Html();    
    23.                                         inDsl.JqueryUI().Dialog.Open(options = >    
    24.                                                                      {    
    25.                                                                        options.Title = "Sign in device";    
    26.                                                                      });    
    27.                                      }))    
    28.       .AsHtmlAttributes()    
    29.       .ToDiv()    

The stepwise algorithm:

  1. After loading the element ( InitIncoding ), issue an Ajax query ( ine 3).
  2. Check the result via OnSuccess, if the user is not authorized choose Break.
  3. If it is OnBreak, choose trigger for SignIn “leg” and give the dialogue with an authorization form.
  4. If the code passes OnBreak, perform the actions to run page scripts.

The code deals with most scripts and does not limit the spectrum (roles, rights, etc ) for tests, there is also а possibility to perform various tests depending on a page, i.e. constructing Home/Index and Dashboard/Index with different codes.

Up Next
    Ebook Download
    View all
    Learn
    View all