Today we will be designing the basic interface of the business logic and database. Among other things, implementation of the entity, the interface repository.
Entities
Let's start with the creation of an entity. For the purpose of building the application architecture so far only a creature entities. It will be you. Let's start by implementing an interface entity. Part of it is common for each entity id. So, our interface will have just the box.
public interface IEntity
To help realize we create The base entities which will be implemented the above interface. This we do not have to write in each entity id field. In the event that there are other fields that will be shared as easily, you can modify the underlying entities, which will affect all entities have.
's implementation of core entities: public abstract class BaseEntity: IEntity
{public virtual Guid
Id {get; set;}}
public class User: BaseEntity, IEntity
As I have the basic types from which they will inherit our entities, we can now implement the first of them:
{
public virtual string Username { get; set; }
public virtual string PasswordQuestion { get; set; }
public virtual string Password { get; set; }
public virtual string Email { get; set; }
public virtual string PasswordAnswer { get; set; }
void Delete(Guid id);
public virtual bool isLockedOut { get; set; }
public virtual DateTime LastActivityDate { get; set; }
public virtual DateTime LastLoginDate { get; set; }
public virtual DateTime LastLockedOutDate { get; set; }
public virtual DateTime CreatedDate { get; set; }
}
Repositories
Then I would like to say something about the objects that can be found in my project. These are the repositories. The repository is a class that directly have access to the database. It provides basic CRUD and other directly related to the entity. Mediates between the base and the services which these data will be needed. This allows the decoupling of the project on a specific ORM-a. Substitution requires only implement the context database and the various repositories. Implementation of the interface repository looks like this: public interface
IRepository \u0026lt;TEntity> where TEntity : class
{
TEntity GetById(Guid id);
IEnumerable<TEntity> GetBySpec(ISpecification<TEntity> spec);
void Add(TEntity entity);
void Delete(TEntity entity); IEnumerable<TEntity> GetPagedElements<S>(int pageIndex, int pageSize, Expression<Func<TEntity, S>> orderExpression, bool ascending); IEnumerable<TEntity> GetPagedElements<S>(int pageIndex, int pageSize, Expression<Func<TEntity, S>> orderExpression, ISpecification \u0026lt;TEntity> specification, bool ascending);
IEnumerable \u0026lt;TEntity> All ();}
public override Expression \u0026lt;Func \u0026lt;User , bool>> SatisfiedBy () {
Most of the features here is quite understandable. The only puzzle is possible to be ISpecifcation interface. Look at the example implementations: public class
UserOnlineUsersSpecification: Specification \u0026lt;user>
{private DateTime
_onlineTime;
public UserOnlineUsersSpecification () {
}
_onlineTime = DateTime.Now.AddMinutes (-5);
return x => x.LastActivityDate> = _onlineTime;
}}
This specification is intended to refer lamba expression that will allow us to find the currently registered users online. How widzymy every specification consists SatisfiedBy method that returns the lamba expression. This expression is used in the database to retrieve the data. In short this is a small part of the business logic wrapped in a class which makes it can be used in many places and avoid repetition in the code. More on this pattern can be found on Wikipedia
In the following we will deal with entries already concrete implementation classes associated with NHibernate and the database.
0 comments:
Post a Comment