簡體   English   中英

在WPF應用程序中管理DbContext

[英]Managing DbContext in WPF Application

我目前正在使用.NET 4.6開發WPF應用程序。 我們正在使用實體框架與SQL Server進行持久性和數據庫交互。 當我加入該項目時,所有數據庫交互都在下面的代碼中完成,如下所示。 (是的,我知道這是不好的做法)

IList<User> users;
using (AppDbContext db = new AppDbContext())
{
    users = db.Users.Where(x => x.Active == true).ToList();
}

// do code to update UI

我基本上說這是一種不好的做法,我們需要從表示層中分離出業務邏輯和查詢。 我以為存儲庫模式可能是一個不錯的起點,但是我對如何管理DbContext表示懷疑。

倉庫接口示例

public interface IUserService : IDisposable
{
    IList<applicationuser> GetUsers();
    IList<AllApplicationUser> GetActiveUsers();
    AllApplicationUser GetUserView(long id);
    applicationuser GetUser(long id);
    void CreateUser(applicationuser user);
    void UpdateUser(applicationuser user);
    void DeleteUser(long id);
    void Save();
    applicationuser Authenticate(string username, string password);
}

並執行

class UserService : IUserService
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        private readonly OMEGAEntities _db;
        private bool _disposed = false;

        public UserService()
        {
            _db = new OMEGAEntities();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _db.Dispose();
                }
            }
            _disposed = true;
        }

        public IList<applicationuser> GetUsers()
        {
            // fetch only active users that do not have the role SuperAdmin
            return _db.applicationusers.Where(u => u.roleid != 1 && u.activeflag == true).ToList();
        }

        public IList<AllApplicationUser> GetActiveUsers()
        {
            return _db.AllApplicationUsers.Where(u => u.activeflag == true && u.roleid != 1).ToList();
        }

        public AllApplicationUser GetUserView(long id)
        {
            return _db.AllApplicationUsers.Single(x => x.id == id);
        }

        public applicationuser GetUser(long id)
        {
            return _db.applicationusers.Find(id);
        }

        public void CreateUser(applicationuser user)
        {
            _db.applicationusers.Add(user);
        }

        public void UpdateUser(applicationuser user)
        {
            _db.Entry(user).State = EntityState.Modified;
        }

        public void DeleteUser(long id)
        {
            var user = _db.applicationusers.Find(id);
            _db.applicationusers.Remove(user);
        }

        public void Save()
        {
            _db.SaveChanges();
        }

        public applicationuser Authenticate(string username, string password)
        {
            applicationuser user =
                _db.applicationusers.SingleOrDefault(u => u.loginid.ToLower().Equals(username.ToLower()));

            if (user != null)
            {
                if (Common.Utils.DecryptData(user.password).Equals(password))
                {
                    return user;
                }          
            }

            return null;
        }
    }

現在的問題是,當有多個窗口打開時,有多個打開的上下文和長時間運行的上下文。 目前浮現的想法之一是對整個應用程序使用單個上下文,因為它是單個用戶桌面應用程序,但是這似乎不是最好的想法。 另外,在關閉窗口等情況下,還需要處理dbContext。

我已經了解了在ASP.NET中每個請求使用DbContext的想法。 也許每個窗口可以使用一個?

我只是想知道存儲庫模式是否是WPF應用程序的最佳方法? 我已經看到許多人在ASP.NET中成功使用它。

您可以在存儲庫頂部創建某種工作單元,然后構造器將dbcontext注入存儲庫中

public class UnitOfWork : IUnitOfWork
{
    private IUserService userService;
    private omegaentities dbcontext;
    public UnitOfWork ()
    {
    dbcontext = new Omegaentities ();
    }
    public IUserService UserService {
    get {
             If (userService == null)
             { userService = new UserService(dbcontext);}

    Return userService;
    }

}

那你就打電話

unitOfWork.UserService.GetUsers();

...這提供了更多的抽象層,您仍然需要處理unitofwork的單個實例,但是可以使用一些DI魔術很好地處理它:)

注意:我是從android應用程序編寫的! (在手機上編碼c#不會晃動!)

這實際上取決於您如何使用DbContext。 如果您正在讀取某些數據,而其他應用程序可能會更改這些數據,則建議每次都使用一次性DbContext。 如果您確定您的應用程序是唯一接觸數據庫的應用程序,則在存儲庫中保留一個DbContext並在整個應用程序中使用一個單例存儲庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM