This repository has been archived on 2020-06-09. You can view files and clone it, but cannot push or open issues or pull requests.
dotnet-nhibernate-test/ConsoleApp1/Repository/PersonRepository.cs

87 lines
2.3 KiB
C#
Raw Normal View History

2020-06-08 22:12:01 +00:00
using System;
using System.Collections.Generic;
using ConsoleApp1.Model;
using ConsoleApp1.ORM;
namespace ConsoleApp1.Repository
{
public interface PersonRepository
{
/// <summary>
/// Get person entity by id
/// </summary>
/// <param name="id">id</param>
/// <returns>person</returns>
Person Get(Guid id);
/// <summary>
/// Save person entity
/// </summary>
/// <param name="person">person</param>
void Save(Person person);
/// <summary>
/// Update person entity
/// </summary>
/// <param name="person">person</param>
void Update(Person person);
/// <summary>
/// Delete person entity
/// </summary>
/// <param name="person">person</param>
void Delete(Person person);
/// <summary>
/// Row count person in db
/// </summary>
/// <returns>number of rows</returns>
long RowCount();
void Save(IEnumerable<Person> persons);
}
public class PersonRepositoryNHibernate : PersonRepository
{
public void Save(IEnumerable<Person> persons)
{
BulkQuery.BulkInsert(persons);
}
public void Save(Person person)
{
using var session = NHibernateHelper.OpenSession();
using var transaction = session.BeginTransaction();
session.Save(person);
transaction.Commit();
}
public Person Get(Guid id)
{
using var session = NHibernateHelper.OpenSession();
return session.Get<Person>(id);
}
public void Update(Person person)
{
using var session = NHibernateHelper.OpenSession();
using var transaction = session.BeginTransaction();
session.Update(person);
transaction.Commit();
}
public void Delete(Person person)
{
using var session = NHibernateHelper.OpenSession();
using var transaction = session.BeginTransaction();
session.Delete(person);
transaction.Commit();
}
public long RowCount()
{
using var session = NHibernateHelper.OpenSession();
return session.QueryOver<Person>().RowCountInt64();
}
}
}