Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.
Customized mappings are achieved through ClassMapper.
Important: This library is a separate effort from Dapper.Contrib (a sub-system of the Dapper project).
For more information, please view our Getting Started guide.
http://nuget.org/List/Packages/DapperExtensions
PM> Install-Package DapperExtensions
The following examples will use a Person POCO defined as:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Active { get; set; }
public DateTime DateCreated { get; set; }
}
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
int personId = 1;
Person person = cn.Get<Person>(personId);
cn.Close();
}
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
Person person = new Person { FirstName = "Foo", LastName = "Bar" };
int id = cn.Insert(person);
cn.Close();
}
public class Car
{
public int ModelId { get; set; }
public int Year { get; set; }
public string Color { get; set; }
}
...
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
Car car = new Car { Color = "Red" };
var multiKey = cn.Insert(car);
cn.Close();
int modelId = multiKey.ModelId;
int year = multiKey.Year;
}
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
int personId = 1;
Person person = cn.Get<Person>(personId);
person.LastName = "Baz";
cn.Update(person);
cn.Close();
}
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
Person person = cn.Get<Person>(1);
cn.Delete(person);
cn.Close();
}
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
var predicate = Predicates.Field<Person>(f => f.Active, Operator.Eq, true);
IEnumerable<Person> list = cn.GetList<Person>(predicate);
cn.Close();
}
Generated SQL
SELECT
[Person].[Id]
, [Person].[FirstName]
, [Person].[LastName]
, [Person].[Active]
, [Person].[DateCreated]
FROM [Person]
WHERE ([Person].[Active] = @Active_0)
More information on predicates can be found in our wiki.
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
var predicate = Predicates.Field<Person>(f => f.DateCreated, Operator.Lt, DateTime.UtcNow.AddDays(-5));
int count = cn.Count<Person>(predicate);
cn.Close();
}
Generated SQL
SELECT
COUNT(*) Total
FROM [Person]
WHERE ([Person].[DateCreated] < @DateCreated_0)
More information on predicates can be found in our wiki.
Copyright 2011 Thad Smith, Page Brooks and contributors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。