Fluent NHibernate 4 :
- Native SQL;
- ;
- ;
- HQL (Hibernate Query Language).
.
Native SQL
Native SQL , SQL. .
, :
var list = session.CreateSQLQuery(
"SELECT Students.* FROM Students")
.AddEntity("Student", typeof(Student))
.List<Student>();
, CreateSQLQuery NHibernate. Native SQL , .
.
, .
:
ICriteria criteria1 = session.CreateCriteria(typeof(Student))
.Add(Expression.Eq("Sex", 'M'))
.SetMaxResults(10);
list = criteria1.List<Student>();
, , :
ICriteria criteria2 = session.CreateCriteria(typeof(Student))
.Add(Expression.Like("FirstName", "K%"))
.AddOrder(Order.Asc("LastName"))
.AddOrder(Order.Desc("Year"));
list = criteria2.List<Student>();
, 1993 1994 :
ICriteria criteria3 = session.CreateCriteria(typeof(Student))
.Add(Expression.Between("Year", 1993, 1994));
list = criteria3.List<Student>();
NHibernate in Action.
, , .
. . , .
//
Student s1 = new Student();
s.Sex = 'M';
s.Year = 1994;
//
Example example = Example.Create(s1)
.ExcludeZeroes()
.ExcludeProperty("Year")
.IgnoreCase()
.EnableLike();
//
ICriteria criteria5 = session.CreateCriteria(typeof(Student))
.Add(example);
list = criteria5.List<Student>();
|
|
HQL
HQL, .
HQL.
:
IQuery query = session.CreateQuery("from Student as stud where stud.FirstName =:Name");
query.SetAnsiString("Name", "Kristina");
list = query.List<Student>();
, KI-:
IQuery query1 = session.CreateQuery("select st.LastName from Student as st inner join st.Group as gr where gr.GroupName like 'KI-%'");
IList<String> list1 = query1.List<String>();
HQL NHibernate in Action.
, (, , ). ORM (object relation mapping) Fluent NHibernate. PostgreSQL. : , . , .
I -
1. PostgreSQL. university.
2. Windows Forms.
3. domain, mapping dao.
4. PostgreSQL, NHibernate Fluent NHibernate. :
postgreSQL | http://www.postgresql.org/ |
NHibernate | http://sourceforge.net/projects/nhibernate/ |
Fluent NHibernate | http://fluentnhibernate.org/ |
:
- Npgsql2.0.10-bin-ms.net.zip
- NHibernate-2.1.2.GA-bin.zip
- fluentnhibernate-1.1.zip
:
Npgsql.dll |
Mono.Security.dll |
NHibernate.dll |
Antlr3.Runtime.dll |
Iesi.Collections.dll |
log4net.dll |
Castle.DynamicProxy2.dll |
Castle.Core.dll |
NHibernate.ByteCode.Castle.dll |
FluentNHibernate.dll |
Npgsql.dll, NHibernate.dll, FluentNHibernate.dll NHibernate.ByteCode.Castle ( , , ).
DAO. NHibernate Fluent Hibernate , NHibernate 3.
II (DAO)
, .
5. domain - EntityBase, Group Student. :
namespace lab4.domain
{
//
public abstract class EntityBase
|
|
{
public virtual long Id { get; set; }
}
}
using System.Collections.Generic;
namespace lab4.domain
{
//
public class Group:EntityBase
{
private IList<Student> studentList = new List<Student>();
public virtual string GroupName { get; set; }
public virtual string CuratorName { get; set; }
public virtual string HeadmanName { get; set; }
public virtual IList<Student> StudentList
{
get { return studentList; }
set { studentList = value; }
}
}
}
namespace lab4.domain
{
//
public class Student:EntityBase
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual char Sex { get; set; }
public virtual int Year { get; set; }
public virtual Group Group { get; set; }
}
}
6. dao IGenericDAO, . . IGenericDAO :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lab4.dao
{
public interface IGenericDAO<T>
{
void SaveOrUpdate(T item);
T GetById(long id);
List<T> GetAll();
void Delete(T item);
}
}
7. , dao . , , - getAllStudentOfGroup ( ). , IGenericDAO. IGroupDAO IStudentDAO:
using lab4.domain;
using System.Collections.Generic;
namespace lab4.dao
{
public interface IGroupDAO:IGenericDAO<Group>
{
Group getGroupByName(string groupName);
IList<Student> getAllStudentOfGroup(string groupName);
void delGroupByName(string groupName);
}
}
using lab4.domain;
namespace lab4.dao
{
public interface IStudentDAO:IGenericDAO<Student>
{
Student getStudentByGroupFirstNameAndLastName(
string groupName, string firstName, string LastName);
}
}
8. dao , DAO :
namespace lab4.dao
{
abstract public class DAOFactory
{
public abstract IStudentDAO getStudentDAO();
public abstract IGroupDAO getGroupDAO();
}
}
9. IGenericDAO, IGroupDAO, IStudentDAO Fluent NHibernate. IGenericDAO:
using System;
using System.Collections.Generic;
using NHibernate;
namespace lab4.dao
{
public class GenericDAO<T>:IGenericDAO<T>
{
protected ISession session;
public GenericDAO() { }
public GenericDAO(ISession session)
{
this.session = session;
}
public void SaveOrUpdate(T item)
{
ITransaction transaction = session.BeginTransaction();
session.SaveOrUpdate(item);
transaction.Commit();
}
public T GetById(long id)
{
return session.Get<T>(id);
}
public List<T> GetAll()
{
return new List<T>(session.CreateCriteria(typeof(T)).List<T>());
}
public void Delete(T item)
{
ITransaction transaction = session.BeginTransaction();
session.Delete(item);
transaction.Commit();
}
}
}
10. dao IGroupDAO, IStudentDAO:
using lab4.domain;
|
|
using NHibernate;
using System.Collections.Generic;
using NHibernate.Criterion;
namespace lab4.dao
{
public class GroupDAO:GenericDAO<Group>, IGroupDAO
{
public GroupDAO(ISession session): base(session) { }
public Group getGroupByName(string groupName)
{
Group group = new Group();
group.GroupName = groupName;
ICriteria criteria = session.CreateCriteria(typeof(Group))
.Add(Example.Create(group));
IList<Group> list = criteria.List<Group>();
group = list[0];
return group;
}
public IList<Student> getAllStudentOfGroup(string groupName)
{
var list = session.CreateSQLQuery(
"SELECT Students.* FROM Students JOIN Groups" +
" ON Students.GroupId = Groups.Id" +
" WHERE Groups.GroupName='" + groupName + "'")
.AddEntity("Student", typeof(Student))
.List<Student>();
return list;
}
public void delGroupByName(string groupName)
{
Group group = getGroupByName(groupName);
Delete(group);
}
}
}
using lab4.domain;
using NHibernate;
namespace lab4.dao
{
public class StudentDAO:GenericDAO<Student>, IStudentDAO
{
public StudentDAO(ISession session): base(session) { }
public Student getStudentByGroupFirstNameAndLastName(
string groupName, string firstName, string lastName)
{
var list = session.CreateSQLQuery(
"SELECT Students.* FROM Students JOIN Groups" +
" ON Students.GroupId = Groups.Id" +
" WHERE Groups.GroupName='" + groupName + "'" +
" and Students.FirstName='" + firstName + "'" +
" and Students.LastName='" + lastName + "'")
.AddEntity("Student", typeof(Student))
.List<Student>();
Student student = list[0];
return student;
}
}
}
11. NHibernateDAOFactory:
using System;
using FluentNHibernate;
using NHibernate;
namespace lab4.dao
{
public class NHibernateDAOFactory:DAOFactory
{
/** NHibernate sessionFactory */
protected ISession session = null;
public NHibernateDAOFactory(ISession session)
{
this.session = session;
}
public override IStudentDAO getStudentDAO()
{
return new StudentDAO(session);
}
public override IGroupDAO getGroupDAO()
{
return new GroupDAO(session);
}
}
}
12. . c mapping- . NHibernate Fluent NHibernate XML-. Mapping- .
using FluentNHibernate.Mapping;
using lab4.domain;
namespace lab4.mapping
{
public class GroupMap:ClassMap<Group>
{
public GroupMap()
{
Table("Groups");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.GroupName);
Map(x => x.CuratorName);
Map(x => x.HeadmanName);
HasMany(x => x.StudentList)
.Inverse()
.Cascade.All()
.KeyColumn("GroupId");
}
}
}
using FluentNHibernate.Mapping;
using lab4.domain;
namespace lab4.mapping
{
public class StudentMap:ClassMap<Student>
{
public StudentMap()
{
Table("Students");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.Sex);
Map(x => x.Year);
References(x => x.Group, "GroupId");
|
|
}
}
}
.
III
Windows Forms ( ). ASP.NET .
13. . , - , .
14. MenuStrip, , , .
15. SplitConteiner , .
16. GoupBox. GoupBox Text , Text , GoupBox Dock Fill.
17. GoupBox DataGridView. Dock Fill. DataGridView , . DataGridView : ReadOnly True, MultiSelect False, RowHeadersVisible False, SelectionMode FullRowSelect, AllowUserToAddRows - False.
18. DataGridView , , , , , , . Columns. Name , .
19. ContextMenuStrip, DataGridView, DataGridView ContextMenuStrip. , , , .
20. . NHibernate. (Form2) TextBox, TextBox Label. TextBox , , , , . , , .
4.1.
4.1
21. (Form1), (ISession). Form2 CSharp. :
private ISessionFactory factory;
//
private ISession session;
//
private Form1 perent;
22. perent:
public void setPerent(Form1 perent)
{
this.perent = perent;
}
23. :
//
private ISession openSession(String host, int port, String database,
String user, String passwd)
{
ISession session = null;
//
Assembly mappingsAssemly = Assembly.GetExecutingAssembly();
if (factory == null)
{
//
factory = Fluently.Configure()
.Database(PostgreSQLConfiguration
.PostgreSQL82.ConnectionString(c => c
.Host(host)
.Port(5432)
.Database(database)
.Username(user)
.Password(passwd)))
.Mappings(m => m.FluentMappings
.AddFromAssembly(mappingsAssemly))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
//
session = factory.OpenSession();
return session;
}
//
private static void BuildSchema(Configuration config)
{
new SchemaExport(config).Create(false, true);
}
24. :
private void button1_Click(object sender, EventArgs e)
{
session = openSession(this.textBox1.Text,
Convert.ToInt32(this.textBox2.Text),
this.textBox3.Text, this.textBox4.Text, this.textBox5.Text);
//
this.Visible = false;
//
|
|
perent.setSession(session);
//
perent.fillDataGridView1();
}
25. . Form1 CSharp. :
private ISession session;
private Form2 form2 = null;
private Form3 form3 = null;
private Form4 form4 = null;
form3, form4 .
setSession(), :
public void setSession(ISession session)
{
this.session = session;
}
:
private Form2 getForm2()
{
if (form2 == null)
{
form2 = new Form2();
}
form2.setPerent(this);
return form2;
}
26. , :
private void ToolStripMenuItem_Click(
object sender, EventArgs e)
{
getForm2().Visible = true;
}
private void ToolStripMenuItem_Click(
object sender, EventArgs e)
{
session.Close();
dataGridView1.Rows.Clear();
dataGridView2.Rows.Clear();
}
27. DataGridView :
// dataGridView1
public void fillDataGridView1()
{
dataGridView1.Rows.Clear();
// NHibernate
DAOFactory dao = new NHibernateDAOFactory(session);
// dao
IGroupDAO groupDAO = dao.getGroupDAO();
//
List<Group> groupList = groupDAO.GetAll();
//
foreach (Group g in groupList)
{
DataGridViewRow row = new DataGridViewRow();
DataGridViewTextBoxCell cell1 = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell cell2 = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell cell3 = new DataGridViewTextBoxCell();
cell1.ValueType = typeof(string);
cell1.Value = g.GroupName;
cell2.ValueType = typeof(string);
cell2.Value = g.CuratorName;
cell3.ValueType = typeof(string);
cell3.Value = g.HeadmanName;
row.Cells.Add(cell1);
row.Cells.Add(cell2);
row.Cells.Add(cell3);
dataGridView1.Rows.Add(row);
}
}
// dataGridView2
public void fillDataGridView2(string key)
{
dataGridView2.Rows.Clear();
// NHibernate
DAOFactory dao = new NHibernateDAOFactory(session);
// dao
IGroupDAO groupDAO = dao.getGroupDAO();
//
IList<Student> studentList = groupDAO.getAllStudentOfGroup(key);
foreach (Student s in studentList)
{
DataGridViewRow row = new DataGridViewRow();
DataGridViewTextBoxCell cell1 = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell cell2 = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell cell3 = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell cell4 = new DataGridViewTextBoxCell();
cell1.ValueType = typeof(string);
cell1.Value = s.FirstName;
System.Console.WriteLine(s.FirstName + "\n");
cell2.ValueType = typeof(string);
cell2.Value = s.LastName;
cell3.ValueType = typeof(string);
cell3.Value = s.Sex;
cell4.ValueType = typeof(string);
cell4.Value = s.Year;
row.Cells.Add(cell1);
row.Cells.Add(cell2);
row.Cells.Add(cell3);
row.Cells.Add(cell4);
dataGridView2.Rows.Add(row);
}
}
28. , . Form3 , TextBox ( ). , , . Label. : . 4.2.
4.2
29. Form3 CSharp. :
private ISession session;
private Form1 perent;
private string key;
, , , . :
public void setPerent(Form1 perent)
{
this.perent = perent;
}
public void setSession(ISession session)
{
this.session = session;
}
public void setKey(string key)
{
this.key = key;
}
30. set-:
public void setTextBox1Text(string text)
{
this.textBox1.Text = text;
}
public void setTextBox2Text(string text)
{
this.textBox2.Text = text;
}
public void setTextBox3Text(string text)
{
this.textBox3.Text = text;
}
public void setButton1Visible(bool visible)
{
this.button1.Visible = visible;
}
public void setButton2Visible(bool visible)
{
this.button2.Visible = visible;
}
31. :
//
private void button1_Click(object sender, EventArgs e)
{
DAOFactory dao = new NHibernateDAOFactory(session);
IGroupDAO groupDAO = dao.getGroupDAO();
Group group = new Group();
group.GroupName = textBox1.Text;
group.HeadmanName = textBox2.Text;
group.CuratorName = textBox3.Text;
groupDAO.SaveOrUpdate(group);
perent.fillDataGridView1();
this.Visible = false;
}
//
private void button2_Click(object sender, EventArgs e)
{
DAOFactory dao = new NHibernateDAOFactory(session);
IGroupDAO groupDAO = dao.getGroupDAO();
Group group = groupDAO.getGroupByName(key);
group.GroupName = textBox1.Text;
group.CuratorName = textBox2.Text;
group.HeadmanName = textBox3.Text;
groupDAO.SaveOrUpdate(group);
this.Visible = false;
perent.fillDataGridView1();
}
32. Form1 CSharp. Form3 :
private Form3 getForm3()
{
if (form3 == null)
{
form3 = new Form3();
}
form3.setSession(session);
form3.setPerent(this);
return form3;
}
33. , , :
// ""
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
getForm3().Visible = true;
getForm3().setTextBox1Text("");
getForm3().setTextBox2Text("");
getForm3().setTextBox3Text("");
getForm3().setButton1Visible(true);
getForm3().setButton2Visible(false);
}
// ""
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
DAOFactory dao = new NHibernateDAOFactory(session);
IGroupDAO groupDAO = dao.getGroupDAO();
int selectedRow = dataGridView1.SelectedCells[0].RowIndex;
string key = (string)dataGridView1.Rows[selectedRow].Cells[0].Value;
DialogResult dr = MessageBox.Show(" ?", "",
MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
try
{
groupDAO.delGroupByName(key);
this.fillDataGridView1();
}
catch (Exception)
{
}
}
}
// ""
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
int selectedRow = dataGridView1.SelectedCells[0].RowIndex;
string key = (string)dataGridView1.Rows[selectedRow].Cells[0].Value;
DAOFactory dao = new NHibernateDAOFactory(session);
IGroupDAO groupDAO = dao.getGroupDAO();
Group group = groupDAO.getGroupByName(key);
getForm3().Visible = true;
getForm3().setKey(key);
getForm3().setTextBox1Text(group.GroupName);
getForm3().setTextBox2Text(group.CuratorName);
getForm3().setTextBox3Text(group.HeadmanName);
getForm3().setButton1Visible(false);
getForm3().setButton2Visible(true);
}
34. DataGridView DataGridView , DataGridView:
// DataGridView
private void dataGridView1_CellClick(
object sender, DataGridViewCellEventArgs e)
{
int selectedRow = dataGridView1.SelectedCells[0].RowIndex;
string key = (string)dataGridView1.Rows[selectedRow].Cells[0].Value;
fillDataGridView2(key);
}
35. , . Form4 . 4 TextBox ( ). , , , . Label. . 4.3
4.3
36. Form4 CSharp. :
private ISession session;
private Form1 perent;
private string key1;
private string key2;
private string key3;
key1 , key2 , key3 .
37. set- :
public void setSession(ISession session)
{
this.session = session;
}
public void setPerent(Form1 perent)
{
this.perent = perent;
}
public void setKey1(string key1)
{
this.key1 = key1;
}
public void setKey2(string key2)
{
this.key2 = key2;
}
public void setKey3(string key3)
{
this.key3 = key3;
}
38. set-:
public void setTextBox1Text(string text)
{
this.textBox1.Text = text;
}
public void setTextBox2Text(string text)
{
this.textBox2.Text = text;
}
public void setTextBox3Text(string text)
{
this.textBox3.Text = text;
}
public void setTextBox4Text(string text)
{
this.textBox4.Text = text;
}
public void setButton1Visible(bool visible)
{
this.button1.Visible = visible;
}
public void setButton2Visible(bool visible)
{
this.button2.Visible = visible;
}
39. :
// ""
private void button1_Click(object sender, EventArgs e)
{
DAOFactory dao = new NHibernateDAOFactory(session);
IGroupDAO groupDAO = dao.getGroupDAO();
Group group = groupDAO.getGroupByName(key1);
Student student = new Student();
student.FirstName = textBox1.Text;
student.LastName = textBox2.Text;
student.Sex = textBox3.Text[0];
student.Year = Int32.Parse(textBox4.Text);
group.StudentList.Add(student);
student.Group = group;
groupDAO.SaveOrUpdate(group);
perent.fillDataGridView2(key1);
this.Visible = false;
}
// ""
private void button2_Click(object sender, EventArgs e)
{
DAOFactory dao = new NHibernateDAOFactory(session);
IStudentDAO studentDAO = dao.getStudentDAO();
Student student = studentDAO
.getStudentByGroupFirstNameAndLastName(key1, key2, key3);
student.FirstName = textBox1.Text;
student.LastName = textBox2.Text;
student.Sex = textBox3.Text[0];
student.Year = Int32.Parse(textBox4.Text);
studentDAO.SaveOrUpdate(student);
this.Visible = false;
perent.fillDataGridView2(key1);
}
40. Form1 CSharp. Form4 :
private Form4 getForm4()
{
if (form4 == null)
{
form4 = new Form4();
}
form4.setSession(session);
form4.setPerent(this);
return form4;
}
41. , , :
// ""
private void ToolStripMenuItem1_Click(object sender, EventArgs e)
{
int selectedRow = dataGridView1.SelectedCells[0].RowIndex;
string key = (string)dataGridView1.Rows[selectedRow].Cells[0].Value;
getForm4().Visible = true;
getForm4().setButton1Visible(true);
getForm4().setButton2Visible(false);
getForm4().setTextBox1Text("");
getForm4().setTextBox2Text("");
getForm4().setTextBox3Text("");
getForm4().setTextBox4Text("");
getForm4().setKey1(key);
}
// ""
private void ToolStripMenuItem1_Click(object sender, EventArgs e)
{
int selectedRow = dataGridView1.SelectedCells[0].RowIndex;
string key1 = (string)dataGridView1.Rows[selectedRow].Cells[0].Value;
selectedRow = dataGridView2.SelectedCells[0].RowIndex;
string key2 = (string)dataGridView2.Rows[selectedRow].Cells[0].Value;
selectedRow = dataGridView2.SelectedCells[0].RowIndex;
string key3 = (string)dataGridView2.Rows[selectedRow].Cells[1].Value;
DAOFactory dao = new NHibernateDAOFactory(session);
IStudentDAO studentDAO = dao.getStudentDAO();
Student student = studentDAO
.getStudentByGroupFirstNameAndLastName(key1, key2, key3);
student.Group.StudentList.Remove(student);
studentDAO.Delete(student);
fillDataGridView2(key1);
}
// ""
private void ToolStripMenuItem1_Click(object sender, EventArgs e)
{
int selectedRow = dataGridView1.SelectedCells[0].RowIndex;
string key1 = (string)dataGridView1.Rows[selectedRow].Cells[0].Value;
selectedRow = dataGridView2.SelectedCells[0].RowIndex;
string key2 = (string)dataGridView2.Rows[selectedRow].Cells[0].Value;
selectedRow = dataGridView2.SelectedCells[0].RowIndex;
string key3 = (string)dataGridView2.Rows[selectedRow].Cells[1].Value;
DAOFactory dao = new NHibernateDAOFactory(session);
IStudentDAO studentDAO = dao.getStudentDAO();
Student student = studentDAO
.getStudentByGroupFirstNameAndLastName(key1, key2, key3);
getForm4().Visible = true;
getForm4().setTextBox1Text(student.FirstName);
getForm4().setTextBox2Text(student.LastName);
getForm4().setTextBox3Text(student.Sex.ToString());
getForm4().setTextBox4Text(student.Year.ToString());
getForm4().setKey1(key1);
getForm4().setKey2(key2);
getForm4().setKey3(key3);
getForm4().setButton1Visible(false);
getForm4().setButton2Visible(true);
}
42. :
private void Form1_Resize(object sender, EventArgs e)
{
this.dataGridView1.Columns["Column1"].Width =
(this.dataGridView1.Width / 3) - 1;
this.dataGridView1.Columns["Column2"].Width =
(this.dataGridView1.Width / 3) - 1;
this.dataGridView1.Columns["Column3"].Width =
(this.dataGridView1.Width / 3) - 1;
this.dataGridView2.Columns["Column4"].Width =
System.Convert.ToInt32(this.dataGridView2.Width * 0.3) - 1;
this.dataGridView2.Columns["Column5"].Width =
System.Convert.ToInt32(this.dataGridView2.Width * 0.3) - 1;
this.dataGridView2.Columns["Column6"].Width =
System.Convert.ToInt32(this.dataGridView2.Width * 0.1) - 1;
this.dataGridView2.Columns["Column7"].Width =
System.Convert.ToInt32(this.dataGridView2.Width * 0.3) - 1;
}
4.4.
4.4
, , . , , 4.1. .
4.1
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) |
- ;
- ;
- ;
- ;
- ( , );
- .
1. ORM?
2. NHibernate Fluent NHibernate?
3. Fluent NHibernate .
4. NHibernate?
5. Fluent NHibernate?
6. HasOne. .
7. HasMany. .
8. HasManyToMany. .
5 5
ASP.NET
ASP.NET, ASP.NET.