Saturday, November 17, 2018

A1 Repo: Simple Pagination for WCF Service Operation

It is never a good idea to paginate on the client.  This post is about a simple pagination for a WCF service operation.  For this work I need a RequestModel and a ResponseModel.



The RequestModel contains two properties; PageNumber and PageSize.  The RequestModerl could contain sort order by certain fields but because this is simple pagination no sorting by fields is supported in this note.

The ResonseModel has a list of items of reference type T and other properties which could be used by the client.

The service in question has tree operations:



We are particularly interested in the implementation of  ResponseModel GetEmployeesByRequest(RequestModel request) and here it is:



The interesting part is the OrderBy > Skip > Take.  Note that in case that the client asks for a page which does not exist the actual page is reset to the last page so we know the records to skip. Note that what is sent to the client has been mapped to a specific DataContract and it was done using a mapping extension very similar to how it was done in A1 Repo: Explicit Mapping Using Extensions Methods Rather than Using AutoMapper.

This little POC is tested using a console application.  Note that when a service reference is created the  client proxy offers the possibility of using asynchronous programming.



For this note the Adventure2012 database was used.  The ORM employed was Entity Framework and the model was created using the Entity Data Model Wizard > Code First from Database.

To Table of contents


Monday, November 5, 2018

A1 Repo: Explicit Mapping Using Extensions Methods Rather than Using AutoMapper

There are many reasons not to use AutoMapper in projects. I started to use AutoMapper about five years ago and the first problem that I encountered from the start gate was that using Automapper creates mapping code which is very difficult, and at times impossible to debug. Another equally distressing observation is that the code can become very complex very quickly due to formatting and conditional mappings which need to be configured in the mapper configuration.

The only one thing that I liked about AutoMapper is that I can tack away the mapping code making service operations, controller actions and methods in general more readable. Hence, in the spirit of putting away the mapping code I prefer to do explicit mapping using extension methods.

The following snippet of code maps AssetDto to AssetViewModel.



Mapping the ViewModel to a Dto would be done just in the same fashion through extension methods.


To Table of contents

Sunday, November 4, 2018

A1 Repro (ASP.Net CORE): Loading Lookup Collection in Action Filter and Simple Dropdownlist Editor

There are times when a collection of lookup to populate a drop down list is needed; this is very common. The usual solution is to create an Enumerable property in the view model to hold the lookup values, inject a service in the controller then in the action load the list to the view model and load the view. A complete example can be accessed at Pluralsight where dropdown lists are polulated in Razor views using the MVVM design pattern, Entity Framework, and Ajax.

Another approach is to load the lookup collection to the ViewData and access the values from the view or a template editor. This note is about a modification of this approach where the lookup collection is loaded into the ViewData using a Fiter Service which decorates the action requiring the loopkup collection.

The Filter
The following filter loads a collection of AssetTypes and converts the collection of DTOs into a SelectListItems using an extension method and loads the list on the ViewData.



The way to consume this filter service attribute is through the ServiceFilter, decorating the action with [ServiceFilter(typeof(AssetTypeCollectionAttribute))] this way:



Inject Service
Like any other service, the Filter Service needs to be injected in order to be discovered, therefore for an ASP.Net CORE application AssetTypeCollectionAttribute needs to  be injected.  In this example is added for the scope of the request:



Simple Drop Down List Editor
The editor template is simple, and can be reused for other lookup lists.



ViewModel and View
To end the note; the editor is invoked from the view passing two parameters in the ViewData; the index or key used to store the collection in the ViewData and the initial default message to load in the drop down list.



None of the above would work if the ViewModel does not have property AssetType decorated with [UIHint("ViewDropdown")]





To Table of contents

Friday, November 2, 2018

A1 Repo: Snippets to Save the Day!

I find myself reinventing the same wheel over and over again and wish I had my repository of my own creation handy. Well, for my own enjoyment I will write a series of articles containing snippets of code which can be reused anywhere. This is the motivation of the "A1 Repo" series and instead of reinventing myself, perhaps this articles will help me making my own creation better. I am not documenting the "other" way of doing things but the final chunk of code which is good for reusing.

Snippets Table of Content:

  1. ASP.Net MVC Editor Template Date using jQuery Date picker supporting Bootstrap 4
  2. Loading Lookup Collection in Action Filter and Simple Dropdownlist Editor (ASP.Net CORE)
  3. Explicit Mapping Using Extensions Methods Rather Using AutoMapper
  4. Simple Pagination for WCF Service Operation


A1 Repro: An Easy Date Editor Template suitable for jQuery Date Picker Plugin


This entry is about creating an editor template for an ASP.Net MVC solution for a Date.

The following piece of code shows a section of "PropertyViewModel" class containing three different dates; "TotalValueAssessmentDate", "PreviousYearValueAssessmentDate", and "LastInspected".




Only these three dates would generate a busy markup, hence to avoid that use an editor for the DateTime data type, which is hinted by the UIHint annotation on the property.




The view now is very easy to read as we see the editor usages right away. The template is also very simple.




The above editor is meant to be used with bootstrap-datepicker jQuery plugin. You can find the plugin documentation in the provided link reference.

The javascript spinet which loads the calendar popup looks like this:





To Table of contents

A1 Repo: Simple Pagination for WCF Service Operation

It is never a good idea to paginate on the client.  This post is about a simple pagination for a WCF service operation.  For this work I nee...