Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

C# - 'Starting' FluentNHibernate

Junior Spellweaver
Joined
Aug 10, 2012
Messages
108
Reaction score
67
Hello, today I'm giving you a tutorial about how to make a MySQL Mapping application with FluentNHibernate.

First you must have these DLL's (search for them yourself):
  • Castle.Core.dll
  • FluentNHibernate.dll
  • Iesi.Collections.dll (NOT SURE!)
  • MySQL.Data.dll
  • NHibernate.dll

Create a new project. Add the dll's in the references of your project. Create a new class.
For example, this will be your class:

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YOURNAMESPACE
{
     class FluentNHibernateManager
     {
     }
}

Under using System.Text, add:
PHP:
using NHibernate;
using FluentNHibernate;
using FluentNHibernate.Cfg;
using NHibernate.Cfg;
using Ferri_Emulator.Database.Mappings;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Automapping;
using MySql.Data.MySqlClient;
[SUB]I know some might be useless, but this is what I have.[/SUB]

First, in the root of your class, add a new variable with the type ISessionFactory. This is an important part, this is going to do all of the queries (well, by using a void in ISessionFactory). Also create a new variable with the type string (your connection string).

PHP:
public ISessionFactory SessionFactory;
public string ConnectionString;

Now create a new method. In this we're going to make the connectionstring. A connectionstring can easily be made with the MySqlConnectionStringBuilder class. For easier use, you can add parameters in the void.

PHP:
public void CreateConnectionStringBuilder()
{
this.ConnectionString = new MySqlConnectionStringBuilder()
{
Server = "localhost",
UserID = "root",
Password = "",
Database = "ferridb"
}.ToString();
}

Now make a new void. In this new void, we're going to 'start' FluentNHibernate.

PHP:
public void StartFluentNHibernate()
{
}

Before adding codes in the void, make 2 new classes. Both the same name, but after 1 of the classes, add Map

For example: your class names will be Users, so the classes:

Users.cs
UsersMap.cs

Open the class with Map after it's name. After class NAME, add : ClassMap<Users>
Go to the class without Map after it's name. Add new table rows you wanna add, for example:

PHP:
public virtual int ID { get; set; }
public virtual string Username { get; set; }

Go back to the class with Map. Create a new constructor (public CLASSNAME() { })
I'm going through the methods of ClassMap<CLASSNAME>.

PHP:
  Table("members");

This starts the ClassMap listening on a table from the database you've choosen in your connectionstringbuilder.

PHP:
LazyLoad();

TBH, I don't know what this is, I think something with loading.

PHP:
Id(..)

This will set the default Id. For example, this will be the code.

PHP:
Id(x => x.id).GeneratedBy.Identity().Column("id");

x = new CLASSNAMEWITHOUTMAP
x.id = the virtual int from your class

PHP:
Column("id")

This will get the column from the table you've choosen.

PHP:
Map(...)

The same as Id, but for all other values. Also GeneratedBy.Identity() is not in here. For making the Column not nullable, add after Column(""):

PHP:
.Not.Nullable();

Now we're going back to our FluentNHibernateManager class. In the void to start Fluent NHibernate, add:

PHP:
                this.SessionFactory = Fluently.Configure().Database(MySQLConfiguration.Standard.ConnectionString(this.ConnectionString))
                    .Cache(c => c.UseQueryCache().UseSecondLevelCache().UseMinimalPuts())
                    .Mappings(x => x.FluentMappings.Add<usersMap>())
                    .BuildSessionFactory();

I'm going to explain one method before I end this tutorial:

PHP:
Mappings(x => x.FluentMappings.Add<usersMap>())

This is to add Mappings. Remember we made 2 classes? The classname with Map will be the class that needs to be between < and >. You can add as many classes as you like, but do everytime the same thing. Now you have started Fluent NHibernate.
 
JavaScript Is Best Script
Joined
Dec 13, 2010
Messages
631
Reaction score
131
Nice job, very useful but there are certain things to note:

This is to add Mappings. Remember we made 2 classes? The classname with Map will be the class that needs to be between < and >. You can add as many classes as you like, but do everytime the same thing. Now you have started Fluent NHibernate.

The thing that goes in between < and > (of course, when calling the method) is called a type argument just in case there is any future needs to refer to it.

Under using System.Text, add:

The order doesn't have to be fixed. Just punch it in anywhere you wish, as long as it "it precedes all other elements defined in the namespace except extern alias declarations." In fact. you can sort them by right clicking (within Visual Studio) -> Organize Usings -> Sort Usings.

I know some might be useless, but this is what I have.

Again, in Right click -> Organize Usings, the option "Removed Unused Usings" would allow you to remove usings that are not used within the code file. In fact, in VS2012(not sure about the other version) there's a "remove and sort" option to remove and then sort the usings. Pretty neat!
 
Experienced Elementalist
Joined
Aug 28, 2012
Messages
214
Reaction score
137
You didn't even explain to us what we're adding. I don't have a clue what I was doing.
 
• ♠️​ ♦️ ♣️ ​♥️ •
Joined
Mar 25, 2012
Messages
909
Reaction score
464
it rly hurts, why u explain something that u cant explain actually. im not just mad cuz im an entityframework lover, also this is ridiculous...

[SUB]I know some might be useless, but this is what I have.[/SUB]
if u dont kno for what your assemblies are, stop explaining programming in any direction.

First, in the root of your class, add a new variable with the type ISessionFactory. This is an important part, this is going to do all of the queries (well, by using a void in ISessionFactory). Also create a new variable with the type string (your connection string).
hell get a c# book, let me think, u dont even kno what this interface is for, right?
but its an important part? why? this isnt a good topic for u my dear. also can u prove the use of a void, or is it still just a non consistent type that represents the nothing? go and use a void in an interface, hahaha. sry im mean, but srsly whats the point for explaining something when its so wrong explained obviously.
you can add parameters in the void
how the fk? xD. stop to call your methods according to their return types.

....

i stop here now or i will never get to end, its getting just more and more ridiculous. and i would never post on my own tutorial that ive no idea how anything is done (i.e. LazyLoad() in your case). never got any ideas to use google and wiki at least few times a day?

a little kid whos copying and pasting a base build model of a free entity framework by pasting code snippets that use interfaces, properties, inherits, lambda expressions, lazy initializations, and generic types without writing any word of them and without knowing anything.

im so sry, but this had to be done :)



edit: sad, i just mentioned he is banned :(
 
Back
Top