开发者

IService extends IRepository correct?

It is correct to say that my IService has everything that IRepository has, and more some specific operations?

Following is the code:

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

Note that all operations in IRepository are also IService.

Is this correct?

If so, it would be better to do something like this:

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

Another option would be:

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) { ... }
}

Note that I put the repository as a property, and in my service class'm exposing this property.

So if you need to add, remove or get some object in the repository 开发者_StackOverflow中文版I could access it via this property.

What is your opinion? Is correct in doing this?


You've probably already worked this out for yourself, but I will offer an opinion anyway.
Your second example:

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

is what you should use - it is nice and clean. Most of the stuff included in IUserService in your first example was totally redundant, the only thing IUserService actually adds is bool Approve(User usr). You will also find that if you use your second example, when you add the UserService and get Visual Studio to automatically implement IUserService you end up with the following:

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

As you can see, the types are all correctly populated for you, without having to do anything extra in IUserService.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜