【问题标题】:IService extends IRepository correct?IService 扩展了 IRepository 正确吗?
【发布时间】:2011-10-10 16:58:37
【问题描述】:

说我的IService拥有IRepository所拥有的一切,以及更多一些具体的操作是正确的吗?

以下是代码:

public interface IRepository<T>
{
    T Add(T Entity);
    T Remove(T Entity);
    IQueryable<T> GetAll();
}

public interface IUserService
{

    //All operations IRepository
    User Add(User Entity);
    User Remove(User Entity);
    IQueryable<User> GetAll();

    //Others specific operations 
    bool Approve(User usr);
}

注意IRepository中的所有操作也是IService。

这是正确的吗?

如果是这样,最好这样做:

public interface IUserService : IRepository<User>
{
    bool Approve(User usr);
}

另一种选择是:

public interface IUserService
{
    IRepository<User> Repository { get; }

    //All operations IRepository
    User Add(User Entity);
    User Remove(User Entity);
    IQueryable<User> GetAll();

    //Others specific operations 
    bool Approve(User usr);
}

public class UserService : IUserService
{
    private readonly IRepository<User> _repository;
    public IRepository<User> Repository
    {
        get
        {
            return _repository;
        }
    }

    //Others specific operations 
    public bool Approve(User usr) { ... }
}

请注意,我将存储库作为属性,并在我的服务类中公开此属性。

因此,如果您需要在存储库中添加、删除或获取某些对象,我可以通过此属性访问它。

你有什么看法? 这样做是否正确?

【问题讨论】:

  • 为防止您的问题被标记或删除,请避免在......每个答案都同样有效的情况下提出主观问题:“你最喜欢的______是什么?”您的答案与问题一起提供,并且您期望得到更多答案:“我用 ______ 表示 ______,你用什么?”没有要解决的实际问题:“我很好奇其他人是否和我一样。”我们被问到一个开放式的假设性问题:“如果 ______ 发生了怎么办?”这是一个伪装成问题的咆哮:“______糟透了,我说的对吗?”
  • 也许我不知道如何提问。我想知道的是如何公开我的存储库的方法。如果我将它们作为我的服务中的属性公开,或者创建访问存储库方法的方法。
  • @cadrell0:他提供了 3 种可能的做事方式,并询问他应该使用哪一种,不是出于主观原因,而是出于客观原因。
  • 问题的最后一行是“你的意见是什么?”对我来说,这属于“我用_代表_,你用什么?”。我最近在我的工作中经历了这个问题,这是一个需要扩展讨论的好问题。在我看来,它不适合 SO 的问答形式。此问题没有一个正确答案,选择的答案将基于个人喜好。
  • 我明白了,实际上在我的上下文中,将存储库作为属性公开是理想的。如果没有一个正确答案,并且在公开存储库时没有问题,这将是做什么。

标签: c# repository-pattern service-layer


【解决方案1】:

您可能已经自己解决了这个问题,但无论如何我都会提供意见。
你的第二个例子:

public interface IUserService : IRepository<User>
{
    bool Approve(User usr);
}

是你应该使用的——它又好又干净。在您的第一个示例中,IUserService 中包含的大部分内容都是完全多余的,IUserService 实际上添加的唯一内容是bool Approve(User usr)。您还会发现,如果您使用第二个示例,当您添加 UserService 并让 Visual Studio 自动实现 IUserService 时,您最终会得到以下结果:

public class UserService : IUserService
{
    public bool Approve(User usr)
    {
        throw new NotImplementedException();
    }

    public User Add(User Entity)
    {
        throw new NotImplementedException();
    }

    public User Remove(User Entity)
    {
        throw new NotImplementedException();
    }

    public IQueryable<User> GetAll()
    {
        throw new NotImplementedException();
    }
}

public class User { }

public interface IRepository<T>
{
    T Add(T Entity);
    T Remove(T Entity);
    IQueryable<T> GetAll();
}

public interface IUserService : IRepository<User>
{
    bool Approve(User usr);
}

如您所见,类型都为您正确填充,无需在 IUserService 中执行任何额外操作。

【讨论】:

  • 好的,我正在考虑将存储库公开为属性;你怎么看?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 2011-07-05
  • 1970-01-01
相关资源
最近更新 更多