這兩個Method,其他的就先維持預設的「throw new NotImplementedException();」,另外wiki中也示範另一個重點是「DataBag」的概念,他是用key、value的方式設定,可以於不同事件method中交換資料,感覺很像mvc網站中的ViewBag、ViewData。範例中是示範ReaderExecuting中設定好startTime參數、ReaderExecuted中讀出startTime參數,與現在時間做比較,算出sql執行時間並印出來。
services.AddScoped<ChloeORM.IDbContext>((serviceProvider) => { var dbContext = new ChloeORM.MySql.MySqlContext( new MySqlConnectionFactory(Configuration.GetConnectionString("ClassicModels"))); dbContext.Session.AddInterceptor(new DbCommandInterceptor()); return dbContext; });
publicCustomerService(IDbContext db) { _db = db; } public Customers GetCustomerById(int customerNumber) { // LIMIT部份不同資料庫會有不同的宣告 /* SELECT `Customers`.`CustomerNumber` AS `CustomerNumber`, `Customers`.`CustomerName` AS `CustomerName`, `Customers`.`ContactLastName` AS `ContactLastName`, `Customers`.`ContactFirstName` AS `ContactFirstName`, `Customers`.`Phone` AS `Phone`, `Customers`.`AddressLine1` AS `AddressLine1`, `Customers`.`AddressLine2` AS `AddressLine2`, `Customers`.`City` AS `City`, `Customers`.`State` AS `State`, `Customers`.`PostalCode` AS `PostalCode`, `Customers`.`Country` AS `Country`, `Customers`.`SalesRepEmployeeNumber` AS `SalesRepEmployeeNumber`, `Customers`.`CreditLimit` AS `CreditLimit` FROM `Customers` AS `Customers` WHERE `Customers`.`CustomerNumber` = ?P_0 LIMIT 0, 1 */ return _db.Query<Customers>() .FirstOrDefault(x => x.CustomerNumber == customerNumber); }
publicIEnumerable<Customers> GetCustomerListByLikeConditions(CustomerLikeConditions conditions) { /* SELECT `Customers`.`CustomerNumber` AS `CustomerNumber`, `Customers`.`CustomerName` AS `CustomerName`, `Customers`.`ContactLastName` AS `ContactLastName`, `Customers`.`ContactFirstName` AS `ContactFirstName`, `Customers`.`Phone` AS `Phone`, `Customers`.`AddressLine1` AS `AddressLine1`, `Customers`.`AddressLine2` AS `AddressLine2`, `Customers`.`City` AS `City`, `Customers`.`State` AS `State`, `Customers`.`PostalCode` AS `PostalCode`, `Customers`.`Country` AS `Country`, `Customers`.`SalesRepEmployeeNumber` AS `SalesRepEmployeeNumber`, `Customers`.`CreditLimit` AS `CreditLimit` FROM `Customers` AS `Customers` WHERE `Customers`.`CustomerName` LIKE CONCAT(?P_0, '%') */ return _db.Query<Customers>() .Where(x => x.CustomerName.StartsWith(conditions.CustomerName)) .ToList(); }
publicIEnumerable<Customers> GetCustomersListByInConditions(CustomerInConditions conditions) { /* SELECT `Customers`.`CustomerNumber` AS `CustomerNumber`, `Customers`.`CustomerName` AS `CustomerName`, `Customers`.`ContactLastName` AS `ContactLastName`, `Customers`.`ContactFirstName` AS `ContactFirstName`, `Customers`.`Phone` AS `Phone`, `Customers`.`AddressLine1` AS `AddressLine1`, `Customers`.`AddressLine2` AS `AddressLine2`, `Customers`.`City` AS `City`, `Customers`.`State` AS `State`, `Customers`.`PostalCode` AS `PostalCode`, `Customers`.`Country` AS `Country`, `Customers`.`SalesRepEmployeeNumber` AS `SalesRepEmployeeNumber`, `Customers`.`CreditLimit` AS `CreditLimit` FROM `Customers` AS `Customers` WHERE `Customers`.`CustomerName` IN (?P_0, ?P_1, ?P_2) */ return _db.Query<Customers>() .Where(x => conditions.CustomerNameList.Contains(x.CustomerName)) .ToList(); }
publicIEnumerable<Customers> GetCustomersListByPaginationConditions(CustomerPageConditions conditions) { /* SELECT `Customers`.`CustomerNumber` AS `CustomerNumber`, `Customers`.`CustomerName` AS `CustomerName`, `Customers`.`ContactLastName` AS `ContactLastName`, `Customers`.`ContactFirstName` AS `ContactFirstName`, `Customers`.`Phone` AS `Phone`, `Customers`.`AddressLine1` AS `AddressLine1`, `Customers`.`AddressLine2` AS `AddressLine2`, `Customers`.`City` AS `City`, `Customers`.`State` AS `State`, `Customers`.`PostalCode` AS `PostalCode`, `Customers`.`Country` AS `Country`, `Customers`.`SalesRepEmployeeNumber` AS `SalesRepEmployeeNumber`, `Customers`.`CreditLimit` AS `CreditLimit` FROM `Customers` AS `Customers` ORDER BY `Customers`.`CustomerName` ASC, `Customers`.`CustomerNumber` ASC LIMIT 0, 10 */ return _db.Query<Customers>() .OrderBy(x => x.CustomerName) .ThenBy(x => x.CustomerNumber) .TakePage(conditions.PageNumber, conditions.PageSize) .ToList(); }
publicList<string> GetCustomersCountryListByDistinct() { // SELECT DISTINCT `Customers`.`Country` AS `C` FROM `Customers` AS `Customers` return _db.Query<Customers>() .Select(x => x.Country) .Distinct() .ToList(); }
public IEnumerable<Customers> GetCustomersListByExists(CustomerExistConditions conditions) { /* SELECT `Customers`.`CustomerNumber` AS `CustomerNumber`, `Customers`.`CustomerName` AS `CustomerName`, `Customers`.`ContactLastName` AS `ContactLastName`, `Customers`.`ContactFirstName` AS `ContactFirstName`, `Customers`.`Phone` AS `Phone`, `Customers`.`AddressLine1` AS `AddressLine1`, `Customers`.`AddressLine2` AS `AddressLine2`, `Customers`.`City` AS `City`, `Customers`.`State` AS `State`, `Customers`.`PostalCode` AS `PostalCode`, `Customers`.`Country` AS `Country`, `Customers`.`SalesRepEmployeeNumber` AS `SalesRepEmployeeNumber`, `Customers`.`CreditLimit` AS `CreditLimit` FROM `Customers` AS `Customers` WHERE Exists ( SELECT ?P_0 AS `C` FROM `Customers` AS `Customers0` WHERE ( `Customers0`.`Country` = ?P_1 AND `Customers0`.`CustomerNumber` = `Customers`.`CustomerNumber` ) ) */ return _db.Query<Customers>() .Where(x => _db.Query<Customers>() .Where(c => c.Country == conditions.CountryName && c.CustomerNumber == x.CustomerNumber) .Any()) .ToList(); } }