开发者

Where putting logic to Approval user?

In my application got:

Classes

public class User
{
    [Key]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public bool IsApproved { get; set; }
}

public class DataContext : DbContext
{
     DbSet<User> Users { get; set; }
}

public class Repository
{
    DataContext db = new DataContext();

    public bool ApproveUser(User usr) //This is correct place?
    {
        usr.IsApproved = true;
        db.Attrach(usr);
        retur开发者_如何学JAVAn db.SaveChanges() > 0;
    }
}

Question

Where putting logic approval user?

In Repository? In own class?

I ask this because today is the repository and am having trouble to test this once approval is the logic of production in the repository in the repository and not fake.


Repository is the place to write data access. User approval is more likely to be business process, so it better be separated from data access. I would do it this way (code below is more like of pseudocode, not the full production-ready stuff)

public interface IUserRepository
{
    bool Save();
}

public class UserRepository : IUserRepository
{
    public bool Save(User user)
    {
        db.Attrach(user);
        return db.SaveChanges() > 0;
    }
}

public interface IUserService
{
   bool Approve(User user);
}

public class UserService : IUserService
{
   readonly IUserRepository _userRepository;

   public UserService(IUserRepository userRepository)
   { 
      _userRepository = userRepository;
   }

   public bool Approve(User user)
   {
     user.IsApproved = true;
     return _repository.Save(User user);
   }
}

And now, this already is the testable code

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜