服务.请,需要建议. [英] Service. Please, need advice.

查看:66
本文介绍了服务.请,需要建议.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个用工作单元模式(ISession)实现的UserRepository.

然后我创建了一个名为Membership Service的服务.
服务基本上不仅可以创建/删除/更新用户,还可以使用FormsAuthentication进行身份验证,验证,登录和注销:

Hello,

I have a UserRepository implemented with Unit Of Work pattern (ISession).

Then I am creating a Service named Membership Service.
This service basically not only Create/Delete/Update users but also Authenticates, Validates, SignIn and SignOut using FormsAuthentication:

  public class MembershipService : IMembershipService {

    public void Approve(Guid id) {
      ISession session = new AppContext();
      UserRepository repository = new UserRepository(session);
      User user = repository.GetById(id);
      user.Approved = true;
      session.Commit();
    } // Approve

    public void Update(User user) {
      ISession session = new AppContext();
      UserRepository repository = new UserRepository(session);
      User _user = repository.GetById(user.Id);
      _user = user;
      session.Commit();
    } // Update

    public void SignOut() {
      FormsAuthentication.SignOut();
    } // SignOut
  }



我遇到一些问题,希望有人可以帮助我:
1.服务及其方法应该是静态的吗?
通常,我将存储库与非静态关联,并且服务到静态;

2.在大多数服务方法中,我拥有:
ISession会话= new AppContext();
UserRepository存储库= new UserRepository(session);
_ User = repository.GetById(user.Id);
我应该在我的服务上输入一些信息吗?
我认为我在实现中缺少了一些东西.

3.更新方法似乎不太正确.通过具有:
_user =用户;
我认为只应更新已更改的属性.与我的服务一起使用.

如果有帮助,我正在发布UserRepository和UnitOfWork的一些代码:



I have a few problems that I hope someone could help me:
1. Should the service and its methods be static?
    Usually I associate a repository to not static and a service to static;

2. In most of the service methods I have:
    ISession session = new AppContext();
    UserRepository repository = new UserRepository(session);
    User _user = repository.GetById(user.Id);
   
    Should I have some input on my service?
    I think I am missing something on my implementation.
  
3. The update method does not seem very correct. By having:
    _user = user;
    I think only the properties that are changed should be updated.
    In this case I think my UnitOfWork Repository is not in "Sync" with my Service.

If it helps I am posting some of the code of my UserRepository and UnitOfWork:

  public interface ISession {   
    void Commit();
    void Rollback();
  } // ISession

  public partial class AppContext : ISession {
    public void Commit() {
      SaveChanges();
    } // Commit
    public void Rollback() {
      Dispose();
    } // Rollback
  }


注意:AppContext继承了ObjectContext,它是由Entity Framework生成的.


NOTE: AppContext inherits ObjectContext and it is generated by Entity Framework.

  public class UserRepository : IUserRepository {
    private AppContext context;

    public UserRepository(ISession session) {
      if (session == null)
        throw new ArgumentNullException("session");
      context = session as AppContext;
    } // UserRepository

    public void Create(User user) {
      context.AddToUsers(user);
    } // Create

    public User GetById(Guid id) {
      return context.Users.Where(u => u.Id.Equals(id)).SingleOrDefault();
    } // GetById

    // Other methods
}



谢谢,
Miguel



Thank You,
Miguel

推荐答案

 

1)我建议创建一个单例类,但是如果您看看ASP.NET附带的其他提供程序类,则所有方法都是静态的.因此,我建议创建静态方法.如果需要初始化,请在t方法本身中进行此操作(通过检查静态布尔类是否已初始化,否则使用方法进行初始化).

您对存储库的看法是正确的.存储库指向一组数据,这是一个实例.服务更像是帮助程序类(但这取决于您的确切要求).

2)由于您多次调用了代码,因此我将考虑为用户检索创建一个单独的(私有)方法. ,例如:

1) I recommend to create either a singleton class, but if you take a look at the other provider classes that ship with ASP.NET, all methods are static. So, I recommend to create static methods. If you need initialization, do this in thet methods itself (by checking a static bool whether the class is already initializes, otherwise initialize using a method).

You are right about a repository. A repository points to a set of data, which is an instance. A service is more of a helper class (but this depends on your exact requirements).

2) Since you invoke the code several times, I would consider creating a separate (private) method for the user retrieval, for example:

private User GetUser(ISession session, Guid userID)
{
    // Get user repository
    UserRepository repository = new UserRepository(session);
    
    // Return user
    return repository.GetById(userID);
}

3)此代码无效:

User _user = repository.GetById(user.Id);
_user = user;

您在这里所做的是检索用户.然后,您用另一个用户对象引用覆盖该用户的对象引用(换句话说,将被垃圾回收由GetById方法检索的用户).

您应该设置用户属性替换整个对象的引用:

_user.Name = user.Name;
等.

或在对象User上创建一个智能方法来复制所有价值观.仅仅因为User对象始终知道其自己的实现,而您不必更新使用User.CopyValuesIntoOtherObject()方法的代码.

3) This code won't work:

User _user = repository.GetById(user.Id);
_user = user;

What you do here is retrieve a user. Then, you overwrite the object reference of that user with another user object reference (in other words, the user that is retrieved by the GetById method will be garbage collected).

You should set the user properties instead of replace the whole object reference:

_user.Name = user.Name;
etc.

Or create a smart method on the object User that copies all the values. Simply because the User object always knows its own implementation and you don't have to update code that uses the User.CopyValuesIntoOtherObject() method.


这篇关于服务.请,需要建议.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆