我应该将控制器业务逻辑放在MVC3中的什么位置

问题描述

| 我了解MVC就是将事物放置在正确的位置以及它应该放置的逻辑位置。我的控制器操作已经充满了业务逻辑(与数据存储无关),我觉得我应该开始将一些逻辑移到另一个地方。 我在哪里放置此逻辑是否有约定?例如,我在controllers文件中有以下控制器:
adminPowerController 

  public ActionResult Create(string test1)
    // business logic
    // business logic
    // business logic
    return View();
  }
  public ActionResult Index(string test1)
    // business logic
    // business logic
    // business logic
    return View();
  }
    

解决方法

        建议将业务逻辑放入服务层。因此,您可以定义一个代表业务操作的接口:
public interface IMyService
{
    DomainModel SomeOperation(string input);
}
然后执行此服务。最终,控制器将使用它:
public class MyController: Controller
{
    private readonly IMyService _service;
    public class MyController(IMyService service)
    {
        _service = service;
    }

    public ActionResult Create(string input)
    {
        var model = _service.SomeOperation(input);
        var viewModel = Mapper.Map<DomainModel,ViewModel>(model);
        return View(viewModel);
    }
}
并配置您的DI框架,以将服务的正确实现传递到控制器中。 备注:在我提供的示例中,我使用了AutoMapper在域模型和视图模型之间转换,该模型传递给视图。     ,        我在MVC项目中倾向于做的是在操作之外保留尽可能多的业务逻辑,以便我可以对其进行测试 在某些情况下,我创建一个服务层,然后使用它
public class QuizRunner : IQuizRunner
{
    private readonly IServiceProxyclient _quizServiceProxy;
    public QuizRunner(IServiceProxyclient quizServiceProxy)
    {
        _quizServiceProxy = quizServiceProxy;
    }

    public GameCategory GetPrizeGameCategory(int prizeId)
    {
        return _quizServiceProxy.GetGameCategoryForPrizeId(prizeId);
    }

}

public interface IQuizRunner
{
    GameCategory GetPrizeGameCategory(int prizeId);
}



private IQuizRunner_serviceClass;

public AdminPowercontroller(IQuizRunner serviceClass)
{
    _serviceClass = serviceClass;
}


public ActionResult Create(string test1)
    var itemsFromLogic = _serviceClass.Method1();
    return View();
}
public ActionResult Index(string test1)
    var gameCategory = _serviceClass.GetPrizeGameCategory(test1);
    var viewModel = Mapper.Map<GameCategory,GameCategoryViewModel>(gameCategory);
    return View(viewModel);
}
这使我的操作可以与服务层分开进行测试,而无需依赖 希望这可以帮助 保罗     ,        业务逻辑应存在于与MVC框架和其他内容分开的域模型中。 真实的例子... 应用程序(我的域实体之一)控制器:
[HttpPost]
public ActionResult Withdraw(int applicationId){
  //find it from repository or whatever
  var app=FindApplication(applicationId);
  //force it do do stuff
  a.Withdraw();
  //send back some response
  return RedirectToAction(\"Application\",new{applicationId});
}
应用实体本身:
public class Application{
 public void Withdraw(){
  //check if current user is authorized to withdraw applications
  Authorize<CanWithdrawApplications>();
  //check if application itself can be withdrawn
  ThrowIf(!CanBeWithdrawn(),\"Application can\'t be withdrawn.\");
  //apply state changes
  IsWithdrawn=true;
  //raise domain event
  Raise(new Withdrawn(this));
 }
 public bool CanBeWithdrawn(){
   return !IsWithdrawn && !Project.Contract.IsSigned;
 }
}
有关此的更多信息,您可能需要查看什么是域驱动的设计。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...