Home

Getting Started

Creating a new mapping class

  1. Copy the Frog.Orm.dll assembly to the Lib directory of your project.
  2. Add a reference to the assembly.
  3. Next, add a new empty class.
  4. Add public properties to the class, that matches your database schema (column
    types and names) like in the example below.
   1:  [Table]
   2:  public class User
   3:  {
   4:       [PrimaryKey]
   5:       public long Id { get; set; }
   6:       
   7:       [Column]
   8:       public string Name { get; set; }
   9:  }

Retrieving data from the database

In order to retrieve data you first
create a mapping class, that matches the structure of the data you are trying to fetch. Next, you can instantiate a Repository object that will actually fetch the data. See example below.
   1:  public void Test()
   2:  {
   3:      using(var connection = new Connection("<connection string>");)
   4:      {
   5:          var repository = new Repository(connection)
   6:          var users = repository.GetAll<User>();
   7:          
   8:          foreach(var user in users)
   9:          {
  10:              Console.WriteLine(user.Name);
  11:          }
  12:      }
  13:  }
In the example above, the Repository object returns a set of users when GetAll<User> is called. The set is then iterated and the name og each user printed to the console.

Naturally you can also perform more complicated queries using a repository. For instance you also want to get the users that meet a certain criteria. See the example below.
   1:  public void Test()
   2:  {
   3:      using(var connection = new Connection("<connection string>");)
   4:      {
   5:          var repository = new Repository(connection)
   6:          var users = repository.GetWhere<User>(Field.And(
   7:                                                  Field.Equals("Name", "John"),
   8:                                                  Field.GreaterThan("Age", 45)
   9:                                                  )
  10:                                               );
  11:          
  12:          foreach(var user in users)
  13:          {
  14:              Console.WriteLine(user.Name);
  15:          }
  16:      }
  17:  }
The example fetches all users named John that has an age above 45. See more about conditions in Conditions. Note that the Get methods on the repository returns IEnumerables, which means you cannot iterate them twice - unless you convert them to Lists (see .ToList())

Storing data in the database

As with retrieving data, you need a mapping class to store data in the database. In most situations you can use the same mapping class that is used when fetching data. You can create a new instance of your object, and provide it as a parameter to the Create or Update method on a Repository object. See example below.
   1:  public void Test()
   2:  {
   3:      using(var connection = new Connection("<connection string>"))
   4:      {
   5:          var repository = new Repository(connection)
   6:          var user =  new User();
   7:          user.Name = "Bill Gates";
   8:          
   9:          user = repository.Create(user);
  10:          
  11:          connection.CommitChanges();
  12:      }
  13:  }
In the example above a new instance of type User is constructed and the name is then set. Subsequently the repository is asked to create this user in the database, and finally actually commit the changes. If you do not explicitly call the CommitChanges() method, the changes will be rolled back when the repository is disposed.