This is a class from an old project:
using System;
namespace AdministrableApplication.Core
{
public class Details
{
public virtual Guid detailsId { get; set; }
public virtual Boolean formDisplayed { get; set; }
public virtual Boolean formExtraFieldDisplayed { get; set; }
public virtual string formExtraFieldName { get; set; }
public virtual string formName { get; set; }
public virtual string formRedirectsTo { get; set; }
public virtual Boolean formSendsUserAnEmail { get; set; }
public virtual string h1 { get; set; }
public virtual string h2 { get; set; }
public virtual string htmlMain { get; set; }
public virtual string htmlSidebar { get; set; }
public virtual string htmlSupplement { get; set; }
public virtual string metatagDescription { get; set; }
public virtual string metatagKeywords { get; set; }
public virtual string metatagTitle { get; set; }
public virtual Guid pageId { get; set; }
}
}
It has this Fluent NHibernate map:
using AdministrableApplication.Core;
using FluentNHibernate.Mapping;
namespace AdministrableApplication.DataIntegration
{
public class DetailsMap : ClassMap<Details>
{
public DetailsMap()
{
Id(x => x.detailsId);
Map(x => x.formDisplayed);
Map(x => x.formExtraFieldDisplayed);
Map(x => x.formExtraFieldName);
Map(x => x.formName);
Map(x => x.formRedirectsTo);
Map(x => x.formSendsUserAnEmail);
Map(x => x.h1);
Map(x => x.h2);
Map(x => x.htmlMain);
Map(x => x.htmlSidebar);
Map(x => x.htmlSupplement);
Map(x => x.metatagDescription);
Map(x => x.metatagKeywords);
Map(x => x.metatagTitle);
Map(x => x.pageId);
}
}
}
It has this repository:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AdministrableApplication.Core.interfaces;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Criterion;
using Details = AdministrableApplication.Core.Details;
namespace AdministrableApplication.DataIntegration
{
public class DetailsRepository : IDetailsRepository
{
public void addDetails(Details details)
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
session.Save(details);
session.Flush();
transaction.Commit();
session.Close();
transaction.Dispose();
sessionFactory.Close();
}
}
}
public void deleteDetails(Details details)
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
session.Delete(details);
session.Flush();
transaction.Commit();
session.Close();
transaction.Dispose();
sessionFactory.Close();
sessionFactory.Dispose();
}
}
}
public Details retrieveDetails(Guid pageId)
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
session.CreateCriteria(typeof (Details)).List();
IList iList = session.CreateCriteria(typeof(Details))
.Add(Restrictions.Eq("pageId", pageId)).List();
IList<Details> allDetailsList = getDetails<Details>(iList);
Details[] relevantDetails = new Details[] { };
relevantDetails = allDetailsList.ToArray();
session.Flush();
session.Close();
sessionFactory.Close();
sessionFactory.Dispose();
if (relevantDetails.Length == 1)
{
return relevantDetails[0];
} else {
return null;
}
}
}
public void updateDetails(Details details)
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
session.SaveOrUpdate(details);
session.Flush();
transaction.Commit();
session.Close();
transaction.Dispose();
sessionFactory.Close();
sessionFactory.Dispose();
}
}
}
public static IList<T> getDetails<T>(IList iList)
{
IList<T> result = new List<T>();
foreach (T value in iList)
{
result.Add(value);
}
return result;
}
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure().Database(
MsSqlConfiguration.MsSql2005
.ConnectionString(c => c
.FromAppSetting("FluentNHibernateConnection"))
)
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<SettingsRepository>())
.BuildSessionFactory();
}
}
}
The repository is called via this interface:
using System;
namespace AdministrableApplication.Core.interfaces
{
public interface IDetailsRepository
{
void addDetails(Details details);
void deleteDetails(Details details);
Details retrieveDetails(Guid pageId);
void updateDetails(Details details);
}
}
No comments:
Post a Comment