Wiki

Clone wiki

Wxen.NET DbHug / Home

Welcome to DbHug docs

DbHug.It is simple Sql commands wrapper, a mini ORM.


DbHug designed to provide functionality of quering and mapping sql queries results to objects. It is assumed that DbHug can help developer to focus on the business-logic, rather than queries...
Primarily DbHug is select- and execute-based, but it has basic functionality of update, delete, insert.
DbHug was inspired by Enterprise Library Data Access Application Block, but has functionality of different existing ORMs.
To be fair DbHug is not so fast as Dapper and not so complex as EF. It was created to cover needs of small developers team and never "reinventing the wheel"... anyway...


ver 0.10.0.
Download...

release notes


On NuGet

NuGet
PM> Install-Package Wxen.DbHug


Quick overview.

Features:

  • Select strongly typed or dynamic values
  • Execute one or set of sql commands as one *
  • Execute procedures with output parameters
  • Automatically open / close or manage db connections
  • Initialize domain entities by constructors and methods
  • Customizable mappings between domain and database objects (by attributes or api)
  • Execute sql commands with DbParameters or Explicit values
  • Select a set of results and merge them together
  • Execute commands asynchronously
  • Compose commands - make an executable command as simple as string.Format()
  • Update, delete, insert values into database (CRUD)
  • Customize behavior for setting NULL values
  • Split entities for parts
  • Use custom scalar types
  • Out of the box support for: MS Sql, Sql CE, MySql, PostgreSql, Oracle, Firebird, SQLite.
  • .NET 4.5

Couple of Examples.

Examples in this wiki are for MS Sql Server but it shows concept for any relative database.

Creating instance:

#!csharp
using Wxen.Data.DbHug; //namespace
#!csharp
//Connection string to database
var connectionString = "...";
#!csharp
//Creating instance with connection string and DbFactory instance.
DbHug dbHug = new DbHug(connectionString, SqlClientFactory.Instance); 


Executing queries:

DbHug allows to select strongly typed entities or dynamyc objects. The difference is generic and non-generic methods.

Select Contacts from database.

#!csharp
var result = dbHug.Select<Person>("SELECT * FROM PERSONS WHERE FirstName = @Name AND AGE < @MaxAge;", new { Name = "John", MaxAge = 55 });
DbHug will create and execute DbCommand with specified parameters.

Built-in templating mechanism.

#!csharp
var result = dbHug.Select<Person>("SELECT * FROM PERSONS WHERE FirstName = {Name} AND AGE < {MaxAge};", new { Name = "John", MaxAge = 55 });
Result will be the same as one of the previous examples. But it has some pros which is documented additionally.
DbHug supports inner properties.
#!csharp
var person = new Person { Name = "John" };
var result = dbHug.Select<Person>("SELECT * FROM PERSONS WHERE FirstName = {person.Name} AND AGE < {MaxAge};", new { person, MaxAge = 55 });

Function:

#!csharp
var result = dbHug.Select<Person>("get_filtered_persons", new { Name = "John", MaxAge = 55 });
This command will be executed as stored procedure automatically. The same result for command text "EXEC [get_filtered_persons] @Name, @MaxAge;".

Procedure with output parameter:

DbHug allows to use a special object for output values. Output<T>.

#!csharp

var id = new Output<int>();
dbHug.ExecuteScalar("[dbo].[add_person]",
    new
    {
        FirstName = "John",
        LastName = "McClane",
        Date = new DateTime(1910, 1, 1),
        Id = id
    });
//id == 6
Instead of Output<T> it is possible to use DbParameter with .Direction = ParameterDirection.Output.

DbParameters

Values may be passed by DbParameters as params in DbHug queries.

#!csharp
var NameDbP = new SqlParameter("Name", "John");
var MaxAgeDbP = new SqlParameter("MaxAge", 55);
var result = dbHug.Select<Contact>("SELECT * FROM PERSONS WHERE FirstName = @Name AND AGE < @MaxAge;", NameDbP, MaxAgeDbP);

More here...

Updated