1 / 10

NHibernate что, где, когда

NHibernate что, где, когда. Артур Дробинский ЗетаСофт Томск, 2012. Способы работы с БД из .NET. ADO.Net – DataSet / DataReader Linq2SQL ORM. Object-Relational Mapper. public class BaseEntity { public virtual int Id { get ; set ; } } public class User : BaseEntity {

liesel
Download Presentation

NHibernate что, где, когда

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NHibernateчто, где, когда АртурДробинский ЗетаСофт Томск, 2012

  2. Способы работы с БДиз .NET • ADO.Net – DataSet/DataReader • Linq2SQL • ORM

  3. Object-Relational Mapper publicclassBaseEntity { publicvirtualint Id { get; set; } } publicclassUser : BaseEntity { publicvirtualstring Login { get; set; } publicvirtualstring FirstName { get; set; } publicvirtualstring LastName { get; set; } } publicclassTopic : BaseEntity { publicvirtualstring Title { get; set; } publicvirtualUser Creator { get; set; } publicvirtualIList<Message> Messages { get; set; } } publicclassMessage : BaseEntity { publicvirtualTopic Topic { get; set; } publicvirtualstring Text { get; set; } publicvirtualDateTime DateTime { get; set; } publicvirtualUser Author { get; set; } }

  4. Object-Relational Mapper var connection = SessionConstructor.OpenConnection(); var command = connection.CreateCommand(); command.CommandText = @"SELECT TOP 5 * FROM [Message] INNER JOIN [User] Author ON Author.Id = [Message].Author_Id WHERE Author.Login='Shaddix' ORDER BY [Message].[DateTime] DESC"; var reader = command.ExecuteReader(); var result = newList<object>(); while (reader.Read()) { result.Add(new { Text = reader["Text"], Id = reader["Id"] }); } var session = SessionConstructor.OpenSession(); var data = session.Query<Message>() .Where(x => x.Author.Login == "Shaddix") .OrderByDescending(x => x.DateTime) .Take(5) .ToList();

  5. Плюсы ORM • Работа с классами удобнее, чем с DataSet • Проектирование на уровне классов • Отсутствие «переключений контекста» • Строгая типизация при работе с БД • Удобные инструменты работы (профилирование, кэширование, миграции, etc) • Использование знаний в LINQ вместо изучения SQL • Ускорение разработки

  6. Существующие ORM • Микро-ОРМ • Dapper.NET • Massive • PetaPoco • Полнофункциональные ORM • NHibernate • Entity Framework • LLBLGen + маленькие + базовый ORM-функционал + простые, старый-добрыйSQL + linq-запросы + БД-независимость + сложные сценарии маппинга (наследование/компоненты/связи) + кэширование/оптимизация запросов

  7. Подходы при работе с ORM • Database-First • Code-First

  8. Маппинги в NHibernate publicclassBaseEntity { publicvirtualint Id { get; protected set;} } publicclassUser : BaseEntity { publicvirtualstring Login { get; set; } publicvirtualstring FirstName { get; set; } publicvirtualstring LastName { get; set; } } • NHibernateи *.hbm.xml • FluentNHibernateи отсутствие всех этих ужастиков (Convention over Configuration) • Здесь мы смотрим примеры

  9. Вопросы?! Артур Дробинский ЗетаСофт, Томск http://arturdr.ru artur.drobinskiy@gmail.com

  10. Всем спасибо за вниманиеУспехов! Артур Дробинский ЗетаСофт, Томск http://arturdr.ru artur.drobinskiy@gmail.com

More Related