2. ETAT DES LIEUX : EF CORE 1.0
Nouvelles plateformes
.NET CORE (ASP.NET Core, UWP)
.NET Framework (ASP.NET, WPF, Windows Forms)
XAMARIN
Windows Phone
Windows Store
MAC
Linux
3. ETAT DES LIEUX : EF CORE 1.0
Nouvelles sources de données
Relationnelles (SQL Server, SQL Lite, SQL Compact, Postgres, MySQL, Oracle)
Non relationnelles (Redis, Azure Table Storage)
InMemory (à utiliser pour les test)
Providers disponibles uniquement pour les SGBD relationnels pour la v 1.0
Possibilité de créer son propre provider
4. ENTITY FRAMEWORK CORE 1.0
On repart de Zero mais on garde le meilleur
Code First
Code First from Database
Approche totalement modulaire
IOC friendly
Totalement Open source comme tout ce qui finit par Core… donc code source disponible sur
GitHub et possiblité de contribuer son l’évolution.
EF Core n’est pas production ready
8. ENTITY FRAMEWORK CORE 1.0
Package à installer
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1 "
Création d’un DbContext
public class CookBookContext : DbContext
{
public CookBookContext(DbContextOptions<CookBookContext> options) : base(options) { }
public DbSet<Book> Book { get; set; }
public DbSet<Recipe> Recipe { get; set; }
}
15. MICRO ORMS : DAPPER
POCO
DYNAMIC OBJECTS
MULTIPLE RESULTS
STORED PROCEDURE
TRANSACTION
ASYNC
PROVIDERS: SQLLite, SQL CE, Firebird, Oracle, MySQL, PostgreSQL and SQL Server
16. MICRO ORMS : DAPPER
CONFIG:
public void ConfigureServices(IServiceCollection services)
{
services.Add(new ServiceDescriptor(typeof(IConfiguration),
provider => new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json",
optional: false,
reloadOnChange: true)
.Build(),
ServiceLifetime.Singleton));
services.AddScoped<ICookBookRepository, CookBookRepository>();
}
// ou (v2) :
public static IConfigurationRoot Configuration;
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<ICookBookRepository, CookBookRepository>();
}
17. MICRO ORMS : DAPPER
CTOR:
private readonly IConfiguration _config;
public CookBookRepository(IConfiguration config)
{
_config = config;
}
public IDbConnection Connection
{
get
{
return new SqlConnection(_config.GetConnectionString("DbConnection"););
}
}
// ou (v2) :
public IDbConnection Connection
{
get
{
return new SqlConnection(Startup.Configuration["connectionStrings:DbConnection"]);
18. MICRO ORMS : DAPPER
REPO:
public IEnumerable<Book> GetAll()
{
using (IDbConnection db = Connection)
{
db.Open();
// version requete Sql
return db.Query<Book>("SELECT * FROM Book").ToList<Book>;
//return await db.QueryAsync<Book>("SELECT * FROM Book");
// version ps
//return db.Query<Book>("GetAllBooks", commandType: CommandType.StoredProcedure);
}
}
19. MICRO ORMS : DAPPER
public Book GetByID(int id)
{
using (IDbConnection db = Connection)
{
db.Open();
// version requete Sql
string sQuery = "SELECT * FROM Book WHERE Id = @Id";
return db.Query<Book>(sQuery, new { Id = id }).FirstOrDefault();
// version ps
//string sp = "GetBookByID";
//var parameter = new DynamicParameters();
//parameter.Add("@Id", id, dbType: DbType.Int32, direction: ParameterDirection.Input);
//return db.Query<Book>(sp, parameter, commandType: CommandType.StoredProcedure).FirstOrDefault();
}
}
20. MICRO ORMS : DAPPER
public Book GetMultiple(int id)
{
using (IDbConnection db = Connection)
{
db.Open();
// version requete Sql
//string query = "SELECT * FROM Book WHERE Id=@id; SELECT * FROM Recipe WHERE IdBook=@id;";
//var multipleResults = db.QueryMultiple(query, new { id = id });
// version sp
var parameter = new DynamicParameters();
parameter.Add("@id", id, dbType: DbType.Int32, direction: ParameterDirection.Input);
var multipleResults = db.QueryMultiple("GetBookRecipes", parameter,
commandType: CommandType.StoredProcedure);
var book = multipleResults.Read<Book>().SingleOrDefault();
var recipes = multipleResults.Read<Recipe>().ToList();
if (book != null && recipes != null)
book.Recipes = new List<Recipe>(); book.Recipes.AddRange(recipes);
return book;
}
21. MICRO ORMS : DAPPER
public void Add(Book book)
{
using (IDbConnection db = Connection)
{
db.Open();
var parameter = new DynamicParameters();
parameter.Add("@name", book.Name, dbType: DbType.String, direction: ParameterDirection.Input);
//string sQuery = "INSERT INTO Book (Name) VALUES(@Name)";
// version 1 requete Sql
//db.Execute(sQuery, parameter);
// version 2 requete Sql
//db.Execute(sQuery, book);
// version sp
db.Execute("InsertBook", parameter, commandType: CommandType.StoredProcedure);
}
}
22. MICRO ORMS : DAPPER
public void Update(Book book)
{
using (IDbConnection db = Connection)
{
db.Open();
var parameters = new DynamicParameters();
parameters.Add("@Id", book.Id, dbType: DbType.Int32, direction: ParameterDirection.Input);
parameters.Add("@Name", book.Name, dbType: DbType.String, direction: ParameterDirection.Input);
string sQuery = "UPDATE Book SET Name = @Name WHERE Id = @Id";
// version 1 requete Sql
//db.Execute(sQuery, parameters, commandType: CommandType.Text);
// version 2 requete Sql
//db.Execute(sQuery, book, commandType: CommandType.Text);
// version sp
db.Execute("UpdateBook", parameters, commandType: CommandType.StoredProcedure);
}
}
23. MICRO ORMS : DAPPER
public void Delete(int id)
{
using (IDbConnection db = Connection)
{
db.Open();
// version 1 requete Sql
//string sQuery = "DELETE FROM Book WHERE Id = @Id";
//db.Execute(sQuery, new { Id = id });
// version sp
var parameter = new DynamicParameters();
parameter.Add("@Id", id, dbType: DbType.Int32, direction: ParameterDirection.Input);
string sp = "DeleteBook";
db.Execute(sp, parameter, commandType: CommandType.StoredProcedure);
}
}
24. MICRO ORMS : PETAPOCO
POCO
DYNAMIC OBJECTS MAPPING
MULTIPLE RESULTS
STORED PROCEDURE
TRANSACTION
ASYNC
T4
PROVIDERS: SQL Server, SQL Server CE, MS Access, SQLite, MySQL, MariaDB, PostgreSQL, Firebird DB, and Oracle
APP.CONFIG
25. MICRO ORMS : PETAPOCO
CONFIG:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<ICookBookRepository, CookBookRepository>();
}
CTOR:
private readonly Database _db;
public CookBookRepository()
{
var connectionString = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
_db = new Database(connectionString, new SqlServerDatabaseProvider());
}
26. MICRO ORMS : PETAPOCO
REPO:
public IEnumerable<Book> GetAll()
{
using (IDatabase dbConnection = Connection)
{
return _db.Query<Book>("SELECT * FROM Book");
}
}
public Book GetByID(int id)
{
using (IDatabase dbConnection = Connection)
{
return _db.FirstOrDefault<Book>($"SELECT * FROM Book WHERE Id={id}");
}
}
27. MICRO ORMS : PETAPOCO
public void Add(Book book)
{
using (IDatabase dbConnection = Connection)
{
_db.Insert(book);
}
}
public void Update(Book book)
{
using (IDatabase dbConnection = Connection)
{
_db.Update(book);
}
}
public void Delete(Book book)
{
using (IDatabase dbConnection = Connection)
{
_db.Delete(book);
}
}
29. MICRO ORMS : MASSIVE
CONFIG:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<ICookBookRepository, CookBookRepository>();
}
public class Book : DynamicModel
{
public Book():base("DbConnection", "Book","Id") { }
public int Id { get; set; }
public string Name { get; set; }
}