0

I am looking at a framework that put different interfaces to each services such as:

public interface ICategoryService
{
    Category GetCategoryById(int categoryId);
    void InsertCategory(Category category);
    void UpdateCategory(Category category);
    void DeleteCategory(Category category);

    IPagedList<Category> GetAllCategories(string categoryName = "",
        int pageIndex = 0, int pageSize = int.MaxValue, bool showHidden = false);

    ...
}

public class CategoryService : ICategoryService
{
    public virtual void DeleteCategory(Category category)
    {
        if (category == null)
            throw new ArgumentNullException("category");

        category.Deleted = true;
        UpdateCategory(category);
    }

    public virtual void InsertCategory(Category category)
    {
        if (category == null)
            throw new ArgumentNullException("category");

        _categoryRepository.Insert(category);
    }

    public virtual void UpdateCategory(Category category)
    {
        if (category == null)
            throw new ArgumentNullException("category");

        var parentCategory = GetCategoryById(category.ParentCategoryId);
        while (parentCategory != null)
        {
            if (category.Id == parentCategory.Id)
            {
                category.ParentCategoryId = 0;
                break;
            }
            parentCategory = GetCategoryById(parentCategory.ParentCategoryId);
        }

        _categoryRepository.Update(category);

    }

    ...
}

Each entity has one repository that does CRUD operations and also there are some Services that is using these repositories to do some business operations. They also use and IOC container to resolve these dependencies.

When I looked at the services, they have some similar methods and same implementations. So why they dont use an abstract base service and inherit from it to do these similar operations ?

public abstract class BaseService<T> where T : BaseEntity
{
    void Insert(T t)
    {
         //do some implementation
    }

    void Update(T t)
    {
         //do some implementation
    }

    ...
}

While we take advantage of using Interface for testing and abstraction purpose, dont we put too much effort for it ? Is there an better way to implement this approach ?

4

0 回答 0