Get started

IncFramework-logo

disclamer:this article is a step-by-step guide to help you to familiarize with the core functionality of Incoding Framework. Following the guide will result in an application that implements the work with the DB (CRUD + data filters) and fully covered with unit tests.

Part 0. Introduction

Let us begin with a short description of Framework. Incoding Framework comprises three packages: Incoding framework – back-end project, Incoding Meta Language – front-end project and Incoding tests helpers – unit-tests for back-end. These packages are installed independently of each other, making it possible to integrate framework by parts into the project: You can connect only front or back end (tests are tightly coupled with the back end, so, they could be more considered as a complement).

Projects developed in Incoding Framework, use CQRS as a server architecture. Incoding Meta Language. В целом Incoding Framework  is used as a basic tool for building front-end. All in all, Incoding Framework covers the entire application development cycle.

Typical solution, that was developed using Incoding Framework, comprises 3 projects:

  1. 1. Domain (class library) is responsible for business logic and database operations.
  2. UI (ASP.NET MVC project) – front-end based on ASP.NET MVC.
  3. UnitTests (class library– unit-tests for Domain.

Domain

After installation of  Incoding framework through Nuget , along with the necessary dll, Bootstrapper.cs file will be added in the project. The file is mainly responsible for the initialization of an application: logging initialization, IoC registration, installation of Ajax-requests settings, etc. By default, StructureMapis installed as loC framework, but there is a provider for Ninject, and it is also possible to write your own implementations.

Further on, commands (Command) and queries (Query) are added to Domain, that perform database operations or any action, related with business application logic.

UI

During the installation of Package Incoding Meta Language , it adds the necessary dll to the package, as well as IncodingStart.cs and DispatcherController.cs (part MVD) files required to work Domain.

After the installation, the client logic is added to UI using IML.

UnitTests

During the installation of Incoding tests helpers, the project is added by the MSpecAssemblyContext.csis file, in which connection is customize to the test dtabse.

Part 1. Installation.

So, we proceed to the task of the disclamer  and start writing our application. The first phase of building the application is to create solution structure of a project and to add the projects to it. The project solution will be called Example and, as was already mentioned in the introduction, will have 3 projects. We begin with the project that is responsible for business logic of the application – Domain.

Create class library Domain.

Domain

Then we proceed to the front-end – create and install ASP.NET Web Application UI with links to the MVC packages as template, empty project.

UI1

UI2

Finally, we add class library UnitTests that is responsible for unit testing.

UnitTests

Note: Although UnitTests are not an obligatory part of the application, we recommend you to cover the code with tests as it will help to avoid numerous problems in future with various possible faults in the code due to test automation.

After having finished all the above activities, you will get following solution:

Solution

After we create the solution structure, we need to install Incoding Framework package from Nuget.

The installation carried out by Nuget. There is the same algorithm of installation for all the projects:

  1. Right-click the project and select Manage Nuget Packages… in the context menu
  2. Search incoding
  3. Select necessary package and install it

First install Incoding framework in Domain.

Incoding_framework_1

Then add to the file Domain -> Infrastructure -> Bootstrapper.cs the link to StructureMap.Graph.

StructureMap_ref

2 packages must be installed to UI:

  1. Incoding Meta Language
  2. Incoding Meta Language Contrib

Incoding_Meta_Languge

MetaLanguageContrib_install

Note: make sure that the Copy Local property is set to true in the References -> System.Web.Mvc.dll

Now change the file  Example.UI -> Views -> Shared -> _Layout.cshtml so that it looks as follows:

Then add the link to Bootstrapper.cs to the files Example.UI -> App_Start -> IncodingStart.cs and Example.UI -> Controllers -> DispatcherController.cs.

IncodingStart_bootstrapper

DispatcherController_bootstrapper

Note: If you use MVC5, it’s necessary for framework to add following code to Web.config file.

Now install  Incoding tests helpers inUnitTests and add the link to Bootstrapper.cs in Example.UnitTests -> MSpecAssemblyContext.cs.

Incoding_tests_helpers

MSpecAssemblyContext_bootstrapper

The last phase of the preparation the projects to work is to create folders structure for the projects.

Add following folders to the Example.Domain project:

  1. Operations – command and query of the project
  2. Persistences – entities for DB mapping
  3. Specifications – where and order specifications for data cleaning when request is made

Example.Domain_folders

In the Example.UnitTests project create just the same folders structure as in Example.Domain.

UnitTests_folders

Part 2. Setting up a DB connection.

To begin this process, create DB with which you will work. Open SQL Managment Studio and create two DB: Example and Example_test.

add_DB

example_db

example_test_db

In order to work with DB, you need to set up a connection. Add to the file Example.UI -> Web.config and Example.UnitTests -> app.config connection string to the BD:

In the file Example.Domain -> Infrastructure -> Bootstrapper.cs, register the appropriate connection string using a key called Example:

In the file Example.UnitTests -> MSpecAssemblyContext.cs, register the connection string to the BD using the key called Example_test:

Note: Example and Example_test databases must exist.

Part 3. CRUD.

After the actions described above, we come to the most interesting part – code writing implementing the CRUD (create, read, update, delete) functionality of an application. To begin this process, create an entity class that will map to the DB. In our case, this is Human.cs that we add to the Example.Domain -> Persistences folder.

Human.cs

Our class contains several fields where we will write data and Nested Class Map.

Note: after creating the Human class, you do not need to perform any operations (creating an XML mapping) due to  FluentNhibernate.

We can now add commands and queries, which are responsible for realization of the CRUD operations. The first command will be responsible for adding a new or change an existing record of the Human type.  The command is quite simple: we either get an entity on a Repository using the key (ld) or, if no entity exist, we create a new one. Both of these entities get the values specified   in the properties of the AddOrEditHumanCommand class. Add Example.Domain -> Operations -> AddOrEditHumanCommand.cs to the project.

AddOrEditHumanCommand.cs

The Read command is the second part of the CRUD. This is a request for reading entities from the DB. Add the file Example.Domain -> Operations -> GetPeopleQuery.cs.

GetPeopleQuery.cs

The Delete command is the remaining part of the CRUD. The command deletes records from the DB using the key (ld). Add the file Example.Domain -> Operations -> DeleteHumanCommand.cs.

DeleteHumanCommand.cs

In order to populate the DB with initial data, add the file Example.Domain -> InitPeople.cs that is derived from the ISetUP interface.

ISetup

All the class instances from the ISetUp are registered with IoC in the Bootstrapper.cs (see Introduction) and run (public void Execute() ) in order (public int GetOrder() ).

InitPeople.cs

The back-end implementation of the CRUD is ready. Now it is time to add a user code. As in the case of the back end, we begin the implementation with creating/editing a record. Add the file Example.UI -> Views -> Home -> AddOrEditHuman.cshtml. 

AddOrEditHuman.cshtml

The IML-code creates the standard HTML form and works with AddOrEditHumanCommand, sending the appropriate Ajax query to the server.

Then comes the template for data loading through the GetPeopleQuery. There is a description of the table that will be responsible not only for data output, but also for record deletion and editing: add the file Example.UI -> Views -> Home -> HumanTmpl.cshtml.

HumanTmpl.cshtml

Note: The task of opening a dialog box is quite common, so the code that is responsible for this task can be exported to the extension. 

Thus, it remains to change the start page so that during its loading AJAX query is transmitted to the server for obtaining data from the GetPeopleQuery and mapping of data using HumanTmpl: change the file Example.UI -> Views -> Home -> Index.cshtml so that it looks as follows.

Index.cshtml

In real-world applications, validation of input form data is one of the most frequent task. Therefore, we add data validation on the adding/editing form of the Human entity. First, we need to add a server code. Add the following code in AddOrEditHumanCommand as a nested class:

On the AddOrEditHuman.cshtml form, we used constructs like this:

It is therefore not necessary to add

for the fields – ForGroup() will do it.

So we have written the application code that implements the CRUD functionality for one DB entity.

Part 4. Specifications – data cleaning.

Another task that often occurs in real projects is to clean requested data.  Incoding Framework uses WhereSpecifications for convenient code writing and complying an encapsulation principle for cleaning data from Query. In the written code add a possibility to clean data from GetPeopleQuery by FirstName and LastName. First, add two specification files Example.Domain -> Specifications -> HumanByFirstNameWhereSpec.cs and Example.UI -> Specifications -> HumanByLastNameWhereSpec.cs

HumanByFirstNameWhereSpec.cs

HumanByLastNameWhereSpec.cs

Now use the written specifications in GetPeopleQuery. .Or()/.And() relations allow to merge atomic specifications that helps to use the created specifications many times and fine-tune necessary data filters (in the example we use .Or() relation)

GetPeopleQuery.cs

Finally, it only remains to modify Index.cshtml in order to add a search box, which uses a Keyword field for data cleaning while a request is being processed.

Index.cshtml

Part 5. Unit-test.

Let’s cover the written code with tests. The first one is responsible for testing of Human entity mapping. Add the file When_save_Human.cs to the folder Persisteces of the UnitTests project.

When_save_Human.cs

The test works with a test database (Example_test): an instance of the Human class with automatically populated fields is created, then stored in the DB, retrieved from and compared to the created instance.

Then add the tests for WhereSpecifications in a folder named Specifications.

When_human_by_first_name.cs

When_human_by_last_name.cs

Now we have to add tests for the command and the query (Operations folder). For the command, you need to add two tests: the first one verifies the creation of a new entity; the second one verifies the editing of an existing entity.

When_get_people_query.cs

When_add_human.cs

When_edit_human.cs

Study materials

  1. CQRS  and CQRS (advanced course) , Repository – back end architecture
  2. MVD -a description of a Model View Dispatcher pattern
  3. IML (TODO), IML vs Angular , Iml vs Jquery , Iml vs ASP.NET Ajax – incoding meta language
  4. IML (selector)-  a description of the selectors’ usage in IML
  5. IML In Ajax – a description of the IML Operation in relation to Ajax
  6. IML template – Templates for data insertion
  7. Extensions– help with writing extensions to  comply the Don’tRepeatYourself principle
  8. Unit Test and Unit test scenario

Vlad Kopachinsky

I am a Senior developer at Incoding Software in Russian Federation. I created the Incoding Framework for rapid development

More Posts - Website - Twitter - Facebook - LinkedIn

Leave a Reply