Sunday, August 8, 2010

How To Use Ubuntu For Runescape Botting

MVC Extensions interface for accessing a database sequence continued ...

As mentioned earlier the next step in the implementation of the interface associated with the database repository will be essential. It will have a common part of all the repositories. So, for example, CRUD operations (insert, add, delete, modify), get the id and return items in a given period (paging elements). This is the implementation of this class: public class

BaseRepository \u0026lt;TEntity>: IRepository \u0026lt;TEntity>, IDisposable TEntity WHERE: class, IEntity
{
private DatabaseContext _databaseContext;
public BaseRepository(IDatabaseFactory databaseFactory)
{
Check.Argument.IsNotNull(databaseFactory, "databaseFactory"); }
     protected IDatabaseFactory DatabaseFactory 
{
get;
private set;
}

protected DatabaseContext DatabaseContext
{
get
{
return _databaseContext ?? (_databaseContext = DatabaseFactory.Get());
}
}

public void Dispose()
{
if (DatabaseContext != null)
GC.SuppressFinalize(DatabaseContext);

if (DatabaseFactory != null)
GC.SuppressFinalize(DatabaseFactory);
}

public IEnumerable<TEntity> GetBySpec(ISpecification<TEntity> specification)
{
Check.Argument.IsNotNull(specification, "specification");

IEnumerable<TEntity> result = DatabaseContext.CreateQuery<TEntity>().Where(specification.SatisfiedBy());

return result;
}

public virtual void Add(TEntity entity)
{
Check.Argument.IsNotNull(entity, "entity");

DatabaseContext.Save<TEntity>(entity);
}

public virtual void Delete(Guid id)
{
Check.Argument.IsNotNull(id, "id");

TEntity entity = GetById(id);

if (entity != null)
DatabaseContext.Delete<TEntity>(entity);
}

public virtual void Delete(TEntity entity)
{
Check.Argument.IsNotNull(entity, "entity");

DatabaseContext.Delete<TEntity>(entity);
}

public virtual TEntity GetById(Guid id)
{
return DatabaseContext.CreateQuery<TEntity>().SingleOrDefault<TEntity>(x => x.Id == id);
}

public virtual IEnumerable<TEntity> All()
{
return DatabaseContext.CreateQuery<TEntity>();
}

public IEnumerable<TEntity> GetPagedElements<S>(int pageIndex, int pageSize, Expression<Func<TEntity, S>> orderExpression, bool ascending)
{
Check.Argument.IsNotNegativeOrZero(pageIndex, "pageIndex");
Check.Argument.IsNotNegative(pageSize, "pageSize");
Check.Argument.IsNotNull(orderExpression, "orderExpression");

IQueryable<TEntity> objectQuery = DatabaseContext.CreateQuery<TEntity>();

return (ascending) ? objectQuery.OrderBy(orderExpression)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.ToList()

: objectQuery.OrderByDescending(orderExpression)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.ToList();

}

public IEnumerable<TEntity> GetPagedElements<S>(int pageIndex, int pageSize, Expression<Func<TEntity, S>> orderExpression, ISpecification<TEntity> specification, bool ascending)
{
Check.Argument.IsNotNegativeOrZero(pageIndex, "pageIndex");
Check.Argument.IsNotNegative(pageSize, "pageSize");
Check.Argument.IsNotNull(orderExpression, "orderExpression");
Check.Argument.IsNotNull(specification, "specification");

IQueryable<TEntity> objectQuery = DatabaseContext.CreateQuery<TEntity>();

return (ascending) ? objectQuery.Where(specification.SatisfiedBy())
.OrderBy(orderExpression)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
. ToList ()

: objectQuery.Where (specification.SatisfiedBy ())
. OrderByDescending (orderExpression)
. Skip ((PageIndex - 1) * pageSize)
. Take (pageSize)
. ToList ();}


}

As you can see most of the methods here are simple operations on a database using LINQ.
Besides this is the method by means of a previously mentioned specifications pulls data from the database. Now the example of a simple repository of user: public class

UserRepository: BaseRepository \u0026lt;user>, IUserRepository
{
public UserRepository(IDatabaseFactory dbFactory)
: base(dbFactory)
{

}
public int CountAllUsers()
{
return DatabaseContext.Users.Count();
     } 

public int CountNumberOfOnlineUsers()
{
UserOnlineUsersSpecification specification = new UserOnlineUsersSpecification();

return this.GetBySpec(specification).Count<User>();
}

public IEnumerable<User> GetAllUsersOnline()
{
UserOnlineUsersSpecification specification = new UserOnlineUsersSpecification();

return this.GetBySpec(specification);
}

public User FindUserByUsername(string username)
{
Check.Argument.IsNotEmpty(username, "username");

UserUsernameSpecification specification = new UserUsernameSpecification(username);

return this.GetBySpec(specification).SingleOrDefault<User>();
}


public User FindUserByEmail(string email)
{
Check.Argument.IsNotEmpty(email, "email");
Check.Argument.IsNotInvalidEmail(email, "email");

UserEmailSpecification specification = new UserEmailSpecification (email);

return this.GetBySpec (specification). SingleOrDefault \u0026lt;user> ();}



} In this example, we can already see the use of these mysterious specifications. As you can see this solution is quite nice presents and separates the logic from the database itself. In addition, they can be re-used in different places of our repository. I added three types of specifications for a project associated with the user. So one can also see. This repository is using them.

In fact, the entire interface to the database already is almost ready. There are possibly a small patch. The project NHibernate tests added tests related to the classes above.


0 comments:

Post a Comment