Implementing Business Logic Using Entity Framework

I am new to Entity infrastructure and am embarrassed about how to implement Business Logic.

I use the Code First approach and created my POCOs. I have them in a separate project so that they can be used in several projects.

I would like to know how I can implement business logic for an object that is being checked when I try to store an item in a database. For example, if I define a rule that MyObject cannot be saved unless a name is entered, how can I do this?

Simple POCO Example

public class MyObject() {
    public String name { get; set; };

    public MyObject() {}
}

Obviously, I have many objects, and each object has different business rules.

I come from the background of the Csla business environment ( http://www.lhotka.net/cslanet/ ), where you define a business object that has a Save method. when Save is called, the environment starts ValidationRules and, thus, determines whether the call is needed in the database.

I would like something like this using the Entity Framework. Sample code would be great, or links to reading material. Thanks

+4
source share
1 answer

Usually I create a service level in my application for processing business logic, which negotiates with my level of data access to save any data.

public class JobService : IJobService
{
    private readonly IJobRepository _jobRepository;

    public JobService(IJobRepository jobRepository)
    {
        _jobRepository = jobRepository;
    }

    public IEnumerable<Job> GetAllJobs()
    {
        return _jobRepository.All().OrderBy(x => x.Name);
    }

    public Job GetJobById(Guid Id)
    {
        return _jobRepository.FindBy(x => x.Id == Id);
    }

    public void UpdateJob(Job job, User user)
    {
        job.LastUpdatedDate = DateTime.Now;
        job.LastUpdatedBy = user;

        _jobRepository.Update(job);
    }

    public void DeleteJob(Job job)
    {
        _jobRepository.Delete(job);
    }

    public void SaveJob(Job job, User user)
    {
        job.CreatedDate = DateTime.Now;
        job.CreatedBy = user;

        _jobRepository.Insert(job);
    }
}

, , . POCO, POCO. -. , , ​​ .

, , . , , , -, , . , EF, , NHibernate. , , , .

:

/// <summary>
/// This interface is implemented by all repositories to ensure implementation of fixed methods.
/// </summary>
/// <typeparam name="TEntity">Main Entity type this repository works on</typeparam>
/// <typeparam name="TKey">Primary key type of the entity</typeparam>
public interface IRepository<TKey, TEntity> : IRepository where TEntity : class, IEntity<TKey>
{
    /// <summary>
    /// Inserts a new entity.
    /// </summary>
    /// <param name="entity">Entity to insert</param>
    TEntity Insert(TEntity entity);

    /// <summary>
    /// Inserts multiple entities.
    /// </summary>
    /// <param name="entities">Entities to insert</param>
    IEnumerable<TEntity> Insert(IEnumerable<TEntity> entities);

    /// <summary>
    /// Updates an existing entity.
    /// </summary>
    /// <param name="entity">Entity</param>
    TEntity Update(TEntity entity);

    /// <summary>
    /// Updates or saves an entity
    /// </summary>
    /// <param name="entity">Entity</param>
    TEntity SaveOrUpdate(TEntity entity);

    /// <summary>
    /// Deletes an entity.
    /// </summary>
    /// <param name="id">Id of the entity</param>
    bool Delete(TKey id);

    /// <summary>
    /// Deletes an entity.
    /// </summary>
    /// <param name="entity">Entity to be deleted</param>
    bool Delete(TEntity entity);

    /// <summary>
    /// Deletes an entity.
    /// </summary>
    /// <param name="entities">Entities to be deleted</param>
    bool Delete(IEnumerable<TEntity> entities);

    /// <summary>
    /// Used to get an IQueryable that is used to retrieve entities from entire table.
    /// </summary>
    /// <returns>IQueryable to be used to select entities from database</returns>
    IQueryable<TEntity> All();

    /// <summary>
    /// Gets an entity.
    /// </summary>
    /// <param name="expression">LINQ expression used to evaluate and find an entity</param>
    /// <returns>Entity</returns>
    TEntity FindBy(Expression<Func<TEntity, bool>> expression);

    /// <summary>
    /// Used to get an IQueryable that is used to retrieve entities from evaluated LINQ expression.
    /// </summary>
    /// <param name="expression">LINQ expression used to evaluate and find entities</param>
    /// <returns>IQueryable to be used to select entities from database</returns>
    IQueryable<TEntity> FilterBy(Expression<Func<TEntity, bool>> expression);

    /// <summary>
    /// Gets an entity.
    /// </summary>
    /// <param name="id">Primary key of the entity to get</param>
    /// <returns>Entity</returns>
    TEntity FindBy(TKey id);
}

IJobRepository:

// We create separate repositories inheriting from IRepository in case we need specific repository methods for that entity
public interface IJobRepository : IRepository<Guid, Job>
{
}

, , , , EF DbSet DbContext .

-. , . // ..

, Inversion of Control (IoC) . Castle Windsor, IoC.

+4

All Articles