0

コントローラー自体に記述するのではなく、models フォルダー内の別のクラス ファイルに EF クエリを記述しても問題ありませんか。

msdn ですべての EF クエリをコントローラー自体に記述しているのを見たからです。同時に、そのコントローラーが短いことを意図していたら、msdn も読みました。

モデルを使用して、次のアプローチを使用します。

コントローラーで:

        public JsonResult SaveStorageLocation(StorageLocations objStorageLocation)
        {
            int Result = 0;
            try
            {
                StorageLocationModel objStorageLocationModel = new StorageLocationModel();
                if (objStorageLocation.Id == Guid.Empty)
                {
                    Result = objStorageLocationModel.AddStorageLocation(objStorageLocation, SessionUserId);
                }
                else
                {
                    Result = objStorageLocationModel.UpdateStorageLocation(objStorageLocation, SessionUserId);
                }
            }
            catch (Exception ex)
            {
                Result = (int)MethodStatus.Error;
            }
            return Json(Result, JsonRequestBehavior.AllowGet);
        }

モデル クラス:

public int AddStorageLocation(StorageLocations objStorageLocation, Guid CreatedBy)
        {
            MethodStatus Result = MethodStatus.None;
            int DuplicateRecordCount = db.StorageLocations.Where(x => x.Location.Trim().ToLower() == objStorageLocation.Location.Trim().ToLower()).Count();
            if (DuplicateRecordCount == 0)
            {
                objStorageLocation.Id = Guid.NewGuid();
                objStorageLocation.CreatedBy = CreatedBy;
                objStorageLocation.CreatedOn = DateTime.Now;
                objStorageLocation.ModifiedBy = CreatedBy;
                objStorageLocation.ModifiedOn = DateTime.Now;
                objStorageLocation.Status = (int)RecordStatus.Active;
                db.StorageLocations.Add(objStorageLocation);
                db.SaveChanges();
                Result = MethodStatus.Success;
            }
            else
            {
                Result = MethodStatus.MemberDuplicateFound;
            }
            return Convert.ToInt32(Result);
        }

        public int UpdateStorageLocation(StorageLocations objStorageLocationNewDetails, Guid ModifiedBy)
        {
            MethodStatus Result = MethodStatus.None;
            int DuplicateRecordCount = 
                db.StorageLocations.
                Where(x => x.Location == objStorageLocationNewDetails.Location && 
                x.Id != objStorageLocationNewDetails.Id).Count();
            if (DuplicateRecordCount == 0)
            {
                StorageLocations objStorageLocationExistingDetails = db.StorageLocations.Where(x => x.Id == objStorageLocationNewDetails.Id).FirstOrDefault();
                if (objStorageLocationExistingDetails != null)
                {
                    objStorageLocationExistingDetails.Location = objStorageLocationNewDetails.Location;
                    objStorageLocationExistingDetails.ModifiedBy = ModifiedBy;
                    objStorageLocationExistingDetails.ModifiedOn = DateTime.Now;
                    objStorageLocationExistingDetails.Status = (int)RecordStatus.Active;
                    db.SaveChanges();
                    Result = MethodStatus.Success;
                }
            }
            else
            {
                Result = MethodStatus.MemberDuplicateFound;
            }
            return Convert.ToInt32(Result);
        }

それとも、コントローラー自体にすべてのコードを記述したほうがよいのでしょうか?

4

2 に答える 2