コード例 #1
0
        private void MainForm_Load(object sender, System.EventArgs e)
        {
            Dal.UnitOfWork unitOfWork = null;

            try
            {
                unitOfWork =
                    new Dal.UnitOfWork();

                Models.User user = null;

                System.Guid sId =
                    new System.Guid
                        ("a1a57f49-51fc-4c4b-8f06-19cd117d5c9e");

                // **************************************************
                // Get Data
                // **************************************************
                //var users =
                //	unitOfWork.UserRepository
                //	.Get()
                //	.ToList()
                //	;

                //var users =
                //	unitOfWork.UserRepository
                //	.Get()
                //	.Where(current => current.IsActive)
                //	.ToList()
                //	;

                //var users =
                //	unitOfWork.UserRepository
                //	.GetActiveUsers()
                //	.ToList()
                //	;

                var users =
                    unitOfWork.UserRepository
                    .GetActiveUsers()
                ;
                // **************************************************
                // /Get Data
                // **************************************************

                // **************************************************
                // Add New User
                // **************************************************
                unitOfWork =
                    new Dal.UnitOfWork();

                user = new Models.User();

                user.Id       = sId;
                user.IsActive = true;
                user.Username = "******";

                unitOfWork.UserRepository.Insert(user);

                unitOfWork.Save();
                unitOfWork.Dispose();
                unitOfWork = null;
                // **************************************************
                // /Add New User
                // **************************************************

                // **************************************************
                // Get And Update
                // **************************************************
                unitOfWork =
                    new Dal.UnitOfWork();

                user =
                    unitOfWork.UserRepository
                    .GetById(sId);

                if (user != null)
                {
                    user.IsActive = !user.IsActive;

                    unitOfWork.UserRepository.Update(user);

                    unitOfWork.Save();
                }

                unitOfWork.Dispose();
                unitOfWork = null;
                // **************************************************
                // /Get And Update
                // **************************************************

                // **************************************************
                // Just Update
                // **************************************************
                unitOfWork =
                    new Dal.UnitOfWork();

                user = new Models.User();

                user.Id       = sId;
                user.Username = "******";
                user.IsActive = !user.IsActive;

                unitOfWork.UserRepository.Update(user);

                unitOfWork.Save();
                unitOfWork.Dispose();
                unitOfWork = null;
                // **************************************************
                // /Just Update
                // **************************************************

                // **************************************************
                // Get And Delete
                // **************************************************
                unitOfWork =
                    new Dal.UnitOfWork();

                user =
                    unitOfWork.UserRepository
                    .GetById(sId);

                if (user != null)
                {
                    unitOfWork.UserRepository.Delete(user);

                    unitOfWork.Save();
                }

                unitOfWork.Dispose();
                unitOfWork = null;
                // **************************************************
                // /Get And Delete
                // **************************************************

                // **************************************************
                // Add Again New User
                // **************************************************
                unitOfWork =
                    new Dal.UnitOfWork();

                user = new Models.User();

                user.Id       = sId;
                user.IsActive = true;
                user.Username = "******";

                unitOfWork.UserRepository.Insert(user);

                unitOfWork.Save();
                unitOfWork.Dispose();
                unitOfWork = null;
                // **************************************************
                // Add New User
                // **************************************************

                // **************************************************
                // Just Delete
                // **************************************************
                unitOfWork =
                    new Dal.UnitOfWork();

                user = new Models.User();

                user.Id = sId;

                unitOfWork.UserRepository.Delete(user);

                unitOfWork.Save();
                unitOfWork.Dispose();
                unitOfWork = null;
                // **************************************************
                // /Just Delete
                // **************************************************
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            finally
            {
                if (unitOfWork != null)
                {
                    unitOfWork.Dispose();
                    unitOfWork = null;
                }
            }
        }
        private void MainForm_Load(object sender, System.EventArgs e)
        {
            // **************************************************
            // DatabaseContext - Add New User
            // **************************************************
            {
                Models.DatabaseContext databaseContext = null;

                try
                {
                    databaseContext =
                        new Models.DatabaseContext();

                    Models.User user = new Models.User
                    {
                        IsActive = true,
                        Username = "******",
                    };

                    databaseContext.Users.Add(user);

                    databaseContext.SaveChanges();
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (databaseContext != null)
                    {
                        databaseContext.Dispose();
                        databaseContext = null;
                    }
                }
            }
            // **************************************************
            // /DatabaseContext - Add New User
            // **************************************************

            // **************************************************
            // UnitOfWork - Add New User
            // **************************************************
            {
                Dal.UnitOfWork unitOfWork = null;

                try
                {
                    unitOfWork =
                        new Dal.UnitOfWork();

                    Models.User user = new Models.User
                    {
                        IsActive = true,
                        Username = "******",
                    };

                    unitOfWork.UserRepository.Insert(user);

                    unitOfWork.Save();
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (unitOfWork != null)
                    {
                        unitOfWork.Dispose();
                        unitOfWork = null;
                    }
                }
            }
            // **************************************************
            // /UnitOfWork - Add New User
            // **************************************************

            System.Guid id =
                new System.Guid
                    ("a1a57f49-51fc-4c4b-8f06-19cd117d5c9e");

            string username = "******";

            // **************************************************
            // DatabaseContext - Get The User From Database -> Update The User -> Save The User Changes To Database
            // **************************************************
            {
                Models.DatabaseContext databaseContext = null;

                try
                {
                    databaseContext =
                        new Models.DatabaseContext();

                    //Models.User user =
                    //	databaseContext.Users
                    //	.Where(current => current.Id == id)
                    //	.FirstOrDefault();

                    Models.User user =
                        databaseContext.Users
                        .Where(current => string.Compare(current.Username, username, true) == 0)
                        .FirstOrDefault();

                    if (user != null)
                    {
                        user.IsActive = false;
                        user.Username = "******";

                        databaseContext.SaveChanges();
                    }
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (databaseContext != null)
                    {
                        databaseContext.Dispose();
                        databaseContext = null;
                    }
                }
            }
            // **************************************************
            // /DatabaseContext - Get The User From Database -> Update The User -> Save The User Changes To Database
            // **************************************************

            // **************************************************
            // UnitOfWork - Get The User From Database -> Update The User -> Save The User Changes To Database
            // **************************************************
            {
                Dal.UnitOfWork unitOfWork = null;

                try
                {
                    unitOfWork =
                        new Dal.UnitOfWork();

                    //Models.User user =
                    //	unitOfWork.UserRepository.GetById(id);

                    Models.User user =
                        unitOfWork.UserRepository.GetByUsername(username);

                    if (user != null)
                    {
                        user.IsActive = false;
                        user.Username = "******";

                        // نوشتن دستور ذیل الزامی نیست! ولی اگر بنویسیم مشکلی ندارد
                        //unitOfWork.UserRepository.Update(user);

                        unitOfWork.Save();
                    }
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (unitOfWork != null)
                    {
                        unitOfWork.Dispose();
                        unitOfWork = null;
                    }
                }
            }
            // **************************************************
            // /UnitOfWork - Get The User From Database -> Update The User -> Save The User Changes To Database
            // **************************************************

            // **************************************************
            // DatabaseContext - Just Update The User with Id -> Save The User Changes To Database
            // **************************************************
            {
                Models.DatabaseContext databaseContext = null;

                try
                {
                    databaseContext =
                        new Models.DatabaseContext();

                    Models.User user = new Models.User();

                    user.Id       = id;
                    user.IsActive = false;
                    user.Username = "******";

                    databaseContext.Entry(user).State =
                        System.Data.Entity.EntityState.Modified;

                    databaseContext.SaveChanges();
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (databaseContext != null)
                    {
                        databaseContext.Dispose();
                        databaseContext = null;
                    }
                }
            }
            // **************************************************
            // /DatabaseContext - Just Update The User with Id -> Save The User Changes To Database
            // **************************************************

            // **************************************************
            // UnitOfWork - Just Update The User with Id -> Save The User Changes To Database
            // **************************************************
            {
                Dal.UnitOfWork unitOfWork = null;

                try
                {
                    unitOfWork =
                        new Dal.UnitOfWork();

                    Models.User user = new Models.User();

                    user.Id       = id;
                    user.IsActive = false;
                    user.Username = "******";

                    unitOfWork.UserRepository.Update(user);

                    unitOfWork.Save();
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (unitOfWork != null)
                    {
                        unitOfWork.Dispose();
                        unitOfWork = null;
                    }
                }
            }
            // **************************************************
            // /UnitOfWork - Just Update The User with Id -> Save The User Changes To Database
            // **************************************************

            // **************************************************
            // DatabaseContext - Get The User From Database -> Delete The User
            // **************************************************
            {
                Models.DatabaseContext databaseContext = null;

                try
                {
                    databaseContext =
                        new Models.DatabaseContext();

                    //Models.User user =
                    //	databaseContext.Users
                    //	.Where(current => current.Id == id)
                    //	.FirstOrDefault();

                    Models.User user =
                        databaseContext.Users
                        .Where(current => string.Compare(current.Username, username, true) == 0)
                        .FirstOrDefault();

                    if (user != null)
                    {
                        databaseContext.Users.Remove(user);

                        databaseContext.SaveChanges();
                    }
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (databaseContext != null)
                    {
                        databaseContext.Dispose();
                        databaseContext = null;
                    }
                }
            }
            // **************************************************
            // /DatabaseContext - Get The User From Database -> Delete The User
            // **************************************************

            // **************************************************
            // UnitOfWork - Get The User From Database -> Delete The User
            // **************************************************
            {
                Dal.UnitOfWork unitOfWork = null;

                try
                {
                    unitOfWork =
                        new Dal.UnitOfWork();

                    //Models.User user =
                    //	unitOfWork.UserRepository.GetById(id);

                    Models.User user =
                        unitOfWork.UserRepository.GetByUsername(username);

                    if (user != null)
                    {
                        unitOfWork.UserRepository.Delete(user);

                        unitOfWork.Save();
                    }
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (unitOfWork != null)
                    {
                        unitOfWork.Dispose();
                        unitOfWork = null;
                    }
                }
            }
            // **************************************************
            // /UnitOfWork - Get The User From Database -> Delete The User
            // **************************************************

            // **************************************************
            // DatabaseContext - Just Delete The User with Id
            // **************************************************
            {
                Models.DatabaseContext databaseContext = null;

                try
                {
                    databaseContext =
                        new Models.DatabaseContext();

                    Models.User user = new Models.User();

                    user.Id = id;

                    databaseContext.Entry(user).State =
                        System.Data.Entity.EntityState.Deleted;

                    databaseContext.SaveChanges();
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (databaseContext != null)
                    {
                        databaseContext.Dispose();
                        databaseContext = null;
                    }
                }
            }
            // **************************************************
            // /DatabaseContext - Just Delete The User with Id
            // **************************************************

            // **************************************************
            // UnitOfWork - Just Delete The User with Id
            // **************************************************
            {
                Dal.UnitOfWork unitOfWork = null;

                try
                {
                    unitOfWork =
                        new Dal.UnitOfWork();

                    Models.User user = new Models.User();

                    user.Id = id;

                    unitOfWork.UserRepository.Delete(user);

                    unitOfWork.Save();
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (unitOfWork != null)
                    {
                        unitOfWork.Dispose();
                        unitOfWork = null;
                    }
                }
            }
            // **************************************************
            // /UnitOfWork - Just Delete The User with Id
            // **************************************************

            // **************************************************
            // DatabaseContext - Get Users For Filling DropDownList
            // **************************************************
            {
                Models.DatabaseContext databaseContext = null;

                try
                {
                    databaseContext =
                        new Models.DatabaseContext();

                    //var users =
                    //	databaseContext.Users
                    //	.ToList()
                    //	;

                    //var users =
                    //	databaseContext.Users
                    //	.OrderBy(current => current.Username)
                    //	.ToList()
                    //	;

                    var users =
                        databaseContext.Users
                        .Where(current => current.IsActive)
                        .OrderBy(current => current.Username)
                        .ToList()
                    ;

                    //var users =
                    //	databaseContext.Users
                    //	.Where(current => current.IsActive)
                    //	.Where(current => current.IsDeleted == false)
                    //	.OrderBy(current => current.Username)
                    //	.ToList()
                    //	;
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (databaseContext != null)
                    {
                        databaseContext.Dispose();
                        databaseContext = null;
                    }
                }
            }
            // **************************************************
            // /DatabaseContext - Get Users For Filling DropDownList
            // **************************************************

            // **************************************************
            // UnitOfWork - Get Users For Filling DropDownList
            // **************************************************
            {
                Dal.UnitOfWork unitOfWork = null;

                try
                {
                    unitOfWork =
                        new Dal.UnitOfWork();

                    var users =
                        unitOfWork.UserRepository.GetActiveUsers();
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (unitOfWork != null)
                    {
                        unitOfWork.Dispose();
                        unitOfWork = null;
                    }
                }
            }
            // **************************************************
            // /UnitOfWork - Get Users For Filling DropDownList
            // **************************************************
        }