【问题标题】:Binding ObjectDataSource to service or repository?将 ObjectDataSource 绑定到服务或存储库?
【发布时间】:2013-01-08 21:40:33
【问题描述】:

大致遵循我见过的一些设计,我正在构建一个 ASP.NET 应用程序,其中我的每个业务对象都有一个关联的存储库和服务。仓库使用nHibernate的ISession进行CRUD操作,对应的服务访问仓库成员。

在 ASP.NET 中使用 ObjectDataSource 时,将其直接绑定到存储库而不是服务(因此完全跳过服务层)是否被认为是不好的做法?或者只是执行简单的 CRUD 操作时,服务层是否真的不需要?

// 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);
    }
}

【问题讨论】:

    标签: c# asp.net repository service-layer


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-05-09
      • 1970-01-01
      • 2016-12-15
      • 2013-02-20
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多