first commit
This commit is contained in:
87
ConsoleApp1/Repository/PersonRepository.cs
Normal file
87
ConsoleApp1/Repository/PersonRepository.cs
Normal file
@ -0,0 +1,87 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
92
ConsoleApp1/Repository/PersonRepositoryTest.cs
Normal file
92
ConsoleApp1/Repository/PersonRepositoryTest.cs
Normal file
@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using ConsoleApp1.Model;
|
||||
using ConsoleApp1.ORM;
|
||||
using NHibernate.Tool.hbm2ddl;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace ConsoleApp1.Repository
|
||||
{
|
||||
[TestFixture]
|
||||
public class NHibernatePersonRepositoryTest
|
||||
{
|
||||
private PersonRepository _personRepo;
|
||||
|
||||
[SetUp]
|
||||
public void CreateSchema()
|
||||
{
|
||||
DeleteDatabaseIfExists();
|
||||
|
||||
var schemaUpdate = new SchemaUpdate(NHibernateHelper.Configuration);
|
||||
schemaUpdate.Execute(false, true);
|
||||
|
||||
_personRepo = new PersonRepositoryNHibernate();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanSavePerson()
|
||||
{
|
||||
_personRepo.Save(new Person());
|
||||
Assert.AreEqual(1, _personRepo.RowCount());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanGetPerson()
|
||||
{
|
||||
var person = new Person();
|
||||
_personRepo.Save(person);
|
||||
Assert.AreEqual(1, _personRepo.RowCount());
|
||||
|
||||
person = _personRepo.Get(person.Id);
|
||||
Assert.IsNotNull(person);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanUpdatePerson()
|
||||
{
|
||||
var person = new Person();
|
||||
_personRepo.Save(person);
|
||||
Assert.AreEqual(1, _personRepo.RowCount());
|
||||
|
||||
person = _personRepo.Get(person.Id);
|
||||
person.FirstName = "Test";
|
||||
_personRepo.Update(person);
|
||||
|
||||
Assert.AreEqual(1, _personRepo.RowCount());
|
||||
Assert.AreEqual("Test", _personRepo.Get(person.Id).FirstName);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanDeletePerson()
|
||||
{
|
||||
var person = new Person();
|
||||
_personRepo.Save(person);
|
||||
Assert.AreEqual(1, _personRepo.RowCount());
|
||||
|
||||
_personRepo.Delete(person);
|
||||
Assert.AreEqual(0, _personRepo.RowCount());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestBulkInsert()
|
||||
{
|
||||
static IEnumerable<Person> GeneratePersons(int num = 3)
|
||||
{
|
||||
var ret = new Person[num];
|
||||
for (var ii = 0; ii < num; ii++) ret[ii] = new Person {Id = Guid.NewGuid(), FirstName = "bac", LastName = "fif"};
|
||||
return ret;
|
||||
}
|
||||
|
||||
_personRepo.Save(GeneratePersons(10));
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void DeleteDatabaseIfExists()
|
||||
{
|
||||
if (File.Exists("test.db"))
|
||||
File.Delete("test.db");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user