87 lines
2.3 KiB
C#
87 lines
2.3 KiB
C#
|
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();
|
||
|
}
|
||
|
}
|
||
|
}
|