Model-view-controller
Jerrycurl as a design framework does away with the repository pattern and instead implements MVC similar to how ASP.NET does it for web projects.
It uses a slightly different terminology, but with
Below is a quick sketch of the terminology you will encounter with Jerrycurl and how these relate to the concepts of ASP.NET MVC.
Jerrycurl | ASP.NET |
---|---|
Relation
A relational model for binding to Razor SQL-based procedures. |
Model
An object model for binding to Razor HTML-based views. |
Procedure
Generates SQL and projects relational bindings with Razor to be consumed by the underlying ORM engine. |
View
Generates HTML and model bindings with Razor to be consumed by a web server. |
Controller
Outputs the generated HTML payload over HTTP to be consumed by a browser. |
Accessor
Executes commands and queries that either read from or writes to your database and returns the resulting data sets as a collection of objects. |
Startup
Provides initial configuration for the MVC environment, e.g. routes. |
Domain
Provides shared configuration for the MVC environment, e.g. connection strings. |
Basic setup
In this example we will set up a basic MVC structure centered around two Razor SQL-based operations:
CreateBlog.cssql
Create a new blog and add the first post.GetBlogsAndPosts.cssql
Get a list blogs with a minimum rating along with their entire archive of posts.
The MVC structure is usually represented with the following layout:
Accessors\
BlogsAccessor.cs
CommandModels\
CreateBlogModel.cs
Commands\
Blogs\
CreateBlog.cssql
Queries\
Blogs\
GetBlogsAndPosts.cssql
QueryModels\
BlogFilter.cs
BlogAndPostsView.cs
Database.cs
BlogsDomain.cs
Model
The object model is usually separated into three distinct parts:
Database.cs
contains table-to-class definitions from your database, usually generated with our toolingViews\
contains classes representing output from queriesModels\
contains classes representing input parameters for commands and queries
Domains
Set up initial configuration in BlogsDomain.cs
by implementing IDomain
in a top-level namespace.
Configuration is looked up by accessors by locating the nearest IDomain
implementation when traversing its namespace.
Therefore, to use multiple domains within the same project create each MVC-substructure in separate folders.
namespace BlogApp
{
class BlogsDomain : IDomain
{
public void Configure(DomainOptions options)
{
options.UseSqlServer("SERVER=.;DATABASE=blogdb;TRUSTED_CONNECTION=true");
}
}
}
In our example we have installed the Jerrycurl.Vendors.SqlServer
package to get the handy
UseSqlServer
extension.
Accessors
Create the controller layer in the Accessors
folder and use a naming convention like ASP.NET,
only with the Accessor
suffix instead. Add two methods representing our SQL query and command.
public class BlogsAccessor : Accessor
{
public IList<Blog> GetBlogs(int minRating)
{
var filter = new MovieFilter
{
MinYear = minYear
};
return this.Query<Blog>(model: filter);
}
public void CreatePosts(IList<BlogPost> newPosts) => this.Execute(model: newPosts);
}