Fluent nhib1_other example, fluentnhib.pdf

Source: Internet
Author: User

Fluent nhib1_other example, fluentnhib.pdf
The latest version for testing:

SQL:

--- Create table Users (UserID int identity (1, 1) primary key, [Name] VARCHAR (50) NULL, [No] VARCHAR (50) NULL) goinsert into Users ([Name], [NO]) VALUES ('geovindu', '001') insert into Users ([Name], [NO]) VALUES ('sibodu', '002 ') select * FROM dbo. usersCREATE TABLE Projects (ProjectID int identity (1, 1) primary key, [Name] VARCHAR (50) NULL, UserID int foreign key references Users (UserID) goinsert into Projects ([Name], userID) VALUES (' ', 1) insert into Projects ([Name], UserID) VALUES ('', 1) insert into Projects ([Name], UserID) VALUES ('quiz ', 2) select * FROM dbo. userDetailsCREATE TABLE UserDetails (UserID intforeign key references Users (UserID), Sex int null, Age int null, BirthDate datetime default (GETDATE (), Height DECIMAL (6, 2) DEFAULT (0 )) goinsert into UserDetails (UserID, Sex, Age, BirthDate, Height) VALUES (1977, 40, '2017-02-14 ', 172.01) insert into UserDetails (UserID, Sex, Age, BirthDate, height) VALUES (2007, 10, '1970-12-07', 122.01) create table Product (ProductID int identity () primary key, [Name] VARCHAR (50) NULL, color VARCHAR (50) NULL) goinsert into Product ([Name], Color) VALUES ('TV', 'black') INSERT INTO Product ([Name], Color) VALUES ('dishwashers ', 'White') insert into Product ([Name], Color) VALUES ('microwave oven ', 'White') insert into Product ([Name], Color) VALUES ('notebooks, 'red') insert into Product ([Name], Color) VALUES (' ', 'red') insert into Product ([Name], Color) VALUES ('desk ', 'red') insert into Product ([Name], Color) VALUES ('car', 'red') insert into Product ([Name], Color) VALUES ('pen ', 'red') insert into Product ([Name], Color) VALUES ('paper', 'red ') create table ProjectProduct (ProjectID int foreign key references Projects (ProjectID), ProductID int foreign key references Product (ProductID) goinsert into ProjectProduct (ProjectID, ProductID) VALUES (1, 2) insert into ProjectProduct (ProjectID, ProductID) VALUES (1, 3) insert into ProjectProduct (ProjectID, ProductID) VALUES (1, 4) insert into ProjectProduct (ProjectID, ProductID) VALUES (1, 6) insert into ProjectProduct (ProjectID, ProductID) VALUES (2, 1) insert into ProjectProduct (ProjectID, ProductID) VALUES (2, 2) insert into ProjectProduct (ProjectID, ProductID) VALUES (2, 3) insert into ProjectProduct (ProjectID, ProductID) VALUES (2, 4) insert into ProjectProduct (ProjectID, ProductID) VALUES (2, 7) insert into ProjectProduct (ProjectID, ProductID) VALUES (2, 8) create table Tasks (TaskID int identity (1, 1) primary key, [Name] VARCHAR (50) NULL, ProjectID int foreign key references Projects (ProjectID) goinsert into Tasks ([Name], projectID) VALUES ('reminder delivery', 1) insert into Tasks ([Name], ProjectID) VALUES ('reminder accept', 2)

Entities (Model)

    /// <summary>    ///     /// </summary>    public abstract class Entity    {        virtual public int ID { get; set; }    }    public class User : Entity    {        virtual public string Name { get; set; }        public virtual string No { get; set; }        public virtual UserDetails UserDetails { get; set; }    }    public class Project : Entity    {        public Project()        {            Task = new List<Task>();            Product = new List<Product>();        }        public virtual string Name { get; set; }        public virtual User User { get; set; }        public virtual IList<Product> Product { get; set; }        public virtual IList<Task> Task { get; protected set; }    }    public class Product : Entity    {        public Product()        {            Project = new List<Project>();        }        public virtual IList<Project> Project { get; set; }        public virtual string Name { get; set; }        public virtual string Color { get; set; }    }    public class Task : Entity    {        public virtual string Name { get; set; }        public virtual Project Project { get; set; }    }    public class UserDetails : Entity    {        public virtual User User { get; set; }        public virtual int Sex { get; set; }        public virtual int Age { get; set; }        public virtual DateTime BirthDate { get; set; }        public virtual decimal Height { get; set; }    }

Mapping:

For the external key table design of each associated primary key, refer:

Http://www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat

Https://www.devbridge.com/articles/entity-framework-6-vs-nhibernate-4/

Http://blog.devart.com/support-of-many-to-one-mapping-for-component-navigation-properties-in-entity-developer.html

Https://www.devart.com/entitydeveloper/nhibernate-mapping-samples.html

Http://www.codeproject.com/Articles/19425/NHibernate-Templates-for-Smart-Code-Generator
Http://www.codeproject.com/Articles/247196/Components-Mapping-in-Fluent-NHibernate
Http://www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat

 

// One to one /// <summary> ///// </summary> public class UsersMapping: ClassMap <User> {public UsersMapping () {Table ("Users"); LazyLoad (); Id (x => x. ID ). column ("UserID "). generatedBy. identity (); HasOne (x => x. userDetails ). cascade. all (). propertyRef ("User"); // multiple-to-one Map (x => x. name ). nullable (); Map (x => x. no ). nullable () ;}} public class UserDetailsMapping: ClassMap <UserDetails> {public UserDetailsMapping () {Table ("UserDetails"); LazyLoad (); Id (x => x. ID ). column ("UserID "). generatedBy. foreign ("User"); Map (x => x. height ). nullable (); Map (x => x. age ). nullable (); Map (x => x. sex ). nullable (); Map (x => x. birthDate ). nullable (); HasOne (x => x. user ). cascade. all (); // one to more // <summary> //// </summary> public class TasksMappping: classMap <Task> {public TasksMappping () {Table ("Tasks"); LazyLoad (); Id (x => x. ID ). column ("TaskID "). generatedBy. identity (); References (x => x. project ). nullable (). column ("ProjectID "). cascade. none (); Map (x => x. name ). nullable () ;}/// <summary> /// </summary> // public class ProjectsMapping: classMap <Project> // {// public ProjectsMapping () // {// Table ("Projects"); // LazyLoad (); // Id (x => x. ID ). column ("ProjectID "). generatedBy. identity (); // References (x => x. user ). column ("UserID "). cascade. none (); // Map (x => x. name ). nullable (); // haslab( x => x. task ). keyColumn ("ProjectID "). lazyLoad (). cascade. saveUpdate (); //} // more to more public class ProjectsMapping: ClassMap <Project> {public ProjectsMapping () {Table ("Projects"); LazyLoad (); id (x => x. ID ). column ("ProjectID "). generatedBy. identity (); References (x => x. user ). column ("UserID "). cascade. none (); Map (x => x. name ). nullable (); haslab( x => x. task ). keyColumn ("ProjectID "). lazyLoad (). cascade. saveUpdate (); // a pair of HasManyToMany (x => x. product ). parentKeyColumn ("ProjectID "). childKeyColumn ("ProductID "). table ("ProjectProduct"); // many to many} public class ProductMapping: ClassMap <Product> {public ProductMapping () {Table ("Product "); id (x => x. ID ). column ("ProductID "). generatedBy. identity (); Map (x => x. name ). nullable (); Map (x => x. color ). nullable (); HasManyToMany (x => x. project ). parentKeyColumn ("ProductID "). childKeyColumn ("ProjectID "). table ("ProjectProduct"); // many-to-many }}

  

/// <Summary> ///// </summary> public partial class Form1: form {/// <summary> ///// </summary> public Form1 () {InitializeComponent ();} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> private void Form1_Load (object sender, eventArgs e) {}/// <summary> /// one-to-one ing // Add record /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </p Aram> private void button#click (object sender, EventArgs e) {try {// var session = NHibernateHelper. GetSession (); if (! Object. equals (session, null) {// get user from database User user1 = session. load <User> (1); // save the User data session. transaction. begin (); User user = new User () {Name = "White", No = "8888"}; UserDetails userDetails = new UserDetails () {Age = 18, birthDate = DateTime. now. date, Height = 140, Sex = 2}; user. userDetails = userDetails; userDetails. user = user; session. save (user); session. transaction. commit () ;}} catch (Exception ex) {MessageBox. show (ex. message. toString ());}} /// <summary> /// one-to-many/multiple-to-one // </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void button2_Click (object sender, eventArgs e) {var session = NHibernateHelper. getSession (); Project project = session. get <Project> (1); // save the User data session. transaction. begin (); Task task = new Task () {Name = "create", Project = project}; session. save (task); session. transaction. commit (); Task 1 = session. get <Task> (1 );} /// <summary> /// many-to-many /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void button3_Click (object sender, eventArgs e) {// var session = NHibernateHelper. getSession (); session. transaction. begin (); // get the Project ICriteria query = session. createCriteria <Project> (); IList <Project> projects = query. list <Project> (); // create the Product product Product = new product () {Name = "Product1", Color = "Red"}; Product. project = projects; session. save (product); session. transaction. commit ();}}

  

/// <Summary> /// FluentNHibernate ///// </summary> public class NHibernateHelper {private static ISessionFactory _ sessionFactory; private static ISession _ session; private static object _ objLock = new object (); // <summary> // </summary> private NHibernateHelper () {}/// <summary> /// create ISessionFactory /// </summary> /// <returns> </returns> public static ISessionFactory GetSessionFactory () {if (_ sessionFactory = null) {lock (_ objLock) {if (_ sessionFactory = null) {// configure ISessionFactory // _ sessionFactory = (new Configuration ()). configure (). buildSessionFactory (); _ sessionFactory = InitializeSessionFactory () ;}} return _ sessionFactory ;} /// <summary> ///// </summary> private static ISessionFactory InitializeSessionFactory () {_ sessionFactory = Fluently. configure (). database (MsSqlConfiguration. msSql2005.ConnectionString (@ "Server = GEOVINDU-PC \ GEOVIN; initial catalog = NHibernateSimpleDemo; User ID = sa; Password = 520 ;"). showSql ()). mappings (m => m. fluentMappings. addFromAssemblyOf <NHibernateHelper> ())//. exposeConfiguration (c => new SchemaExport (c ). create (true, true) // clears data. buildSessionFactory (); return _ sessionFactory;} // <summary> // </summary> private static ISessionFactory InitializeSessionFactoryP () {_ sessionFactory = Fluently. configure (). database (MsSqlConfiguration. msSql2005.ConnectionString (s => s. server (@ "LF-WEN \ GEOVINDU "). database ("NHibernateSimpleDemo "). username ("sa "). password ("520 ")). showSql ()). mappings (m => m. fluentMappings. addFromAssemblyOf <NHibernateHelper> ())//. exposeConfiguration (c => new SchemaExport (c ). create (true, true) // clears data. buildSessionFactory (); return _ sessionFactory ;} /// <summary> /// enable ISession /// </summary> /// <returns> </returns> public static ISession GetSession () {_ sessionFactory = GetSessionFactory (); if (_ session = null) {lock (_ objLock) {if (_ session = null) {_ session = _ sessionFactory. openSession () ;}}return _ session ;}}

  

 

The object Association and stored procedure operations in the query must continue.

 

  1. The MVC (Model-View-Controller) architecture is integrated with large systems. It can be divided into machines or servers on the actual plane, as long as each other has an appropriate variable. The MVVM (Model-View-ViewModel) architecture shares the user interface design that is irrelevant to code ignorance in XAML, as long as the specified commands in the View are connected to the ViewModel, you can enjoy the ViewModel preview function. However, ViewModel only needs to implement some special interfaces, so that it can communicate with the View preview. In the MVP (Model-View-Presenter) architecture, the program determines the application of the View action, and the View only needs to implement a specific interface, it does not require too much manual work, but the Presenter may be limited to the action of the View Interface, but cannot further control the View.
    Reference:
    Http://en.wikipedia.org/wiki/Model-view-presenter
    Http://en.wikipedia.org/wiki/Model-view-controller
    Http://en.wikipedia.org/wiki/Model_View_ViewModel

Related Article

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.