Figure 1: Incoding
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 the following three projects:
- Domain (class library) - is responsible for business logic and database operations.
- UI (ASP.NET MVC project) - front-end based on ASP.NET MVC.
- 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 to 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, StructureMap is installed as loC framework, but there is a provider for Ninject, and it is also possible to write your own implementations.
- namespace Example.Domain
- {
- #region << Using >>
- using System;
- using System.Configuration;
- using System.IO;
- using System.Linq;
- using System.Web.Mvc;
- using FluentNHibernate.Cfg;
- using FluentNHibernate.Cfg.Db;
- using FluentValidation;
- using FluentValidation.Mvc;
- using Incoding.Block.IoC;
- using Incoding.Block.Logging;
- using Incoding.CQRS;
- using Incoding.Data;
- using Incoding.EventBroker;
- using Incoding.Extensions;
- using Incoding.MvcContrib;
- using NHibernate.Tool.hbm2ddl;
- using StructureMap.Graph;
- #endregion
- public static class Bootstrapper
- {
- public static void Start()
- {
-
- LoggingFactory.Instance.Initialize(logging =>
- {
- string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log");
- logging.WithPolicy(policy => policy.For(LogType.Debug)
- .Use(FileLogger.WithAtOnceReplace(path,
- () => "Debug_{0}.txt".F(DateTime.Now.ToString("yyyyMMdd")))));
- });
-
- IoCFactory.Instance.Initialize(init => init.WithProvider(new StructureMapIoCProvider(registry =>
- {
- registry.For<IDispatcher>().Use<DefaultDispatcher>();
- registry.For<IEventBroker>().Use<DefaultEventBroker>();
- registry.For<ITemplateFactory>().Singleton().Use<TemplateHandlebarsFactory>();
-
- var configure = Fluently
- .Configure()
- .Database(MsSqlConfiguration.MsSql2008.ConnectionString(ConfigurationManager.ConnectionStrings["Example"].ConnectionString))
- .Mappings(configuration => configuration.FluentMappings.AddFromAssembly(typeof(Bootstrapper).Assembly))
- .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true))
- .CurrentSessionContext<NhibernateSessionContext>();
-
- registry.For<INhibernateSessionFactory>().Singleton().Use(() => new NhibernateSessionFactory(configure));
- registry.For<IUnitOfWorkFactory>().Use<NhibernateUnitOfWorkFactory>();
- registry.For<IRepository>().Use<NhibernateRepository>();
-
- registry.Scan(r =>
- {
- r.TheCallingAssembly();
- r.WithDefaultConventions();
- r.ConnectImplementationsToTypesClosing(typeof(AbstractValidator<>));
- r.ConnectImplementationsToTypesClosing(typeof(IEventSubscriber<>));
- r.AddAllTypesOf<ISetUp>();
- });
- })));
- ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new IncValidatorFactory()));
- FluentValidationModelValidatorProvider.Configure();
-
- foreach (var setUp in IoCFactory.Instance.ResolveAll<ISetUp>().OrderBy(r => r.GetOrder()))
- {
- setUp.Execute();
- }
- var ajaxDef = JqueryAjaxOptions.Default;
- ajaxDef.Cache = false;
- }
- }
- }
Further on, commands (Command) and queries (Query) are added to the 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.
- public class DispatcherController : DispatcherControllerBase
- {
- #region Constructors
- public DispatcherController()
- : base(typeof(Bootstrapper).Assembly) { }
- #endregion
- }
After the installation, the client logic is added to the UI using IML.
UnitTests
During the installation of Incoding tests helpers, the project is added by the MSpecAssemblyContext.csis file, in which connection is customized to the test database.
- public class MSpecAssemblyContext : IAssemblyContext
- {
- #region IAssemblyContext Members
-
- public void OnAssemblyStart()
- {
-
- var configure = Fluently
- .Configure()
- .Database(MsSqlConfiguration.MsSql2008
- .ConnectionString(ConfigurationManager.ConnectionStrings["Example_Test"].ConnectionString)
- .ShowSql())
- .Mappings(configuration => configuration.FluentMappings.AddFromAssembly(typeof(Bootstrapper).Assembly));
- PleasureForData.StartNhibernate(configure, true);
- }
-
- public void OnAssemblyComplete() { }
-
- #endregion
- }
Part 1: Installation
So, we proceed to the task of the disclaimer 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 three projects. We begin with the project that is responsible for business logic of the application - Domain.
Create class library Domain.
Figure 2: Library
Then we proceed to the front-end and create, install ASP.NET Web Application UI with links to the MVC packages as template, empty project.
Figure 3: Empty Project
Figure 4: MVC App
Finally, we added class library UnitTests responsible for unit testing.
Figure 5: Unit Testing
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 the following solution.
Figure 6: Solution
After we create the solution structure, we need to install Incoding Framework package from Nuget. The installation carried out by Nuget.
Here is the same algorithm of installation for all the projects.
- Right-click the project and select Manage NuGet Packages… in the context menu
- Search incoding
- Select necessary package and install it
First install Incoding framework in Domain
Figure 7: Framework Domain
Then add to the file Domain, Infrastructure, Bootstrapper.cs link to StructureMap.Graph.
Figure 8: Structure MAp Graph
2 packages must be installed to UI:
- Incoding Meta Language
- Incoding Meta Language Contrib (optional)
Figure 9: Incoding Meta
Figure 10: ExampleUI
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 - Go to Views, Shared, then _Layout.cshtml so that it looks as in the following:
- @using Incoding.MvcContrib
- <!DOCTYPE html>
- <html >
- <head>
- <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")"> </script>
- <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.10.2.min.js")"></script>
- <script type="text/javascript" src="@Url.Content("~/Scripts/underscore.min.js")"> </script>
- <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.form.min.js")"> </script>
- <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.history.js")"> </script>
- <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"> </script>
- <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"> </script>
- <script type="text/javascript" src="@Url.Content("~/Scripts/handlebars-1.1.2.js")"> </script>
- <script type="text/javascript" src="@Url.Content("~/Scripts/incoding.framework.min.js")"> </script>
- <script type="text/javascript" src="@Url.Content("~/Scripts/incoding.meta.language.contrib.js")"> </script>
- <script type="text/javascript" src="@Url.Content("~/Scripts/bootstrap.min.js")"> </script>
- <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/bootstrap.min.css")">
- <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/themes/base/jquery.ui.core.css")">
- <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/themes/base/jquery.ui.datepicker.css")">
- <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/themes/base/jquery.ui.dialog.css")">
- <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/themes/base/jquery.ui.theme.css")">
- <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/themes/base/jquery.ui.menu.css")">
- <script>
- TemplateFactory.Version = '@Guid.NewGuid().ToString()';
- </script>
- </head>
- @Html.Incoding().RenderDropDownTemplate()
- <body>
- @RenderBody()
- </body>
- </html>
Then add the link to Bootstrapper.cs to the files Example.UI, go to App_Start, IncodingStart.cs and select Example.UI, then click Controllers and DispatcherController.cs.
Figure 11: Bootstrapper
Figure 12: Coding
Note: If you use MVC5, it’s necessary for framework to add the following code to Web.config file.
- <dependentAssembly>
- <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
- <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
- </dependentAssembly>
Now install Incoding tests helpers in UnitTests and add the link to Bootstrapper.cs in Example.UnitTests, MSpecAssemblyContext.cs.
Figure 13: Incoding Test
Figure 14: Output
The last phase of the preparing the projects to work is to create folders structure for the projects. Add the following folders to the Example.Domain project:
- Operations – command and query of the project.
- Persistence – entities for DB mapping.
- Specifications – where and order specifications for data cleaning when request is made.
Figure 15: Opretions
In the Example.UnitTests project create just the same folders structure as in Example.Domain.
Figure 16: Example Unit Test
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.
Figure 17: Database
Figure 18: New Database
Figure 19: Create Table
In order to work with DB, you need to set up a connection. Add to the file Example.UI, then Web.config and Example.UnitTests, then app.config connection string to the BD.
- <connectionStrings>
- <add name="Example" connectionString="Data Source=INCODING-PC\SQLEXPRESS;Database=Example;Integrated Security=false; User Id=sa;Password=1" providerName="System.Data.SqlClient" />
- <add name="Example_Test" connectionString="Data Source=INCODING-PC\SQLEXPRESS;Database=Example_Test;Integrated Security=true" providerName="System.Data.SqlClient" />
- </connectionStrings>
In the file Example.Domain, go to Infrastructure and click Bootstrapper.cs, register the appropriate connection string using a key called Example.
-
- var configure = Fluently
- .Configure()
- .Database(MsSqlConfiguration.MsSql2008.ConnectionString(ConfigurationManager.ConnectionStrings["Example"].ConnectionString))
- .Mappings(configuration => configuration.FluentMappings.AddFromAssembly(typeof(Bootstrapper).Assembly))
- .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true))
- .CurrentSessionContext();
In the file Example.UnitTests -> MSpecAssemblyContext.cs, register the connection string to the BD using the key called Example_test.
-
- var configure = Fluently
- .Configure()
- .Database(MsSqlConfiguration.MsSql2008
- .ConnectionString(ConfigurationManager.ConnectionStrings["Example_Test"].ConnectionString)
- .ShowSql())
- .Mappings(configuration => configuration.FluentMappings.AddFromAssembly(typeof(Bootstrapper).Assembly));
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 the 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
- namespace Example.Domain
- {
- #region << Using >>
-
- using System;
- using Incoding.Data;
-
- #endregion
-
- public class Human : IncEntityBase
- {
- #region Properties
-
- public virtual DateTime Birthday { get; set; }
-
- public virtual string FirstName { get; set; }
-
- public virtual string Id { get; set; }
-
- public virtual string LastName { get; set; }
-
- public virtual Sex Sex { get; set; }
-
- #endregion
-
- #region Nested Classes
-
- public class Map : NHibernateEntityMap<Human>
- {
- #region Constructors
-
- protected Map()
- {
- IdGenerateByGuid(r => r.Id);
- MapEscaping(r => r.FirstName);
- MapEscaping(r => r.LastName);
- MapEscaping(r => r.Birthday);
- MapEscaping(r => r.Sex);
- }
-
- #endregion
- }
-
- #endregion
- }
-
- public enum Sex
- {
- Male = 1,
-
- Female = 2
- }
- }
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 and AddOrEditHumanCommand.cs to the project.
AddOrEditHumanCommand.cs
- namespace Example.Domain
- {
- #region << Using >>
-
- using System;
- using FluentValidation;
- using Incoding.CQRS;
- using Incoding.Extensions;
-
- #endregion
-
- public class AddOrEditHumanCommand : CommandBase
- {
- #region Properties
-
- public DateTime BirthDay { get; set; }
-
- public string FirstName { get; set; }
-
- public string Id { get; set; }
-
- public string LastName { get; set; }
-
- public Sex Sex { get; set; }
-
- #endregion
-
- public override void Execute()
- {
- var human = Repository.GetById<Human>(Id) ?? new Human();
-
- human.FirstName = FirstName;
- human.LastName = LastName;
- human.Birthday = BirthDay;
- human.Sex = Sex;
-
- Repository.SaveOrUpdate(human);
- }
- }
- }
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
- namespace Example.Domain
- {
- #region << Using >>
-
- using System.Collections.Generic;
- using System.Linq;
- using Incoding.CQRS;
-
- #endregion
-
- public class GetPeopleQuery : QueryBase<List<GetPeopleQuery.Response>>
- {
- #region Properties
-
- public string Keyword { get; set; }
-
- #endregion
-
- #region Nested Classes
-
- public class Response
- {
- #region Properties
-
- public string Birthday { get; set; }
-
- public string FirstName { get; set; }
-
- public string Id { get; set; }
-
- public string LastName { get; set; }
-
- public string Sex { get; set; }
-
- #endregion
- }
-
- #endregion
-
- protected override List<Response> ExecuteResult()
- {
- return Repository.Query<Human>().Select(human => new Response
- {
- Id = human.Id,
- Birthday = human.Birthday.ToShortDateString(),
- FirstName = human.FirstName,
- LastName = human.LastName,
- Sex = human.Sex.ToString()
- }).ToList();
- }
- }
- }
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
- namespace Example.Domain
- {
- #region << Using >>
-
- using Incoding.CQRS;
-
- #endregion
-
- public class DeleteHumanCommand : CommandBase
- {
- #region Properties
-
- public string HumanId { get; set; }
-
- #endregion
-
- public override void Execute()
- {
- Repository.Delete<Human>(HumanId);
- }
- }
- }
In order to populate the DB with initial data, add the file Example.Domain -> InitPeople.cs that is derived from the ISetUP interface.
ISetup
- using System;
-
- namespace Incoding.CQRS
- {
- public interface ISetUp : IDisposable
- {
- int GetOrder();
-
- void Execute();
- }
- }
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
- namespace Example.Domain
- {
- #region << Using >>
-
- using System;
- using Incoding.Block.IoC;
- using Incoding.CQRS;
- using NHibernate.Util;
-
- #endregion
-
- public class InitPeople : ISetUp
- {
- public void Dispose() { }
-
- public int GetOrder()
- {
- return 0;
- }
-
- public void Execute()
- {
-
- var dispatcher = IoCFactory.Instance.TryResolve<IDispatcher>();
-
-
- if (dispatcher.Query(new GetEntitiesQuery<Human>()).Any())
- return;
-
-
- dispatcher.Push(new AddOrEditHumanCommand
- {
- FirstName = "Hellen",
- LastName = "Jonson",
- BirthDay = Convert.ToDateTime("06/05/1985"),
- Sex = Sex.Female
- });
- dispatcher.Push(new AddOrEditHumanCommand
- {
- FirstName = "John",
- LastName = "Carlson",
- BirthDay = Convert.ToDateTime("06/07/1985"),
- Sex = Sex.Male
- });
- }
- }
- }
The back-end implementation of the CRUD is ready. Now it is the 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, then AddOrEditHuman.cshtml.
AddOrEditHuman.cshtml
- @using Example.Domain
- @using Incoding.MetaLanguageContrib
- @using Incoding.MvcContrib
- @model Example.Domain.AddOrEditHumanCommand
- @*Submit form for AddOrEditHumanCommand*@
- @using (Html.When(JqueryBind.Submit)
- @*Prevent default behavior and submit form by Ajax*@
- .PreventDefault()
- .Submit()
- .OnSuccess(dsl =>
- {
- dsl.WithId("PeopleTable").Core().Trigger.Incoding();
- dsl.WithId("dialog").JqueryUI().Dialog.Close();
- })
- .OnError(dsl => dsl.Self().Core().Form.Validation.Refresh())
- .AsHtmlAttributes(new
- {
- action = Url.Dispatcher().Push(new AddOrEditHumanCommand()),
- enctype = "multipart/form-data",
- method = "POST"
- })
- .ToBeginTag(Html, HtmlTag.Form))
- {
- <div>
- @Html.HiddenFor(r => r.Id)
- @Html.ForGroup(r => r.FirstName).TextBox(control => control.Label.Name = "First name")
- <br/>
- @Html.ForGroup(r => r.LastName).TextBox(control => control.Label.Name = "Last name")
- <br/>
- @Html.ForGroup(r => r.BirthDay).TextBox(control => control.Label.Name = "Birthday")
- <br/>
- @Html.ForGroup(r => r.Sex).DropDown(control => control.Input.Data = typeof(Sex).ToSelectList())
- </div>
-
- <div>
- <input type="submit" value="Save"/>
- @*Закрытие диалога*@
- @(Html.When(JqueryBind.Click)
- .PreventDefault()
- .StopPropagation()
- .Direct()
- .OnSuccess(dsl => { dsl.WithId("dialog").JqueryUI().Dialog.Close(); })
- .AsHtmlAttributes()
- .ToButton("Cancel"))
- </div>
- }
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, then HumanTmpl.cshtml.
HumanTmpl.cshtml
- @using Example.Domain
- @using Incoding.MetaLanguageContrib
- @using Incoding.MvcContrib
- @{
- using (var template = Html.Incoding().Template<GetPeopleQuery.Response>())
- {
- <table class="table">
- <thead>
- <tr>
- <th>
- First name
- </th>
- <th>
- Last name
- </th>
- <th>
- Birthday
- </th>
- <th>
- Sex
- </th>
- <th></th>
- </tr>
- </thead>
- <tbody>
- @using (var each = template.ForEach())
- {
- <tr>
- <td>
- @each.For(r => r.FirstName)
- </td>
- <td>
- @each.For(r => r.LastName)
- </td>
- <td>
- @each.For(r => r.Birthday)
- </td>
- <td>
- @each.For(r => r.Sex)
- </td>
- <td>
- @*Open edit dialog form*@
- @(Html.When(JqueryBind.Click)
- .AjaxGet(Url.Dispatcher().Model<AddOrEditHumanCommand>(new
- {
- Id = each.For(r => r.Id),
- FirstName = each.For(r => r.FirstName),
- LastName = each.For(r => r.LastName),
- BirthDay = each.For(r => r.Birthday),
- Sex = each.For(r => r.Sex)
- }).AsView("~/Views/Home/AddOrEditHuman.cshtml"))
- .OnSuccess(dsl => dsl.WithId("dialog").Behaviors(inDsl =>
- {
- inDsl.Core().Insert.Html();
- inDsl.JqueryUI().Dialog.Open(option =>
- {
- option.Resizable = false;
- option.Title = "Edit human";
- });
- }))
- .AsHtmlAttributes()
- .ToButton("Edit"))
- @*Button delete*@
- @(Html.When(JqueryBind.Click)
- .AjaxPost(Url.Dispatcher().Push(new DeleteHumanCommand() { HumanId = each.For(r => r.Id) }))
- .OnSuccess(dsl => dsl.WithId("PeopleTable").Core().Trigger.Incoding())
- .AsHtmlAttributes()
- .ToButton("Delete"))
- </td>
- </tr>
- }
- </tbody>
- </table>
- }
- }
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 loading, the AJAX query is transmitted to the server for obtaining data from the GetPeopleQuery and mapping of data using HumanTmpl: change the file Example.UI, View, Home, then Index.cshtml so that it looks like the following code snippet:
Index.cshtml
- @using Example.Domain
- @using Incoding.MetaLanguageContrib
- @using Incoding.MvcContrib
- @{
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <div id="dialog"></div>
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. Firstly, we need to add a server code. Add the following code in AddOrEditHumanCommand as a nested class.
- #region Nested Classes
-
- public class Validator : AbstractValidator
- {
- #region Constructors
-
- public Validator()
- {
- RuleFor(r => r.FirstName).NotEmpty();
- RuleFor(r => r.LastName).NotEmpty();
- }
-
- #endregion
- }
-
- #endregion
On the AddOrEditHuman.cshtml form, we used constructs like this:Hide Copy Code.
@Html.ForGroup()
It is therefore not necessary to add
@Html.ValidationMessageFor()
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 cleaningAnother 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. Firstly, add two specification files Example.Domain, Specifications, then HumanByFirstNameWhereSpec.cs and Example.UI, Specifications, then HumanByLastNameWhereSpec.cs
HumanByFirstNameWhereSpec.cs
- namespace Example.Domain
- {
- #region << Using >>
-
- using System;
- using System.Linq.Expressions;
- using Incoding;
-
- #endregion
-
- public class HumanByFirstNameWhereSpec : Specification
- {
- #region Fields
-
- readonly string firstName;
-
- #endregion
-
- #region Constructors
-
- public HumanByFirstNameWhereSpec(string firstName)
- {
- this.firstName = firstName;
- }
-
- #endregion
-
- public override Expression<Func<Human, bool>> IsSatisfiedBy()
- {
- if (string.IsNullOrEmpty(this.firstName))
- return null;
-
- return human => human.FirstName.ToLower().Contains(this.firstName.ToLower());
- }
- }
- }
HumanByLastNameWhereSpec.cs
- namespace Example.Domain
- {
- #region << Using >>
-
- using System;
- using System.Linq.Expressions;
- using Incoding;
-
- #endregion
-
- public class HumanByLastNameWhereSpec : Specification
- {
- #region Fields
-
- readonly string lastName;
-
- #endregion
-
- #region Constructors
-
- public HumanByLastNameWhereSpec(string lastName)
- {
- this.lastName = lastName.ToLower();
- }
-
- #endregion
-
- public override Expression<Func<Human, bool>> IsSatisfiedBy()
- {
- if (string.IsNullOrEmpty(this.lastName))
- return null;
-
- return human => human.LastName.ToLower().Contains(this.lastName);
- }
- }
- }
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
- namespace Example.Domain
- {
- #region << Using >>
-
- using System.Collections.Generic;
- using System.Linq;
- using Incoding.CQRS;
- using Incoding.Extensions;
-
- #endregion
-
- public class GetPeopleQuery : QueryBase<List<GetPeopleQuery.Response>>
- {
- #region Properties
-
- public string Keyword { get; set; }
-
- #endregion
-
- #region Nested Classes
-
- public class Response
- {
- #region Properties
-
- public string Birthday { get; set; }
-
- public string FirstName { get; set; }
-
- public string Id { get; set; }
-
- public string LastName { get; set; }
-
- public string Sex { get; set; }
-
- #endregion
- }
-
- #endregion
-
- protected override List<Response> ExecuteResult()
- {
- return Repository.Query(whereSpecification: new HumanByFirstNameWhereSpec(Keyword)
- .Or(new HumanByLastNameWhereSpec(Keyword)))
- .Select(human => new Response
- {
- Id = human.Id,
- Birthday = human.Birthday.ToShortDateString(),
- FirstName = human.FirstName,
- LastName = human.LastName,
- Sex = human.Sex.ToString()
- }).ToList();
- }
- }
- }
Finally, it only remains to modify Index.cshtml in order to add a search box that uses a Keyword field for data cleaning while a request is being processed.
Index.cshtml
- @using Example.Domain
- @using Incoding.MetaLanguageContrib
- @using Incoding.MvcContrib
- @{
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <div id="dialog"></div>
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 Persistences of the UnitTests project.
When_save_Human.cs
- namespace Example.UnitTests.Persistences
- {
- #region << Using >>
-
- using Example.Domain;
- using Incoding.MSpecContrib;
- using Machine.Specifications;
-
- #endregion
-
- [Subject(typeof(Human))]
- public class When_save_Human : SpecWithPersistenceSpecification
- {
- #region Fields
-
- It should_be_verify = () => persistenceSpecification.VerifyMappingAndSchema();
-
- #endregion
- }
- }
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
- namespace Example.UnitTests.Specifications
- {
- #region << Using >>
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Example.Domain;
- using Incoding.MSpecContrib;
- using Machine.Specifications;
-
- #endregion
-
- [Subject(typeof(HumanByFirstNameWhereSpec))]
- public class When_human_by_first_name
- {
- #region Fields
-
- Establish establish = () =>
- {
- Func<string, Human> createEntity = (firstName) =>
- Pleasure.MockStrictAsObject(mock =>
- mock.SetupGet(r => r.FirstName).Returns(firstName));
-
- fakeCollection = Pleasure.ToQueryable(createEntity(Pleasure.Generator.TheSameString()),
- createEntity(Pleasure.Generator.String()));
- };
-
- Because of = () =>
- {
- filterCollection = fakeCollection
- .Where(new HumanByFirstNameWhereSpec(Pleasure.Generator.TheSameString()).IsSatisfiedBy())
- .ToList();
- };
-
- It should_be_filter = () =>
- {
- filterCollection.Count.ShouldEqual(1);
- filterCollection[0].FirstName.ShouldBeTheSameString();
- };
-
- #endregion
-
- #region Establish value
-
- static IQueryable fakeCollection;
-
- static List filterCollection;
-
- #endregion
- }
- }
When_human_by_last_name.cs
- namespace Example.UnitTests.Specifications
- {
- #region << Using >>
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Example.Domain;
- using Incoding.MSpecContrib;
- using Machine.Specifications;
-
- #endregion
-
- [Subject(typeof(HumanByLastNameWhereSpec))]
- public class When_human_by_last_name
- {
- #region Fields
-
- Establish establish = () =>
- {
- Func<string, Human> createEntity = (lastName) =>
- Pleasure.MockStrictAsObject(mock =>
- mock.SetupGet(r => r.LastName)
- .Returns(lastName));
-
- fakeCollection = Pleasure.ToQueryable(createEntity(Pleasure.Generator.TheSameString()),
- createEntity(Pleasure.Generator.String()));
- };
-
- Because of = () =>
- {
- filterCollection = fakeCollection
- .Where(new HumanByLastNameWhereSpec(Pleasure.Generator.TheSameString()).IsSatisfiedBy())
- .ToList();
- };
-
- It should_be_filter = () =>
- {
- filterCollection.Count.ShouldEqual(1);
- filterCollection[0].LastName.ShouldBeTheSameString();
- };
-
- #endregion
-
- #region Establish value
-
- static IQueryable fakeCollection;
-
- static List filterCollection;
-
- #endregion
- }
- }
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
- namespace Example.UnitTests.Operations
- {
- #region << Using >>
-
- using System.Collections.Generic;
- using Example.Domain;
- using Incoding.Extensions;
- using Incoding.MSpecContrib;
- using Machine.Specifications;
-
- #endregion
-
- [Subject(typeof(GetPeopleQuery))]
- public class When_get_people
- {
- #region Fields
-
- Establish establish = () =>
- {
- var query = Pleasure.Generator.Invent<GetPeopleQuery>();
-
- human = Pleasure.Generator.Invent<Human>();
-
- expected = new List<GetPeopleQuery.Response>();
-
- mockQuery = MockQuery<GetPeopleQuery, List<GetPeopleQuery.Response>>
- .When(query)
-
- .StubQuery(whereSpecification: new HumanByFirstNameWhereSpec(query.Keyword)
- .Or(new HumanByLastNameWhereSpec(query.Keyword)),
- entities: human);
- };
-
- Because of = () => mockQuery.Original.Execute();
-
-
- It should_be_result = () => mockQuery.ShouldBeIsResult(list => list.ShouldEqualWeakEach(new List<Human>() { human },
- (dsl, i) => dsl.ForwardToValue(r => r.Birthday, human.Birthday.ToShortDateString())
- .ForwardToValue(r => r.Sex, human.Sex.ToString())
- ));
-
- #endregion
-
- #region Establish value
-
- static MockMessage<GetPeopleQuery, List<GetPeopleQuery.Response>> mockQuery;
-
- static List<GetPeopleQuery.Response> expected;
-
- static Human human;
-
- #endregion
- }
- }
When_add_human.cs
- namespace Example.UnitTests.Operations
- {
- #region << Using >>
-
- using Example.Domain;
- using Incoding.MSpecContrib;
- using Machine.Specifications;
-
- #endregion
-
- [Subject(typeof(AddOrEditHumanCommand))]
- public class When_add_human
- {
- #region Fields
-
- Establish establish = () =>
- {
- var command = Pleasure.Generator.Invent<AddOrEditHumanCommand>();
-
- mockCommand = MockCommand<AddOrEditHumanCommand>
- .When(command)
-
- .StubGetById<Human>(command.Id, null);
- };
-
- Because of = () => mockCommand.Original.Execute();
-
- It should_be_saved = () => mockCommand.ShouldBeSaveOrUpdate<Human>(human => human.ShouldEqualWeak(mockCommand.Original));
-
- #endregion
-
- #region Establish value
-
- static MockMessage<AddOrEditHumanCommand, object> mockCommand;
-
- #endregion
- }
- }
When_edit_human.cs
- namespace Example.UnitTests.Operations
- {
- #region << Using >>
-
- using Example.Domain;
- using Incoding.MSpecContrib;
- using Machine.Specifications;
-
- #endregion
-
- [Subject(typeof(AddOrEditHumanCommand))]
- public class When_edit_human
- {
- #region Fields
-
- Establish establish = () =>
- {
- var command = Pleasure.Generator.Invent<AddOrEditHumanCommand>();
-
- human = Pleasure.Generator.Invent<Human>();
-
- mockCommand = MockCommand<AddOrEditHumanCommand>
- .When(command)
-
- .StubGetById(command.Id, human);
- };
-
- Because of = () => mockCommand.Original.Execute();
-
- It should_be_saved = () => mockCommand.ShouldBeSaveOrUpdate<Human>(human => human.ShouldEqualWeak(mockCommand.Original));
-
- #endregion
-
- #region Establish value
-
- static MockMessage<AddOrEditHumanCommand, object> mockCommand;
-
- static Human human;
-
- #endregion
- }
- }