public class DataController
{
private ISessionFactory _sessions;
public DataController()
{
Configuration cfg = new Configuration();
cfg.AddClass( typeof (DataElement)); //
cfg.AddClass( typeof (Pins));
cfg.AddClass( typeof (Properties));
cfg.AddClass( typeof (Library));
_sessions = cfg.BuildSessionFactory(); //
}
}
* This source code was highlighted with Source Code Highlighter .
public DataElement SaveElement(DataElement el)
{
ISession session = _sessions.OpenSession(); //
try
{
session.Save(el); //
return el; // , PK, PK
}
catch (HibernateException e)
{
throw e;
}
finally
{
session.Close(); //
}
}
* This source code was highlighted with Source Code Highlighter .
public interface IGenericDao
{
*type* Get( int id);
*type* Save(*type*obj);
*type* SaveOrUpdate(*type* obj);
void Delete(*type* obj);
}
* This source code was highlighted with Source Code Highlighter .
public interface IGenericDao<T, ID>
{
T Get(ID id);
T Save(T obj);
T SaveOrUpdate(T obj);
void Delete(T obj);
}
* This source code was highlighted with Source Code Highlighter .
public class SessionFactory
{
public static void Init() //
{
Configuration cfg = new Configuration();
cfg.AddAssembly(”Win.Objects”); // NHibernate. , .
sessionFactory = cfg.BuildSessionFactory();
}
private static ISessionFactory sessionFactory; // ,
private static ISession threadSession //
{
get
{
return (ISession)CallContext.GetData(”THREAD_SESSION”); // ,
}
set
{
CallContext.SetData(”THREAD_SESSION”, value );
}
}
public static ISession GetSession() // .
{
ISession session = threadSession; //
if (session == null ) // “”
{
session = sessionFactory.OpenSession(); //
threadSession = session; //
}
return session; //
}
}
* This source code was highlighted with Source Code Highlighter .
public class GenericImpl<T, ID> : IGenericDao<T, ID> // IGenericDao
{
private ISession session //
{
get
{
return SessionFactory.GetSession(); // .
}
}
private Type _type = typeof (T); // , .
public T Get(ID id) //
{
try
{
T result = (T) session.Load(_type, id); // T Load
return result; //
}
catch (HibernateException e)
{
throw e;
}
}
public T Save(T obj)
{
try
{
session.Save(obj);
return obj;
}
catch (HibernateException e)
{
throw e;
}
}
public T SaveOrUpdate(T obj)
{
session.SaveOrUpdate(obj);
return obj;
}
public void Delete(T obj)
{
session.Delete(obj);
}
}
* This source code was highlighted with Source Code Highlighter .
GenericImpl<Library, int > libdao = new GenericImpl<Library, int >(); // Library
Library lib = new Library(); //
lib.Name = “ ”; //
libdao.Save(lib); //
libdao.Get(1); //
libdao.Delete(lib); //
libdao.SaveOrUpdate(lib);//
* This source code was highlighted with Source Code Highlighter .
public interface IDataElementDao : IGenericDao<DataElement, int >{}
public interface ILibraryDao : IGenericDao<Library, int > { }
public interface IPinsDao : IGenericDao<Pins, int > { }
public interface IPropertiesDao : IGenericDao<Properties, int > { }
* This source code was highlighted with Source Code Highlighter .
public interface IDaoFactory
{
IDataElementDao GetDataElementDao();
ILibraryDao GetLibraryDao();
IPinsDao GetPinsDao();
IPropertiesDao GetPropertiesDao();
}
* This source code was highlighted with Source Code Highlighter .
public class HDataElement : GenericImpl<DataElement, int >, IDataElementDao{}
public class HLibrary : GenericImpl<Library, int >, ILibraryDao{}
public class HPins : GenericImpl<Pins, int >, IPinsDao{}
public class HProperties : GenericImpl<Properties, int >, IPropertiesDao{}
* This source code was highlighted with Source Code Highlighter .
public class FactoryDao : IDaoFactory
{
public IDataElementDao GetDataElementDao()
{
return new HDataElement();
}
public ILibraryDao GetLibraryDao()
{
return new HLibrary();
}
public IPinsDao GetPinsDao()
{
return new HPins();
}
public IPropertiesDao GetPropertiesDao()
{
return new HProperties();
}
}
* This source code was highlighted with Source Code Highlighter .
IDaoFactory fact = new FactoryDao(); //
ILibraryDao libdao = fact.GetLibraryDao(); // Library
Library lib = new Library();
lib.Name = “ ”;
libdao.Save(lib);
libdao.Get(1);
libdao.Delete(lib);
libdao.SaveOrUpdate(lib);
IDataElementDao eldao = fact.GetDataElementDao();// DataElement .
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/50187/