protected void Button1_Click(object sender, EventArgs e) {
IAirportRepository objAirportDao = daoFactory.GetAirportDao();
Airport objAirport = objAirportDao.GetByAirportCode ("A1");
if (objAirport != null){
objAirportDao.Delete(objAirport);
//objAirportDao.CommitChanges();
}
}
public class AirportRepository : AbstractNHibernateRepository<Airport, int>, IAirportRepository
{
public AirportRepository() { }
public Airport GetByAirportCode(string airportCode) {
return GetByCriteria(new ICriterion[] { Expression.Eq("AirportCode", airportCode )}).ToList<Airport>().FirstOrDefault();
}
}
public class NHibernateSessionFactory{
public static NHibernateSessionFactory Instance
{
get {
return Nested.NHibernateSessionFactory;
}
}
private NHibernateSessionFactory(){
InitSessionFactory();
}
private class Nested {
static Nested() { }
internal static readonly NHibernateSessionFactory NHibernateSessionFactory = new NHibernateSessionFactory();
}
private void InitSessionFactory(){
sessionFactory = new Configuration().Configure().BuildSessionFactory();
}
public void RegisterInterceptor(IInterceptor interceptor) {
ISession session = ContextSession;
if (session != null && session.IsOpen)
{
throw new CacheException("You cannot register an interceptor once a session has already been opened");
}
GetSession(interceptor);
}
public ISession GetSession(){
return GetSession(null);
}
private ISession GetSession(IInterceptor interceptor) {
ISession session = ContextSession;
if (session == null){
if (interceptor != null){
session = sessionFactory.OpenSession(interceptor);
}else{
session = sessionFactory.OpenSession();
}
ContextSession = session;
}
return session;
}
public void CloseSession(){
ISession session = ContextSession;
if (session != null && session.IsOpen){
session.Flush();
session.Close();
}
ContextSession = null;
}
public void BeginTransaction() {
ITransaction transaction = ContextTransaction;
if (transaction == null){
transaction = GetSession().BeginTransaction();
ContextTransaction = transaction;
}
}
public void CommitTransaction(){
ITransaction transaction = ContextTransaction;
try
{
if (HasOpenTransaction()) {
transaction.Commit();
ContextTransaction = null;
}
} catch (HibernateException){
RollbackTransaction();
throw;
}
}
public bool HasOpenTransaction(){
ITransaction transaction = ContextTransaction;
return transaction != null && !transaction.WasCommitted && !transaction.WasRolledBack;
}
public void RollbackTransaction(){
ITransaction transaction = ContextTransaction;
try
{
if (HasOpenTransaction()){
transaction.Rollback();
}
ContextTransaction = null;
}
finally
{
CloseSession();
}
}
private ITransaction ContextTransaction{
get
{
if (IsInWebContext()){
return (ITransaction)HttpContext.Current.Items[TRANSACTION_KEY];
}else{
return (ITransaction)CallContext.GetData(TRANSACTION_KEY);
}
}
set
{
if (IsInWebContext()){
HttpContext.Current.Items[TRANSACTION_KEY] = value;
}else{
CallContext.SetData(TRANSACTION_KEY, value);
}
}
}
private ISession ContextSession{
get
{
if (IsInWebContext())
{
return (ISession)HttpContext.Current.Items[SESSION_KEY];
}
else
{
return (ISession)CallContext.GetData(SESSION_KEY);
}
}
set
{
if (IsInWebContext())
{
HttpContext.Current.Items[SESSION_KEY] = value;
}
else
{
CallContext.SetData(SESSION_KEY, value);
}
}
}
private bool IsInWebContext() {
return HttpContext.Current != null;
}
private const string TRANSACTION_KEY = "CONTEXT_TRANSACTION";
private const string SESSION_KEY = "CONTEXT_SESSION";
private ISessionFactory sessionFactory;
}
public abstract class AbstractNHibernateRepository<T, IdT> : IRepository<T, IdT> {public T GetById(IdT id, bool shouldLock){
T entity;
if (shouldLock) {
entity = (T)NHibernateSession.Load(persitentType, id, LockMode.Upgrade);
} else {
entity = (T)NHibernateSession.Load(persitentType, id);
}
return entity;
}
public List<T> GetAll() {
return GetByCriteria();
}
public List<T> GetByCriteria(params ICriterion[] criterion) {
ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);
foreach (ICriterion criterium in criterion)
{
criteria.Add(criterium);
}
return criteria.List<T>() as List<T>;
}
public List<T> GetByExample(T exampleInstance, params string[] propertiesToExclude) {
ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);
Example example = Example.Create(exampleInstance);
foreach (string propertyToExclude in propertiesToExclude) {
example.ExcludeProperty(propertyToExclude);
}
criteria.Add(example);
return criteria.List<T>() as List<T>;
}
public T GetUniqueByExample(T exampleInstance, params string[] propertiesToExclude) {
List<T> foundList = GetByExample(exampleInstance, propertiesToExclude);
if (foundList.Count > 1) {
throw new NonUniqueResultException(foundList.Count);
}
if (foundList.Count > 0){
return foundList[0];
} else {
return default(T);
}
}
public T Save(T entity){
NHibernateSession.Save(entity);
return entity;
}
public T SaveOrUpdate(T entity) {
NHibernateSession.SaveOrUpdate(entity);
return entity;
}
public void Delete(T entity){
NHibernateSession.Delete(entity);
}
public void CommitChanges() {
if (NHibernateSessionFactory.Instance.HasOpenTransaction())
{
NHibernateSessionFactory.Instance.CommitTransaction();
} else{
NHibernateSessionFactory.Instance.GetSession().Flush();
}
}
private ISession NHibernateSession{
get {
return NHibernateSessionFactory.Instance.GetSession();
}
}
private Type persitentType = typeof(T);
}
public void Delete(T entity) {
NHibernateSession.Delete(entity);
NHibernateSession.Flush();
}
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.
To post to this group, send email to nhu...@googlegroups.com.
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "nhusers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nhusers/svafC-vVlAI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nhusers+u...@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "nhusers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nhusers/svafC-vVlAI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nhusers+u...@googlegroups.com.