Sample Model View Dispatcher
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.nuget
.vs/config
MVD.Domain
MVD.UI
packages
.gitignore
LICENSE
MVD.sln
README.md

README.md

Article-5-small

 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:

public ActionResult Details(GetUserDetailsQuery query)
{
    var model = dispatcher.Query(query);
    return IncView(model);
}

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

Html.When(JqueryBind.InitIncoding)
      .Do()
      .AjaxGet(Url.Action("Details","Users"))
      .OnSuccess(dsl = > dsl.Self().Core().Insert.Html())
      .AsHtmlAttributes()
      .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 many Action, 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
Html.When(JqueryBind.InitIncoding)
      .Do()
      .AjaxGet(Url.Dispatcher()
                  .Query(new GetUserDetailsQuery())
                  .AsView("~/Admin/Views/Users/Details.cshtml"))
      .OnSuccess(dsl = > dsl.Self().Core().Insert.Html())
      .AsHtmlAttributes()
      .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
Url.Dispatcher()
   .Query(new GetUserDetailsQuery())
   .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)
public class DispatcherController : DispatcherControllerBase
{        
    public DispatcherController()
   : base(typeof(T).Assembly)
    {    }    
}
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:

Query(string incType, string incGeneric, bool? incValidate)
Render(string incView, string incType, string incGeneric)
Push(string incType, string incGeneric)
Composite(string incTypes)
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
Html.When(JqueryBind.Click)
                  .Do()
                  .AjaxPost(Url.Dispatcher().Push(new AddUserCommand
                                                      {
                                                              Id = "59140B31-8BB2-49BA-AE52-368680D5418A",
                                                              Name = "Vlad"
                                                      }))
                  .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())
                  .AsHtmlAttributes()
                  .ToButton("Run")
 Note: Selector can be used as values for parameters
Html.When(JqueryBind.Click)
                  .Do()
                  .AjaxPost(Url.Dispatcher().Push(new AddEntityCommand<T>()))
                  .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())
                  .AsHtmlAttributes()
                  .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
Html.When(JqueryBind.Click)
                  .Do()
                  .AjaxPost(Url.Dispatcher()
                               .Push(new AddUserCommand { Id = "1", Name = "Name" })
                               .Push(new ApproveUserCommand { UserId = "2" }))
                  .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())
                  .AsHtmlAttributes()
                  .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
Html.When(JqueryBind.Click)
                  .Do()
                  .AjaxPost(Url.Dispatcher()
                               .Query(new GetCurrentDtQuery())
                               .AsJson())
                  .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())
                  .AsHtmlAttributes()
                  .ToButton("Run")
Html.When(JqueryBind.Click)
                  .Do()
                  .AjaxPost(Url.Dispatcher()
                               .Query(new GetTypeNameQuery<T>())
                               .AsJson())
                  .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())
                  .AsHtmlAttributes()
                  .ToButton("Run")
  •  Query as View
Html.When(JqueryBind.Click)
                  .Do()
                  .AjaxPost(Url.Dispatcher()
                               .Query(new GetUserQuery())
                               .AsView("~/Views/Home/User.cshtml"))
                  .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())
                  .AsHtmlAttributes()
                  .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
Html.When(JqueryBind.Click)
    .Do()
    .AjaxPost(Url.Dispatcher()
                 .Model(new GetUserQuery.Response
                             {
                               Id = "2",
                               Name = "Incoding Framework"
                              })
                 .AsView("~/Views/Home/User.cshtml"))
    .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())
    .AsHtmlAttributes()
    .ToButton("Run")
Note: if the query is not completed via Ajax, it will result in ContentResult  rather than JSON
  • View
Html.When(JqueryBind.Click)
                  .Do()
                  .AjaxPost(Url.Dispatcher().AsView("~/Views/Home/Template.cshtml"))
                  .OnSuccess(dsl = > dsl.With(r = > r.Id(containerId)).Core().Insert.Html())
                  .AsHtmlAttributes()
                  .ToButton("Run")
Note: the present way is well-suited to obtain the template via Ajax
Selector urlTemplate = Url.Dispatcher().AsView("~/Views/Medication/Medication_Table_Row_Tmpl.cshtml").ToAjaxGet();
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 most frequent task 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 very 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 into the List.
  • Use Dispatcher Event capabilities described in the article
Note: the Command/Query can be marked with attributes, which is similar to the standard asp.net mvc work
  • Implement the test for a client
Html.When(JqueryBind.InitIncoding)
      .Do()
      .AjaxGet(Url.Dispatcher()
                  .Query(new IsAuthorizeDeviceQuery
                             {
                                     DeviceId = Selector.Incoding.Cookie(OnAddCookieEvent.DeviceIdKey)
                             })
                  .AsJson())
      .OnSuccess(dsl = >
                     {
                         dsl.Self().Core().Break
                            .If(builder = > builder.Data(r = > r.Value == false));
                         //some code if authorize ok
                     })
      .OnBreak(dsl = > dsl.Self().Core().Trigger.Invoke("SignIn"))
      .When("SignIn")
      .Do()
      .AjaxGet(Url.Dispatcher().AsView("~/Areas/Client/Views/Account/SingIn.cshtml"))
      .OnSuccess(dsl = > dsl.With(r = > r.Id(dialogId))
                           .Behaviors(inDsl = >
                                          {
                                              inDsl.Core().Insert.Html();
                                              inDsl.JqueryUI().Dialog.Open(options = >
                                                                               {
                                                                                   options.Title = "Sign in device";
                                                                               });
                                          }))
      .AsHtmlAttributes()
      .ToDiv()
The stepwise algorithm:
  1. After loading the element ( InitIncoding ) issue an Ajax query ( line 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.