Clamper is a tool which can generate a library that can be used to access a database. the generated library has an optimized, rich API that can be used to do all kind of operations on a relational database without writing any SQL.
Clamper uses the Dapper Micro ORM to map the result sets. this makes the mapping high efficient. Clamper can generate libraries for .Net Framework and .Net core. the generated library has the API to access the database and it references the Clamper.Core library from Nuget. the Core library has all infrastructure that is necessary the generated library to work.
Clamper is being developed keeping the best practices in mind. you can integrate the generated code easily as it exposes a layer of abstraction to the upper layers.
You can download the latest version from the Github releases page. This page contains the 3 versions of the release which are for Windows, Linux and Mac.
The release is a self contained package which means it has no external dependencies. You can extract the downloaded package to anywhere in and add a the path to the folder to the PATH environment variable. then you will be able to run ClamperCLI by just typing ClamperCLI in the terminal. otherwise you will have to run the ClamperCLI by providing the full path to executable.
Download the new version and extract to the existing installation and replace existing files.
Basically it reads the configuration file in a folder, retrieves the metadata from the specified database in the configuration and creates a .Net library.
Below is a very basic example configuration file that will direct ClamperCLI to generate a library in the current folder. the name of the configuration file should be clamper.config.json
. the Clamper CLI will find the file automatically in the current folder.
This will create a .Net standard class library if the project file doesn't exist. if the project file exists it will modify it if necessary.
{
"connectionString":
"Server=localhost;Database=myProject;UID=root;Password=password",
"projectPath": ".",
"baseNamespace": "MyProject.DAL",
"ProjectFile": "MyProject.DAL.csproj",
"dbms": "mysql",
"schema": "MyProject"
}
To start generating, run GenieCLI from the current folder.
Reference the DAL project generated by the Clamper from any .net project and then you can start using the data access API provided by the generated library.
To start using the API,
- You must create an
IDatabaseMetadataProvider
implementation which should be something like this
using Clamper.Core.Infrastructure.Interfaces;
public class DatabaseMetadataProvider : IDatabaseMetadataProvider
{
public string GetConnectionString()
{
return "Server=localhost;Database=MyDatabase;User Id=sa; Password=password;";
}
public Dbms GetDbms()
{
return Dbms.Mssql;
}
}
- You must create an instance of
IDBContext
which should be created only once providing an instance of the newly createdDatabaseMetadataProvider
class.
If you use a dependency container, you should add bindings like below
Bind<IUnitOfWork>().To<UnitOfWork>();
Bind<IDapperContext>().To<DapperContext>().InSingletonScope();
- You can use the
Unit()
method in theIDbContext
to get anIUnitOfWork
instance.
Below is an example which retrieves list of items from a database
IDBContext dbContext = new DBContext(new DatabaseMetadataProvider());
var unit = dbContext.Unit();
var elements = unit.ElementRepository().Get()
.Query();
API has everything you need to work with a database. it supports all CRUD operations, complex filtering, sorting, paging. All calls has a Sync version and Asynchronous version.
This creates a repository for each table and view in your database and creates a list of methods for the stored procedures in your database.
You can use the repository instance from the IUnitOfWork
to invoke any operation on the table or view.
All operations are linked with a UnitOfWork
. all the operations except Read
operations are deferred until you call the Commit
method of the UnitOfWork
instance.