开发者

Where putting logic to Approval user?

开发者 https://www.devze.com 2023-04-11 10:26 出处:网络
In my application got: Classes public class User { [Key] public Guid Id { get; set; } public string Name { get; set; }

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

精彩评论

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

关注公众号