none
求教,想我这样结构的代码,在哪里使用using(....){} 来回收dbdatacontext呢? RRS feed

  • 问题

  • namespace _66motor.Data.Repositories
    {public
    class OnlineUserRepository : _66motor.Data.Repositories.IOnlineUserRepository { private _66motoDBDataContext db; public OnlineUserRepository(_66motoDBDataContext dataContext) { this.db = dataContext; } public onlineuser GetOnlineUser(int userId) { onlineuser user = null; user = db.onlineuser.FirstOrDefault(x => x.userid == userId); return user; } } }

    namespace _66motor.Services
    {public
    class OnlineUsers { private IRepositoriesWrapper Repositories; public void CreateOnlineUser(onlineuser theOnlineUser) { this.Repositories.OnlineUsers.CreateOnlineUser(theOnlineUser); } public onlineuser GetOnlineUser(int userId) { return this.Repositories.OnlineUsers.GetOnlineUser(userId); } public OnlineUsers(IRepositoriesWrapper repos) { this.Repositories = repos; } } }

    public class ForumUtils
     {
      private RepositoriesWrapper repos;
      private ServicesWrapper myServices;
      private static ForumUtils _instance = null;
      private static object lockHelper = new object();
      private ForumUtils() {
       repos = new RepositoriesWrapper(new _66motoDBDataContext());
       myServices = new ServicesWrapper(repos);
      }
    
      
      public static ForumUtils getInstance() {
       if (_instance == null)
       {
        lock (lockHelper)
        {
         if (_instance == null)
         {
          _instance = new ForumUtils();
         }
        }
        
       }
    
       return _instance;
      }
    
    
     public bool IsOnline(user currentUser)
      {
       if (currentUser.invisible)
        return false;
    
       //RepositoriesWrapper repos = new RepositoriesWrapper(new _66motoDBDataContext());
       //var myServices = new ServicesWrapper(repos);
    
       return myServices.OnlineUsers.IsOnline(currentUser.userid);
      }
    
     }
    }
    


    最终调用的是

     

     

    ForumUtils.getInstance().IsOnline(Model)
    

    问题是老师出现如帖子:

     

    http://social.msdn.microsoft.com/Forums/zh-CN/adonetzhchs/thread/51bbc8ac-68eb-43c7-853d-f15931dc5365

    里提到的问题

    实在不知道怎么办了。求各位版主大人帮忙看下

     


    2011年6月17日 2:15

答案

全部回复

  • Hi

    Try edit like this:

     

    namespace _66motor.Data.Repositories

    {

    public class OnlineUserRepository :_66motor.Data.Repositories.IOnlineUserRepository

     { 

     

       public onlineuser GetOnlineUser(int userId)

      {

         _66motoDBDataContext db=new _66motoDBDataContext();

     

         return db.onlineuser.FirstOrDefault(x => x.userid == userId);

      }

    }

    }


    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    2011年6月17日 5:56
  • 为 OnlineUserRepository 实现 IDispoable接口
    2011年6月17日 8:53