Jako kod bazowy użyjemy obiekt encji tabeli:
Code:
klasa którą chcemy uzyskać ma następującą strukturę:
Code:
Kod mapowania:
Code:
użycie sprowadza się do wywołania metody Map na obiekcie bazowym:
Code:
Blog o tematyce związanej z platformą .NET i szeroko pojętym programowaniem. Znajdziesz tu informacje nt. platformy .NET, języka C#, wiadomości o wzorcach projektowych oraz tworzeniu poprawnej architektury oprogramowania.
public class PersonEntity { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public string Address { get; set; } }
public class PersonView { public string FullName { get; set; } public string Address { get; set; } }
public static class Mappers { public static PersonView Map(this PersonEntity personEntity) { return new PersonView { FullName = personEntity.FirstName + " " + personEntity.LastName, Address = personEntity.Address }; } }
var personEntity = new PersonEntity { Id = 1, Address = "Andrychowska 34, 10-124 Andrychów", Age = 31, FirstName = "Jan", LastName = "Kowalski" }; var pv = personEntity.Map();
$(document).ready(function() { });
static void Main(string[] args) { var test1Context = new test1Context(); var single = test1Context.Pojazds.Single(x => x.Id == 100); var first = test1Context.Pojazds.First(x => x.Id == 100); }
SELECT TOP (2) [Extent1].[Id] AS [Id], [Extent1].[Nazwa] AS [Nazwa]FROM [dbo].[Pojazd] AS [Extent1]WHERE 100 = [Extent1].[Id]
SELECT TOP (1) [Extent1].[Id] AS [Id], [Extent1].[Nazwa] AS [Nazwa]FROM [dbo].[Pojazd] AS [Extent1]WHERE 100 = [Extent1].[Id]
static void Main(string[] args) { var test1Context = new test1Context(); test1Context.Configuration.AutoDetectChangesEnabled = false; test1Context.Configuration.ProxyCreationEnabled = false; for (int i = 0; i < 1000; i++) { var pojazd = new Pojazd {Nazwa = Guid.NewGuid().ToString()}; test1Context.Pojazds.Add(pojazd); } test1Context.SaveChanges(); }
EXEC sp_executesql N'insert [dbo].[Pojazd]([Nazwa])
values (@0)
select [Id]
from [dbo].[Pojazd]
where @@ROWCOUNT > 0 and [Id] = scope_identity()',N'@0 nchar(100)',@0=N'6dd1fb44-61d3-4fce-8ef8-4f0d61492df2
'
SELECT p.Nazwa, COUNT(1) AS 'Ilosc kól'
FROM Pojazd p JOIN Kolo k ON p.Id = k.IdPojazd
GROUP BY p.Nazwa
SELECT p.Nazwa, k.IloscFROM Pojazd p JOIN (SELECT IdPojazd, COUNT(1) 'Ilosc' FROM Kolo GROUP BY IdPojazd) AS k ON p.Id = k.IdPojazd
(8) SELECT (9) TOP
(1) FROM
(3) JOIN
(2) ON
(4) WHERE
(5) GROUP BY
(6) WITH
(7) HAVING
(1) ORDER BY
public class ClientModel { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int BirthYear { get; set; } public ClientModel(int id, string firstName, string lastName, int birthYear) { Id = id; FirstName = firstName; LastName = lastName; BirthYear = birthYear; } public ClientModel() { } public override string ToString() { return string.Format("Id: {0}, FirstName: {1}, LastName: {2}, BirthYear: {3}", Id, FirstName, LastName, BirthYear); } }
public class HomeController : Controller { public List<ClientModel> Clients { get { return new List<ClientModel> { new ClientModel(1, "Jacek", "Markowski", 2000), new ClientModel(2, "Marek", "Polkowski", 1988), new ClientModel(3, "Sebastian", "Dunkowski", 1900) }; } } public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; return View(); } public ActionResult About() { return View(); } public JsonResult GetPersonList() { return Json(Clients, JsonRequestBehavior.AllowGet); } public JsonResult GetPersonById(int id) { var client = Clients.Single(x => x.Id == id); return Json(client, JsonRequestBehavior.AllowGet); } }
@{ ViewBag.Title = "Home Page"; } <h2>@ViewBag.Message</h2> <p> <div id="PersonList"> </div> <input type="button" value="Pobierz dane" id="przycisk1"/> </p>
@{ ViewBag.Title = "Home Page"; } <h2>@ViewBag.Message</h2> <p> <div id="PersonList"> </div> <input type="button" value="Pobierz dane" id="przycisk1" /> <script type="text/javascript"> $(document).ready(function () { $("#przycisk1").click(function () { $.ajax({ url: '@Url.Action("GetPersonList")', cache: false, dataType: "JSON", success: function (data) { $.each(data, function (i, item) { $("#PersonList").append(item.Id + " " + item.FirstName + " " + item.LastName + "</br>"); }); } }); return false; }); }); </script> </p>
@{ ViewBag.Title = "Home Page"; } <h2>@ViewBag.Message</h2> <p> <div id="PersonList"> </div> <form method="post" name="formularz"> <label>Id: </label> <input type="text" name="id" id="id"/> <input type="button" value="Pobierz dane" id="przycisk1" name="przycisk1" /> </form> <script type="text/javascript"> $(document).ready(function() { $("#przycisk1").click(function() { $.ajax({ url: '@Url.Action("GetPersonById")', cache: false, dataType: "JSON", data: $("form[name=formularz]").serialize(), success: function(data) { $("#PersonList").append(data.Id + " " + data.FirstName + " " + data.LastName + "</br>"); } }); return false; }); }); </script> </p>
private static void CreateDatabase() { SQLiteConnection.CreateFile("database.sqlite"); using (var connection = GetSqLiteConnection()) { using(var cmd = connection.CreateCommand()) { cmd.CommandText = @"CREATE TABLE Table1 ( Id INTEGER PRIMARY KEY AUTOINCREMENT, Name1 VARCHAR, Name2 VARCHAR, Name3 VARCHAR, Name4 VARCHAR);"; cmd.ExecuteNonQuery(); } } }
private static long NormalInsert(List<Table1> data, SQLiteConnection connection) { var stopwatch = new Stopwatch(); stopwatch.Start(); using (var cmd = connection.CreateCommand()) { cmd.CommandText = "INSERT INTO Table1(Name1,Name2,Name3,Name4) VALUES(@Name1,@Name2,@Name3,@Name4);"; cmd.Parameters.AddWithValue("@Name1", ""); cmd.Parameters.AddWithValue("@Name2", ""); cmd.Parameters.AddWithValue("@Name3", ""); cmd.Parameters.AddWithValue("@Name4", ""); for (int i = 0; i < data.Count; i++) { cmd.Parameters["@Name1"].Value = data[i].Name1; cmd.Parameters["@Name2"].Value = data[i].Name2; cmd.Parameters["@Name3"].Value = data[i].Name3; cmd.Parameters["@Name4"].Value = data[i].Name4; cmd.ExecuteNonQuery(); } } stopwatch.Stop(); return stopwatch.ElapsedMilliseconds; }
private static long TransactionInsert(List<Table1> data, SQLiteConnection connection) { var stopwatch = new Stopwatch(); stopwatch.Start(); using (var cmd = connection.CreateCommand()) { cmd.Transaction = connection.BeginTransaction(); cmd.CommandText = "INSERT INTO Table1(Name1,Name2,Name3,Name4) VALUES(@Name1,@Name2,@Name3,@Name4);"; cmd.Parameters.AddWithValue("@Name1", ""); cmd.Parameters.AddWithValue("@Name2", ""); cmd.Parameters.AddWithValue("@Name3", ""); cmd.Parameters.AddWithValue("@Name4", ""); for (int i = 0; i < data.Count; i++) { cmd.Parameters["@Name1"].Value = data[i].Name1; cmd.Parameters["@Name2"].Value = data[i].Name2; cmd.Parameters["@Name3"].Value = data[i].Name3; cmd.Parameters["@Name4"].Value = data[i].Name4; cmd.ExecuteNonQuery(); } cmd.Transaction.Commit(); } stopwatch.Stop(); return stopwatch.ElapsedMilliseconds; }
private static long TransactionInsertWithPrepare(List<Table1> data, SQLiteConnection connection) { var stopwatch = new Stopwatch(); stopwatch.Start(); using (var cmd = connection.CreateCommand()) { cmd.Transaction = connection.BeginTransaction(); cmd.CommandText = "INSERT INTO Table1(Name1,Name2,Name3,Name4) VALUES(@Name1,@Name2,@Name3,@Name4);"; cmd.Parameters.AddWithValue("@Name1", ""); cmd.Parameters.AddWithValue("@Name2", ""); cmd.Parameters.AddWithValue("@Name3", ""); cmd.Parameters.AddWithValue("@Name4", ""); cmd.Prepare(); for (int i = 0; i < data.Count; i++) { cmd.Parameters["@Name1"].Value = data[i].Name1; cmd.Parameters["@Name2"].Value = data[i].Name2; cmd.Parameters["@Name3"].Value = data[i].Name3; cmd.Parameters["@Name4"].Value = data[i].Name4; cmd.ExecuteNonQuery(); } cmd.Transaction.Commit(); } stopwatch.Stop(); return stopwatch.ElapsedMilliseconds; }
private static long TransactionPrepareSynchronousOff(List<Table1> data, SQLiteConnection connection) { var stopwatch = new Stopwatch(); stopwatch.Start(); using (var cmd = connection.CreateCommand()) { cmd.CommandText = "PRAGMA synchronous = OFF"; cmd.ExecuteNonQuery(); cmd.Transaction = connection.BeginTransaction(); cmd.CommandText = "INSERT INTO Table1(Name1,Name2,Name3,Name4) VALUES(@Name1,@Name2,@Name3,@Name4);"; cmd.Parameters.AddWithValue("@Name1", ""); cmd.Parameters.AddWithValue("@Name2", ""); cmd.Parameters.AddWithValue("@Name3", ""); cmd.Parameters.AddWithValue("@Name4", ""); for (int i = 0; i < data.Count; i++) { cmd.Parameters["@Name1"].Value = data[i].Name1; cmd.Parameters["@Name2"].Value = data[i].Name2; cmd.Parameters["@Name3"].Value = data[i].Name3; cmd.Parameters["@Name4"].Value = data[i].Name4; cmd.ExecuteNonQuery(); } cmd.Transaction.Commit(); } stopwatch.Stop(); return stopwatch.ElapsedMilliseconds; }
CREATE TABLE Table2(
Id serial NOT NULL,
Name1 CHARacter VARYING(60),
Name3 CHARacter VARYING(60),
Name2 CHARacter VARYING(60),
Name4 CHARacter VARYING(60),
CONSTRAINT "Table2_pkey"PRIMARY KEY (Id)
);
public class Table1 { public int Id { get; set; } public string Name1 { get; set; } public string Name2 { get; set; } public string Name3 { get; set; } public string Name4 { get; set; } public Table1(string name1, string name2, string name3, string name4) { Name1 = name1; Name2 = name2; Name3 = name3; Name4 = name4; } }
public class Repository { public static List<Table1> GetData(int rowCount) { var data = new List<Table1>(); for (int i = 0; i < rowCount; i++) { data.Add(new Table1(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString())); } return data; } }
private static long InsertAsSingleRows(List<Table1> data, NpgsqlConnection connection) { var stopwatch = new Stopwatch(); stopwatch.Start(); using (var cmd = connection.CreateCommand()) { cmd.Parameters.AddWithValue(Name1, ""); cmd.Parameters.AddWithValue(Name2, ""); cmd.Parameters.AddWithValue(Name3, ""); cmd.Parameters.AddWithValue(Name4, ""); cmd.CommandText = "INSERT INTO Table2(Name1,Name2,Name3,name4) VALUES(@Name1,@Name2,@Name3,@Name4)"; for (int i = 0; i < data.Count; i++) { cmd.Parameters[Name1].Value = data[i].Name1; cmd.Parameters[Name2].Value = data[i].Name2; cmd.Parameters[Name3].Value = data[i].Name3; cmd.Parameters[Name4].Value = data[i].Name4; cmd.ExecuteNonQuery(); } } stopwatch.Stop(); return stopwatch.ElapsedMilliseconds; }
private static long InsertAsTransaction(List<Table1> data, NpgsqlConnection connection) { var stopwatch = new Stopwatch(); stopwatch.Start(); using (var cmd = connection.CreateCommand()) { cmd.Transaction = connection.BeginTransaction(); cmd.Parameters.AddWithValue(Name1, ""); cmd.Parameters.AddWithValue(Name2, ""); cmd.Parameters.AddWithValue(Name3, ""); cmd.Parameters.AddWithValue(Name4, ""); cmd.CommandText = "INSERT INTO Table2(Name1,Name2,Name3,name4) VALUES(@Name1,@Name2,@Name3,@Name4)"; for (int i = 0; i < data.Count; i++) { cmd.Parameters[Name1].Value = data[i].Name1; cmd.Parameters[Name2].Value = data[i].Name2; cmd.Parameters[Name3].Value = data[i].Name3; cmd.Parameters[Name4].Value = data[i].Name4; cmd.ExecuteNonQuery(); } cmd.Transaction.Commit(); } stopwatch.Stop(); return stopwatch.ElapsedMilliseconds; }
private static long InsertAsBatch(List<Table1> data, NpgsqlConnection connection) { var stopwatch = new Stopwatch(); stopwatch.Start(); using (var cmd = connection.CreateCommand()) { var dataAsFile = new StringBuilder(); for (int i = 0; i < data.Count; i++) { dataAsFile.AppendFormat("{0}\t{1}\t{2}\t{3}\n", data[i].Name1, data[i].Name2, data[i].Name3, data[i].Name4); } var memoryStream = new MemoryStream(new UTF8Encoding().GetBytes(dataAsFile.ToString())); cmd.CommandText = "COPY Table2(Name1,Name2,Name3,name4) FROM STDIN"; var npgsqlCopyIn = new NpgsqlCopyIn(cmd, connection, memoryStream); npgsqlCopyIn.Start(); stopwatch.Stop(); return stopwatch.ElapsedMilliseconds; } }