Example:
Connection
public interface IConnection: IDisposable
{
void Close();
ISession Open();
FluentConfiguration Configuration { get; }
ISessionFactory SessioFactory { get; }
ISession Session { get; }
}
public class Connection : IConnection
{
private FluentConfiguration _configuration;
private ISessionFactory _sessiofactory;
private ISession _session;
public FluentConfiguration Configuration
{
get { return _configuration; }
private set { _configuration = value; }
}
public ISessionFactory SessioFactory
{
get { return _sessiofactory; }
private set { _sessiofactory = value; }
}
public ISession Session
{
get { return _session; }
private set { _session = value; }
}
public Connection()
{
Init();
}
private void Init()
{
_configuration = Fluently.Configure()
.Database(MySQLConfiguration.Standard.ConnectionString(x => x
.Server("localhost")
.Username("root")
.Password("senha")
.Database("site1")))
.Mappings(c => c.FluentMappings.AddFromAssemblyOf<WindowsFormsApp.Code.Models.Cliente>());
_sessiofactory = _configuration.BuildSessionFactory();
_session = _sessiofactory.OpenSession();
}
public ISession Open()
{
if (_session.IsOpen == false)
{
_session = _sessiofactory.OpenSession();
}
return _session;
}
public void Close()
{
if (_session != null && _session.IsOpen)
{
_session.Close();
_session.Dispose();
}
_configuration = null;
if (_sessiofactory != null && _sessiofactory.IsClosed == false)
{
_sessiofactory.Close();
_sessiofactory.Dispose();
}
}
~Connection()
{
}
public void Dispose()
{
Close();
}
}
Repository
public interface IRepository<T>: IDisposable
where T : class, new()
{
IConnection Connection { get; }
void Add(T model);
void Edit(T model);
void AddorEdit(T model);
void Remove(T model);
T Find(object Key);
IQueryable<T> Query();
IQueryable<T> Query(Expression<Func<T, bool>> Where);
}
using NHibernate.Linq;
public abstract class Repository<T> : IRepository<T> where T : class, new()
{
private IConnection _connection;
public IConnection Connection
{
get { return this._connection; }
private set { this._connection = value; }
}
public Repository()
{
this._connection = new Connection();
this.Connection.Open();
}
public Repository(IConnection Connection)
{
this._connection = Connection;
this.Connection.Open();
}
public void Add(T model)
{
this._connection.Session.Transaction.Begin();
this._connection.Session.Save(model);
this._connection.Session.Transaction.Commit();
}
public void Edit(T model)
{
this._connection.Session.Transaction.Begin();
this._connection.Session.SaveOrUpdate(model);
this._connection.Session.Transaction.Commit();
}
public void AddorEdit(T model)
{
this._connection.Session.Transaction.Begin();
this._connection.Session.SaveOrUpdate(model);
this._connection.Session.Transaction.Commit();
}
public void Remove(T model)
{
this._connection.Session.Transaction.Begin();
this._connection.Session.Delete(model);
this._connection.Session.Transaction.Commit();
}
public T Find(object Key)
{
return (T)this._connection.Session.Get<T>(Key);
}
public IQueryable<T> Query()
{
try
{
return this._connection.Session.Query<T>();
}
catch (NHibernate.ADOException ex)
{
var er = ex.Data;
}
return null;
}
public IQueryable<T> Query(Expression<Func<T, bool>> Where)
{
return this._connection.Session.Query<T>().Where(Where);
}
~Repository()
{
}
public void Dispose()
{
if (_connection != null)
{
_connection = null;
}
}
}
Class Mapped
//CLIENTE E CLIENTEMAP
public class ClienteMap : ClassMap<Cliente>
{
public ClienteMap()
{
Table("cliente");
Id(x => x.Codigo).GeneratedBy.Increment().Not.Nullable().Column("codigo");
Map(x => x.Nome).Nullable().Column("nome");
HasMany(x => x.Telefone).Cascade.All().LazyLoad().KeyColumn("codigocliente");
}
}
public class Cliente
{
public Cliente()
{
this.Telefone = new List<Telefone>();
}
public virtual int Codigo { get; set; }
public virtual String Nome { get; set; }
public virtual IList<Telefone> Telefone { get; set; }
}
//TELEFONE E TELEFONEMAP
public class TelefoneMap : ClassMap<Telefone>
{
public TelefoneMap()
{
Table("telefone");
Id(x => x.Codigo).Not.Nullable().UniqueKey("codigo").GeneratedBy.Increment().Column("codigo");
Map(x => x.Ddd).Not.Nullable().Column("ddd").Length(3);
Map(x => x.Numero).Not.Nullable().Column("numero").Length(14);
References(x => x.Cliente).Cascade.All().LazyLoad().Column("codigocliente");
}
}
public class Telefone
{
public Telefone() { }
public virtual int Codigo { get; set; }
public virtual Cliente Cliente { get; set; }
public virtual String Ddd { get; set; }
public virtual String Numero { get; set; }
}
RepositoryCliente e RepositoryTelefone
public sealed class RepositoryCliente : Repository<Cliente>
{
public RepositoryCliente() : base() { }
public RepositoryCliente(IConnection Connection)
: base(Connection) { }
}
public sealed class RepositoryTelefone : Repository<Telefone>
{
public RepositoryTelefone() : base() { }
public RepositoryTelefone(IConnection Connection)
: base(Connection) { }
}
Well that’s all the code I usually do, now how to use it all:
Explanation: I want to show in ListBox
the Clientes
and on Gridview their respective Phones
Form
Form Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApp.Code;
using WindowsFormsApp.Code.Abstract;
using WindowsFormsApp.Code.Models;
namespace WindowsFormsApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Repository<Cliente> RepCliente;
private void Form1_Load(object sender, EventArgs e)
{
DataGridFones.AutoGenerateColumns = false;
RepCliente = new RepositoryCliente();
ListClientes.DataSource = RepCliente.Query().ToList();
ListClientes.DisplayMember = "Nome";
ListClientes.ValueMember = "Codigo";
}
~Form1()
{
RepCliente.Dispose();
}
private void ListClientes_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListClientes.SelectedItems.Count > 0 &&
ListClientes.SelectedIndex >= 0)
{
Cliente cliente = RepCliente.Find(((Cliente)ListClientes.SelectedItem).Codigo);
if (cliente != null)
{
DataGridFones.DataSource = cliente.Telefone.ToList();
DataGridFones.Update();
}
}
}
}
}
Good with only RepositoryCliente
i can redeem without closing your session and only close when the form closes. Also a little code point that does a lot of stuff on form, and the important working with lazyload by the configuration given by framework
.
About the doubt of several Session
open, how to use 1 for all Repository
Coding Example:
using (IConnection connection = new Connection())
using (Repository<Cliente> RepClientes = new RepositoryCliente(connection))
using (Repository<Telefone> RepTelefones = new RepositoryTelefone(connection))
{
//CODIFICAÇÃO
}
Or
IConnection connection = new Connection();
Repository<Cliente> RepClientes = new RepositoryCliente(connection);
Repository<Telefone> RepTelefones = new RepositoryTelefone(connection);
//CODIFICAÇÃO
//CODIFICAÇÃO
//CODIFICAÇÃO
RepClientes.Dispose();
RepTelefones.Dispose();
connection.Dispose();
It was then made a connection
that will serve two or more repository
.
I have a question: are you accepting answers that focus on changing DAL technology? I ask this because the Entity Framework is not based on the concept of data sessions. That is, the data is only loaded when the aggregate object is actually used.
– Leonel Sanches da Silva
I do, but I’m very concerned about the other layers, they can’t (shouldn’t) change.
– Latrova
@Lizard, your doubt would be at the time of the time the schedules click have to open again?
– user6026
@Harrypotter not necessarily, but it’s an affair. It turns out that if I open a new Ssion, it doesn’t work (unless I’ve performed some wrong procedure). Anyway, if you have any solution for all situations (not just this one) put (:
– Latrova
@Lizard so I asked, because, it will depend a lot on the way you implemented this screen, or even the classes, how I work with patterns for me does not need to change your repository in Ibernate (although the Entity Framework is the best solution)What I need to see honestly was parts of your code.
– user6026
@Harrypotter I would very much like to know your proposal, because of my little experience with this type of tools. What do you suggest? That I post screen image and code?
– Latrova
You can put both!!!
– user6026
@Harrypotter Done, what do you think?
– Latrova
is the way to do it in my mind, because I would do it differently and I would not close the session right away, I would work with addiction injection and solve the part of Sesssion, what I could do is a simple example of the way this screen !
– user6026
@Harrypotter If it wouldn’t be in your way and you’re not too busy, I’d love to (but it would change the layers a lot?
– Latrova
@Lizard I’ll propose you something else then you can decide ok!
– user6026
@Lizard if you need the source code send me an e-mail [email protected] that I send the full example more ta ai as I would reply!
– user6026
@Harrypotter I already sent the e-mail (:
– Latrova
I’ll send you the zip with those changes
– user6026
Perhaps a simple and practical suggestion would be to use Yield. http://msdn.microsoft.com/pt-br/library/9k7k7cf0.aspx
– giovanibarili