Since I decided to use my application to NHibernate 3.0, consequently, also wanted to be able to use FluentNhibernate. And there appeared a little problem. Since the authors do not officially support the new version of NHibernate yet because it is alpha. But when I wanted to use the current version is throwing errors on the version of the library he is not responding. Poszperałem a little on the internet and found solution. Well, just download the source FluentNHibernate, then in that project to replace the old library to new with NHibernate 3.0. Then build and everything works.
As already solved the problem I posted earlier basic classes of service database, such as DatabaseContext and DatabaseFactory. The investments were largely modeled on the CodePlex project shrinkr.
Database Context
DatabaseContext contains basic methods such as saving, deleting objects from the database, approving amendments. Public class
ISession _session;
private ITransaction _transaction;
private IQueryable<User> _users;
public DatabaseContext(ISession session) {
Check.Argument.IsNotNull(session, "session");
_session = session;
}
public IQueryable<User> Users
{
[DebuggerStepThrough]
get
{
return _users ?? (_users = CreateQuery<User>());
}
}
public virtual IQueryable<TEntity> CreateQuery<TEntity>() where TEntity : class, IEntity
{
return _session.Query<TEntity>();
}
public virtual TEntity GetById<TEntity>(long id) where TEntity : class, IEntity
{
Check.Argument.IsNotNegative(id, "id");
return _session.Get<TEntity>(id);
}
public virtual void Save<TEntity>(TEntity entity) where TEntity : class, IEntity
{
Check.Argument.IsNotNull(entity, "entity");
EnsureTransaction();
_session.SaveOrUpdate(entity);
}
public virtual void Delete<TEntity>(TEntity entity) where TEntity : class, IEntity
{
Check.Argument.IsNotNull(entity, "entity");
EnsureTransaction();
_session.Delete(entity);
}
public virtual void Commit()
{
EnsureTransaction();
_transaction.Commit();
}
private void EnsureTransaction()
{
if (_transaction == null
{
_transaction _session.BeginTransaction = ();
}}}
Database Factory
DatabaseFactory contrast is the main object associated with the management of the database because it is the whole configuration Nhibernate using the previously mentioned syntax Fluent . It is located in a private method, but I will not show it here. The interface of this class has only one method by means of which we can get DatabaseContext. Public interface
IDatabaseFactory
DatabaseContext {Get ();}
Unit of Work
last object associated with the database which is now added UnitOfWork. Since most operations on the data held by the transaction. We must have an object that allows us to approve the changes. When it is this objective has been created. The interface looks like this: public interface
IUnitOfWorkimplementations of the same class after invite to the sources.
{void Commit ();}
Oh, and even added to the walkthrough test project associated with Nhibernatem. That's all for today. In the next post will be a little bit about how to implement the underlying repositories and probably Pre Implementation repository user.
0 comments:
Post a Comment