MVC Validation - Keep it DRY with a service layer - What is best practice?

前端 未结 6 991
栀梦
栀梦 2020-12-29 08:23

I am trying to adhere to best multi-layer design practices, and don\'t want my MVC controller to interact with my DAL (or any IRepository for that matter). It must go throug

6条回答
  •  被撕碎了的回忆
    2020-12-29 08:43

    The issue here is validation in the service layer but how to get that information 'back up' to the web app. Something similar we discussed a bit back since the idea of dependency injection clearly comes into play here if a service is validating, you can't sinply call the service in the Model (for instance if IValidateableObject is implemented there you dont want to call the service directly)

    The approach taken was:

    Option 3: I didn't know about this earlier, but what appears to be a very powerful way to write validators is to use the ModelValidator class and a corresponding ModelValidatorProvider.

    ASP.NET MVC 3: Validating model when information external to the model is required

    So basically you are injecting a validator (which would then be in your service layer) to be resolved by mvc without a need to an explicit service locator call.

提交回复
热议问题