Basic additions and deletions-nhibernate entry to proficient series 3

Source: Internet
Author: User
Tags assert

From today onwards, we will enter the NHibernate development study, first of all to some nhibernate add and remove changes to check the operation, these several learned, the basis of the dozen.
Have the time also can study the domestic cyq. The data frame is also a fairly good framework.

Below we follow the following steps:

1. Entity classes and XML mappings

2. The NHibernate tool generates the corresponding table structure

3. Write database Access Object DAO

4. Unit test to add, delete, this, check the method


A: New project, the following figure:

  


Two: Writing entity classes:

public class Product
{
Public virtual Guid ID {get; set;}
Public virtual string Code {get; set;}
Public virtual string Name {get; set;}
Public virtual string QuantityPerUnit {get; set;}
Public virtual string Unit {get; set;}
Public virtual decimal Sellprice {get; set;}
Public virtual decimal Buyprice {get; set;}
Public virtual string Remark {get; set;}
}
Three: Map to xml:

<?xml version= "1.0" encoding= "Utf-8"?>

<class name= "Product" table= "T_product" lazy= "true" >
<id name= "id" column= "id" type= "Guid" >
<generator class= "Assigned"/>
</id>

<property name= "Code" type= "string" >
<column name= "Code" length= "Wuyi"/>
</property>

<property name= "Name" type= "string" >
<column name= "name" length= "/>"
</property>

<property name= "QuantityPerUnit" type= "string" >
<column name= "QuantityPerUnit" length= "/>"
</property>

<property name= "unit" type= "string" >
<column name= "unit" length= "Si"/>
</property>


<property name= "Sellprice" type= "decimal" >
<column name= "Sellprice" precision= "scale=" "2"/>
</property>

<property name= "Buyprice" type= "decimal" >
<column name= "Buyprice" precision= "scale=" "2"/>
</property>

<property name= "Remark" type= "string" >
<column name= "Remark" length= "191"/>
</property>

</class>

Four: Embed XML as a resource, as shown in the following figure:


Five: Set up a Web test project, as shown below:


Six: Reference the relevant assembly, as shown in the following figure:

 

 

Seven: Copy and paste the NHibernate configuration template into the project and modify the file's properties to always copy, as shown in the following figure:




Viii. preparing to initialize the database table structure [NHibernateInit.cs]

[Testfixture]
public class Nhibernateinit
{
[Test]
public void Initt ()
{
var cfg = new NHibernate.Cfg.Configuration (). Configure ("Config/hibernate.cfg.xml");
using (isessionfactory sessionfactory = cfg. Buildsessionfactory ()) {}
}
}

Nine: LinFu.DynamicProxy.dll "and" NHibernate.ByteCode.LinFu.dll "copy to the project and modify the build mode, as shown in the following figure:


    

X: Debug and start external programs, as shown below:


11: Create a new database, as shown below:





11: Navigate to the "NHibernateTest.dll" assembly, start the unit test, as shown in the following figure:


12: The database table is created by default.




12: Create a new Database Access object item, as shown below:




13: Referencing the project DLL, implementing the Iproductdao interface and the Productdao class

  
Productdao
public interface Iproductdao
{
Object Save (Product entity);

void Update (Product entity);

void Delete (Product entity);

Product get (object ID);

Product Load (object ID);

Ilist<product> Loadall ();
}


public class Productdao:iproductdao
{
Private Isessionfactory sessionfactory;

Public Productdao ()
{
var cfg = new NHibernate.Cfg.Configuration (). Configure ("Config/hibernate.cfg.xml");
Sessionfactory = cfg. Buildsessionfactory ();
}

public Object Save (domain.product entity)
{
using (ISession session = Sessionfactory.opensession ())
{
var id = session. Save (entity);
Session. Flush ();
return ID;
}
}

public void Update (domain.product entity)
{
using (ISession session = Sessionfactory.opensession ())
{
Session. Update (entity);
Session. Flush ();
}
}

public void Delete (domain.product entity)
{
using (ISession session = Sessionfactory.opensession ())
{
Session. Delete (entity);
Session. Flush ();
}
}

Public domain.product get (object ID)
{
using (ISession session = Sessionfactory.opensession ())
{
return session. Get<domain.product> (ID);
}
}

Public domain.product Load (object ID)
{
using (ISession session = Sessionfactory.opensession ())
{
return session. Load<domain.product> (ID);
}
}

Public ilist<domain.product> Loadall ()
{
using (ISession session = Sessionfactory.opensession ())
{
return session. Query<domain.product> (). ToList ();
}
}
}

14: Write Test class [Productdaotest]:

[Testfixture]
public class Productdaotest
{
Private Iproductdao Productdao;

[SetUp]
public void Init ()
{
Productdao = new Productdao ();
}

[Test]
public void Savetest ()
{
var product = new Domain.product
{
ID = Guid.NewGuid (),
Buyprice = 10M,
Code = "ABC123",
Name = "Computer",
QuantityPerUnit = "20x1",
Sellprice = 11M,
Unit = "Taiwan"
};

var obj = this.productDao.Save (product);

Assert.notnull (obj);
}

[Test]
public void Updatetest ()
{
var product = This.productDao.LoadAll (). FirstOrDefault ();
Assert.notnull (product);

Product. Sellprice = 12M;

Assert.AreEqual (12M, product. Sellprice);
}

[Test]
public void Deletetest ()
{
var product = This.productDao.LoadAll (). FirstOrDefault ();
Assert.notnull (product);

var id = product.id;
This.productDao.Delete (product);
Assert.null (This.productDao.Get (id));
}

[Test]
public void Gettest ()
{
var product = This.productDao.LoadAll (). FirstOrDefault ();
Assert.notnull (product);

var id = product.id;
Assert.notnull (This.productDao.Get (id));
}

[Test]
public void LoadTest ()
{
var product = This.productDao.LoadAll (). FirstOrDefault ();
Assert.notnull (product);

var id = product.id;
Assert.notnull (This.productDao.Get (id));
}

[Test]
public void Loadalltest ()
{
var count = This.productDao.LoadAll (). Count;
Assert.true (Count > 0);
}
}
XV, run unit test, done, as shown below:


After the above 15 steps, finally put this basic operation to toss good, anyway NHibernate is so complex and powerful, like tossing hurriedly try it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.