0

Roughly following some designs I've seen, I'm building an ASP.NET application where each of my business objects has an associated Repository and Service. The repositories use nHibernate's ISession to perform CRUD operations, and the corresponding service accesses the repository members.

When using an ObjectDataSource in ASP.NET, is it considered bad practice to bind it directly to a repository instead of to a service (thus entirely skipping the service layer)? Or is the service layer not really necessary when just performing simple CRUD operations?

// Repository interface
public partial interface IRepository<T>
{
    void Add(T entity);
    void Update(T entity);
    void Remove(T entity);
    ICollection<T> GetAll();
    T GetByKey(int _ID);
}

// Service example
public class TestService : IService<Test>
{
    private static TestRepository _repository;
    ...

    public virtual ICollection<Test> FindAll()
    {
        return (_repository.GetAll());
    }

    public virtual Test FindBy(int Id)
    {
        return (_repository.GetByKey(Id));
    }

    public virtual void Save(Test entity)
    {
        _repository.Update(entity);
    }

    public virtual void Add(Test entity)
    {
        _repository.Add(entity);
    }

    public virtual void Remove(Test entity)
    {
        _repository.Remove(entity);
    }
}
4

1 回答 1

0

绝对你的 UI 不应该知道数据访问。即使您没有处理来自数据库的数据,最好至少再多一层(服务或业务层)并让您的 UI 与服务交互。如果我是你,我会为 Asp.net 做一个穷人 MVC,这样我就可以进行单元测试

于 2013-01-25T00:03:50.327 回答