Quick Start

Below is a list of features with an example for each. They are basic, but to the point.

Create an Accessor
var accessor = DbFactory.CreateAccessor(
     DbAccessorType.MsSql, 
     AccessorExtension.All,
     () => new SqlConnection(CSTRING)
     );

Read a Single Value
var value = accessor.ReadValue<long>("SELECT 7");

Read an Array of ValueRead an Array of Values*
var ids = accessor.ReadValues<long>("SELECT [Id] FROM [Thing1]");

Read Single Entity
var thing1 = accessor.ReadEntity<Thing1>("SELECT * FROM [Thing1] WHERE Id = @0", 1);

Read Multiple Entities
var thing1s = accessor.ReadEntities<Thing1>("SELECT * FROM [Thing1]");

Read Multiple Entities using Parallel Mapping
var thing1s = accessor.Parallel.ReadEntities<Thing1>("SELECT * FROM [Thing1]");

Read Single Entity by It's Primary Key
var thing1 = accessor.ReadIdentity<Thing1, int>(1);
var thing2 = accessor.ReadIdentity<Thing2, int, int>(1, 2);

Read Multiple Types of Multiple Entities
var entities = accessor.ReadEntities<Thing1, Thing2>(
	@"SELECT * FROM [Thing1] WHERE [Id] = @0;
	  SELECT * FROM [Thing2] WHERE [Thing1Id] = @0;",
	  1
	  );

Read Anonymous Types
var anon1s = accessor.ReadAnonymous(new { Id = 0L, Name = "", Value = 0L },
	"SELECT * FROM [Thing1]"
	);

Read Any Type of Class
var types = accessor.ReadType<Thing1>("SELECT * FROM [Thing1]");

Read "Related" Entities
var thing1 = accessor.ReadIdentity<Thing1, int>(1);
var thing2s = accessor.ReadRelated<Thing1, Thing2>(thing1)
	.On(t1 => new { t1.Id }, t2 => new { t2.Thing1Id });

Execute SQL
accessor.Execute("DELETE FROM [Thing2] WHERE [Id] > @0", 50);

Write Multiple Entities (Even Multiple Types)
var thing2s = accessor.ReadEntities<Thing2>("SELECT * FROM [Thing2]")
	.ToList();
  
thing2s[0].Value = "one";     // update
thing2s[2].Entity.Delete();   // delete

// insert
var gnu2 = new Thing2() { Thing1Id = 1, Name = "New Thing 2", Value = "Priceless" };

thing2s.Add(gnu2);

// this is done within a transaction in one trip, all writes rollback on error
accessor.WriteEntities(thing2saccessor.WriteEntities(thing2s);

// the id has been updated wit the one generated by the d// the id has been updated wit the one generated by the db
Console.WriteLine("Gnu2 Id = '{0}'", gnu2.Id); 

Register for Accessor Database Events
// DbChangeEventArgs includes the table name, columns, and the entity affecte// DbChangeEventArgs includes the table name, columns, and the entity affected
access

Write Entities within a Transaction
var thing2s = accessor.ReadEntities<Thing2>("SELECT * FROM [Thing2]")
	.ToList();
  
thing2s[0].Value = "one";     // update
thing2s[2].Entity.Delete();   // delete

// insert
var gnu2 = new Thing2() { Thing1Id = 1, Name = "New Thing 2", Value = "Priceless" };

thing2s.Add(gnu2);

using (var scope = accessor.CreateScope())
{
   scope.WriteEntity(thing2s[0]);
   scope.WriteEntity(thing2s[1]);
   scope.WriteEntity(gnu2);
   scope.Commit();   
}

Last edited Mar 20, 2013 at 4:31 PM by TBone512, version 15

Comments

No comments yet.