ThunderstruckA really fast way to access databases
What? Thunderstruck is a .NET library that makes access to databases more simple and fast             using ADO.NET.
Another ORM?      No! Thunderstruck dont is a ORM.  It dont abstracts the powerful database,      just makes easy the acce...
How?Thunderstruck
How?       First, set your connection string.
How?<connectionStrings>    <add name="Default" (...)</connectionStrings>
How? Now, you need meeting a important object   called DataContext. It controls everyconnection, transaction and command t...
How?using (var context = new DataContext()){    context.Execute("DELETE FROM Cars");    context.Execute("DELETE FROM Tools...
How?                  Caution!       It will clean your garage :)
How?                     Yes!  Forget connection string, open connection, close connection, create command, execute   comm...
Transactional DataContext controls the connection, and its  transactional by default, nothing was made              before...
How does not Transactional?using (var context = new DataContext(Transaction.No)){    context.Execute("DELETE FROM Cars"); ...
Connection String  Maybe you want to use another specified  connection string for a different database.
Connection Stringusing (var context = new DataContext("OtherDatabase")){    context.Execute("DELETE FROM Cars");    contex...
And Queries?    Is easy to get a value on database!
DataContext.GetValuevar query = "SELECT COUNT(Id) FROM Tools";var toolsCount = context.GetValue(query);
And Queries?   Or get a list of objects on database...
DataContext.Allvar allCars = context.All<Car>("SELECT * FROM Cars");
DataContext  This is the basic uses of DataContext...            really simple and fast!
DataContext Methods     Thunderstruck
DataContext Methods● Execute -- Executes a database sql  command and returns the number of rows  affected.● ExecuteGetIden...
DataContext Methods● GetValue -- Executes a sql query and  returns the value of first column of the first  row.● GetValue<...
DataContext Methods● First<Type> -- Executes a sql query and  return the first result binded in specified  object.● All<Ty...
Parameters Binding     Thunderstruck
Parameters Binding Thunderstruck is focused on use of sql, andthis involves parameters... many parameters.
Parameters Binding     Concatenate sql parameters will            take you to hell!
Parameters Binding"INSERT INTO Car VALUES ("+ carName +", "+ modelYear+")"
Parameters Binding       Ugly and insecure, really!     Thunderstruck makes it easy...
Parameters Binding (on Sql Server)var command = "INSERT INTO Car VALUES (@Name,@ModelYear)";context.Execute(command, car);
Parameters BindingThunderstruck will bind the car properties with           the query parameters.
Future carsvar query = "SELECT * FROM Car WHERE ModelYear > @Year";car futureCars = context.All<Car>(query, DateTime.Today);
DataObjects Thunderstruck
DataObjects  Manipulate sql is powerful, but repetitive.
DataObjects   DataObjects abstracts more commons          database operations: INSERT, UPDATE, DELETE and SELECT.
DataObjectsThats operations are separated in two objects: DataObjectCommand and DataObjectQuery.
DataObjects        Assuming that this class         exists in the domain...
DataObjectsclass Car{    public int Id { get; set; }    public string Name { get; set; }    public int ModelYear { get; se...
DataObjects  In this are created two objects (DataObjects)to manipulate cars on database...
DataObjectsvar command = new DataObjectCommand<Car>();var select = new DataObjectQuery<Car>();
DataObjects    Or, more pretty, put this (separated)              in properties...
DataObjectspublic DataObjectCommand<Car> Command{    get { return new DataObjectCommand<Car>(); }}public DataObjectQuery<C...
DataObjectsOk, done. Just use.
DataObjectCommand     Thunderstruck
DataObjectCommand InsertCommand.Insert(car);
DataObjectCommand Insert   It will bind the generated primary key.
DataObjectCommand Insert// car.Id == 0Command.Insert(car);// car.Id > 0
Transactional DataObjectCommandIn this case, DataObjectCommand creates an   internal DataContext, but in transactional    ...
Transactional DataObjectCommandCommand.Insert(car, context);(...)context.Commit();
Transactional DataObjectCommand    Update and Delete has the same        transactional behavior.
DataObjectCommandcar.Name = "Lotus Esprit";Command.Update(car);Command.Delete(car);
DataObjectQuery   Thunderstruck
DataObjectQueryvar allCars = Select.All();var lotusCars = Select.All("WHERE Name Like %Lotus%");var newerCar = Select.Firs...
DataObjectQuery     Parameters Binding works here.
DataObjectQueryvar queryCar = new { Name = "Lotus Esprit" };var cars = Select.All("WHERE Name = @Name", queryCar);
Transactional DataObjectQuery     Many times is necessary get data in a  transaction scope, with Thunderstruck this       ...
Transactional DataObjectQuerySelect.With(context).First("ORDER BY ModelYear DESC");
Transactional DataObjectQuery              Done.
Custom DataObjects     Thunderstruck
Custom DataObjects     If, for example, TB_CARS is the         name of the table of cars...
Custom DataObjectCommandnew DataObjectCommand<Car>("TB_CAR");
Custom DataObjectQuery Data query is a little different, because you can/need customizing the query projection.
Custom DataObjectQuerynew DataObjectQuery<Car>("Name, ModelYear FROM TB_CAR");
Custom DataObjectQuery With the customized projection is possible  to make sql joins, giving more flexibility           to...
Custom DataObjectQuery   But, in most cases you just need the    projection with the defaults fields.
Custom DataObjectQuerynew DataObjectQuery<Car>("{0} FROM TB_CAR");
ThunderstruckA really fast way to access databases
ThunderstruckSource, download and more info at Github.         http://github.com/wagnerandrade/Thunderstruck
Thunderstruck        Thanks, thats all folks!
Upcoming SlideShare
Loading in …5
×

Thunderstruck

432 views

Published on

A really fast way to access databases with ADO.NET

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Thunderstruck

  1. 1. ThunderstruckA really fast way to access databases
  2. 2. What? Thunderstruck is a .NET library that makes access to databases more simple and fast using ADO.NET.
  3. 3. Another ORM? No! Thunderstruck dont is a ORM. It dont abstracts the powerful database, just makes easy the access, really!
  4. 4. How?Thunderstruck
  5. 5. How? First, set your connection string.
  6. 6. How?<connectionStrings> <add name="Default" (...)</connectionStrings>
  7. 7. How? Now, you need meeting a important object called DataContext. It controls everyconnection, transaction and command to the database.
  8. 8. How?using (var context = new DataContext()){ context.Execute("DELETE FROM Cars"); context.Execute("DELETE FROM Tools"); context.Commit();}
  9. 9. How? Caution! It will clean your garage :)
  10. 10. How? Yes! Forget connection string, open connection, close connection, create command, execute command... DataContext does it for you!
  11. 11. Transactional DataContext controls the connection, and its transactional by default, nothing was made before the commit.
  12. 12. How does not Transactional?using (var context = new DataContext(Transaction.No)){ context.Execute("DELETE FROM Cars"); context.Execute("DELETE FROM Tools");}
  13. 13. Connection String Maybe you want to use another specified connection string for a different database.
  14. 14. Connection Stringusing (var context = new DataContext("OtherDatabase")){ context.Execute("DELETE FROM Cars"); context.Execute("DELETE FROM Tools"); context.Commit();}
  15. 15. And Queries? Is easy to get a value on database!
  16. 16. DataContext.GetValuevar query = "SELECT COUNT(Id) FROM Tools";var toolsCount = context.GetValue(query);
  17. 17. And Queries? Or get a list of objects on database...
  18. 18. DataContext.Allvar allCars = context.All<Car>("SELECT * FROM Cars");
  19. 19. DataContext This is the basic uses of DataContext... really simple and fast!
  20. 20. DataContext Methods Thunderstruck
  21. 21. DataContext Methods● Execute -- Executes a database sql command and returns the number of rows affected.● ExecuteGetIdentity -- Executes a sql command and returns the last identity value inserted.
  22. 22. DataContext Methods● GetValue -- Executes a sql query and returns the value of first column of the first row.● GetValue<Type> -- Same as GetValue, but makes a Convert to specified type.● GetValues<Type> -- Executes a sql query and returns list of values of first column in the specified type.
  23. 23. DataContext Methods● First<Type> -- Executes a sql query and return the first result binded in specified object.● All<Type> -- Same as first, but returns a list of all rows.
  24. 24. Parameters Binding Thunderstruck
  25. 25. Parameters Binding Thunderstruck is focused on use of sql, andthis involves parameters... many parameters.
  26. 26. Parameters Binding Concatenate sql parameters will take you to hell!
  27. 27. Parameters Binding"INSERT INTO Car VALUES ("+ carName +", "+ modelYear+")"
  28. 28. Parameters Binding Ugly and insecure, really! Thunderstruck makes it easy...
  29. 29. Parameters Binding (on Sql Server)var command = "INSERT INTO Car VALUES (@Name,@ModelYear)";context.Execute(command, car);
  30. 30. Parameters BindingThunderstruck will bind the car properties with the query parameters.
  31. 31. Future carsvar query = "SELECT * FROM Car WHERE ModelYear > @Year";car futureCars = context.All<Car>(query, DateTime.Today);
  32. 32. DataObjects Thunderstruck
  33. 33. DataObjects Manipulate sql is powerful, but repetitive.
  34. 34. DataObjects DataObjects abstracts more commons database operations: INSERT, UPDATE, DELETE and SELECT.
  35. 35. DataObjectsThats operations are separated in two objects: DataObjectCommand and DataObjectQuery.
  36. 36. DataObjects Assuming that this class exists in the domain...
  37. 37. DataObjectsclass Car{ public int Id { get; set; } public string Name { get; set; } public int ModelYear { get; set; }}
  38. 38. DataObjects In this are created two objects (DataObjects)to manipulate cars on database...
  39. 39. DataObjectsvar command = new DataObjectCommand<Car>();var select = new DataObjectQuery<Car>();
  40. 40. DataObjects Or, more pretty, put this (separated) in properties...
  41. 41. DataObjectspublic DataObjectCommand<Car> Command{ get { return new DataObjectCommand<Car>(); }}public DataObjectQuery<Car> Select{ get { return new DataObjectQuery<Car>(); }}
  42. 42. DataObjectsOk, done. Just use.
  43. 43. DataObjectCommand Thunderstruck
  44. 44. DataObjectCommand InsertCommand.Insert(car);
  45. 45. DataObjectCommand Insert It will bind the generated primary key.
  46. 46. DataObjectCommand Insert// car.Id == 0Command.Insert(car);// car.Id > 0
  47. 47. Transactional DataObjectCommandIn this case, DataObjectCommand creates an internal DataContext, but in transactional procedures you can specify it.
  48. 48. Transactional DataObjectCommandCommand.Insert(car, context);(...)context.Commit();
  49. 49. Transactional DataObjectCommand Update and Delete has the same transactional behavior.
  50. 50. DataObjectCommandcar.Name = "Lotus Esprit";Command.Update(car);Command.Delete(car);
  51. 51. DataObjectQuery Thunderstruck
  52. 52. DataObjectQueryvar allCars = Select.All();var lotusCars = Select.All("WHERE Name Like %Lotus%");var newerCar = Select.First("ORDER BY ModelYear DESC");
  53. 53. DataObjectQuery Parameters Binding works here.
  54. 54. DataObjectQueryvar queryCar = new { Name = "Lotus Esprit" };var cars = Select.All("WHERE Name = @Name", queryCar);
  55. 55. Transactional DataObjectQuery Many times is necessary get data in a transaction scope, with Thunderstruck this means in a DataContext.
  56. 56. Transactional DataObjectQuerySelect.With(context).First("ORDER BY ModelYear DESC");
  57. 57. Transactional DataObjectQuery Done.
  58. 58. Custom DataObjects Thunderstruck
  59. 59. Custom DataObjects If, for example, TB_CARS is the name of the table of cars...
  60. 60. Custom DataObjectCommandnew DataObjectCommand<Car>("TB_CAR");
  61. 61. Custom DataObjectQuery Data query is a little different, because you can/need customizing the query projection.
  62. 62. Custom DataObjectQuerynew DataObjectQuery<Car>("Name, ModelYear FROM TB_CAR");
  63. 63. Custom DataObjectQuery With the customized projection is possible to make sql joins, giving more flexibility to queries, like Views.
  64. 64. Custom DataObjectQuery But, in most cases you just need the projection with the defaults fields.
  65. 65. Custom DataObjectQuerynew DataObjectQuery<Car>("{0} FROM TB_CAR");
  66. 66. ThunderstruckA really fast way to access databases
  67. 67. ThunderstruckSource, download and more info at Github. http://github.com/wagnerandrade/Thunderstruck
  68. 68. Thunderstruck Thanks, thats all folks!

×