Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lcaballero/lucidedge.datamapping

Library to help in the hand-writing of ORM code in C#.
https://github.com/lcaballero/lucidedge.datamapping

Last synced: 23 days ago
JSON representation

Library to help in the hand-writing of ORM code in C#.

Awesome Lists containing this project

README

        

# LucidEdge.DataMapping

## Overview

This library consists of code that provides some assistance when hand-writing
ORM. The current release focuses only on the PostGres DB, and doesn't attempt
to handle concurrency or asynchronicity. The hope is that later version will
use `TaskResult` or something similar.

## Getting Started

To build the project open the .sln in the Tests project, which will bring
in both the Core library, and the Samples library. Build the project and
do what you will with the resulting .dlls.

## Example

The code below is an example of a class which can be injected with a map, and
can thus map the underlying dictionary to a strongly typed interface. The
class can remap the names of columns to something more appropriate as is done
below with `CreatedAt` and `Created_At` for instance.

```CSharp
public class UserProfile : IDataMapping
{
public IDictionary Map { get; set; }

public long? Id { get { return Map["Id"].ToValue(); } }
public int? Version { get { return Map["Version"].ToValue(); } }
public DateTime? CreatedAt { get { return Map["Created_At"].ToValue(); } }
public DateTime? UpdatedOn { get { return Map["Updated_On"].ToValue(); } }
public bool IsActive { get { return Map["IsActive"].ToValue(); } }
public bool IsLocked { get { return Map["IsLocked"].ToValue(); } }

public string UserName { get { return Map["UserName"].ToValue(); } }
public string Password { get { return Map["Password"].ToValue(); } }
public int? UserGroupId { get { return Map["UserGroup_Id"].ToValue(); } }
}
```

The only requirement is that this class implements IDataMapping.
With IDataMapping any number of functions that produce Dictionaries
can be used to provide data to an instance with the underlying data.

The library comes with a few helpful extension functions for creating
these backing Dictionaries, and the following example which is pulled
from `Test_IDataMapping_Lookup` is an example use of `.Add(...)` and
`.ToDataMapping()`.

```CSharp
var dps =
new List()
.Add("id", id)
.Add("version", version)
.Add("created_at", created_at)
.Add("updated_on", updated_on)
.Add("isactive", true)
.Add("islocked", true)
.Add("usergroup_id", group_id)
.Add("password", "password")
.Add("username", "username")
.ToDataMapping();
```

## The DataBase

With the above code ready to run, we now can consider a data source,
and any data source that can be mapped to the `IDataMapping` interface
will do.

The helper methods found in `LucidEdge.DataMapping.Requests` were
designed with the idea that the underlying data would eventually
be injected into instances of `IDataMapping`.

`Requests` currently runs the function `Insert`, `Read`, `Create`, and
`DropTable` in a synchronous fashion. (Later versions of this library
may come to use either concurrency or asynchronocity).

`UserScripts.Read_All_Users` is nothing more than a Sql Script embedded
in the dll as a Resource. The empty data points are provided since
there are no required parameters, but if the script needed paramters
they could be passed as DataPoint instances.

With these functions in hand we can use them like this:

```CSharp
UserScripts.Read_All_Users.Read(
new List());
```

In truth, Read uses an optional parameter for both the parameters
and the connection, which means the above code could have been made
even simpler:

```CSharp
UserScripts.Read_All_Users.Read();
```

`Insert` most definitely requires paramters, but it too is simple:

```CSharp
var dps = new List()
.Add("id", Rand.Next())
.Add("version", Rand.Next())
.Add("created_at", DateTime.Now)
.Add("updated_on", DateTime.Now)
.Add("isactive", true)
.Add("islocked", true)
.Add("usergroup_id", Rand.Next())
.Add("password", "w4llst")
.Add("username", "lucas.caballero");

UserScripts.Insert_User.Insert(dps);
```

# License

LucidEdge.DataMapping subnamespaces and the projects in those subnamespaces
are released under the MIT License.

Copyright 2013

Read the license here: http://opensource.org/licenses/MIT

Date: 2013-2-17