0

是否可以将 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

1

我希望您的问题很快就会结束,因为它将被视为基于意见。

除此之外,如果您的控制器中没有查询,则有很多优点。

  • 控制器不应该规定如何访问数据,这是模型的工作。
  • 如果您将模型(或服务或存储库,无论您想将其称为应用程序)作为依赖项注入,那么模拟数据访问会容易得多。
  • 稍后您可能会发现,如果将某些查询迁移到存储过程,则可以更好地处理它们,因为它们处理大量数据。如果控制器不直接访问数据存储,则此更改将更容易进行。您可以在模型类中进行更改并保持相同的接口,或者编写一个新的实现然后注入。
  • 控制器和模型都可以更容易地相互隔离进行测试。
于 2020-01-22T06:37:46.773 回答
1

您希望您的代码始终是可测试的。

永远不要将逻辑放在模型中,将逻辑项放在模型文件夹中会使您的结构变脏,并且更容易失去概览。

您应该使用实现接口的存储库类。在存储库类中,您可以执行 EF 逻辑、捕获数据库异常等。

您的控制器将被注入存储库接口。通过这种方式,您可以将控制器与 EF 逻辑分开测试,因为您可以模拟该接口。您的存储库也可以对其开放功能进行测试。

于 2020-01-22T06:39:02.390 回答