将 HttpFileCollectionBase 传递到业务层 - 不好?
希望有一个简单的解决方案来解决这个问题。
我有我的 MVC2 项目,它允许上传某些表单上的文件。我正在努力保持控制器的精简,并处理此类事情的业务层内的处理。
也就是说,HttpFileCollectionBase 显然位于 System.Web 程序集中。
理想情况下,我想调用类似的东西:
UserService.SaveEvidenceFiles(MyUser user, HttpFileCollectionBase files);
或类似的东西,并让我的业务层处理这些东西保存方式和位置的逻辑。
但是,在关注点分离等方面让我的模型层引用 System.Web 感觉有点恶心。
因此,我们有(据我所知)一些选择:
- 处理此问题的 Web 项目,以及我的控制器变得越来越胖,
- 将 HttpFileCollectionBase 映射到我的业务层喜欢
- 传递集合的东西,并接受我从我的业务项目引用 System.Web
希望在这里得到一些关于此类事情的最佳实践方法的反馈 - 即使不是专门在上述的上下文。
hopefully there's an easy solution to this one.
I have my MVC2 project which allows uploads of files on certain forms. I'm trying to keep my controllers lean, and handle the processing within the business layer of this sort of thing.
That said, HttpFileCollectionBase is obviously in the System.Web assembly.
Ideally I want to call to something like:
UserService.SaveEvidenceFiles(MyUser user, HttpFileCollectionBase files);
or something similar and have my business layer handle the logic of how and where these things are saved.
But, it feels a little icky to have my models layer with a reference to System.Web in terms of separation of concerns etc.
So, we have (that I'm aware of) a few options:
- the web project handling this, and my controllers getting fatter
- mapping the HttpFileCollectionBase to something my business layer likes
- passing the collection through, and accepting that I reference System.Web from my business project
Would love some feedback here on best practice approaches to this sort of thing - even if not specifically within the context of the above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想将逻辑保留在控制器之外,我只需使用自定义模型绑定器 - 然后您可以将提取的数据粘贴到模型中,您可以使用该模型执行您想要的操作。
http://www.heartysoft.com/post/2010/05/06/ASPNET-MVC-ModelBinding-Multiple-File-Uploads-to-an-Array.aspx
说明 -我认为模型绑定器和其他拦截技术是将逻辑从控制器中分离出来并分成可单独维护的容器的便捷方法 - 这是基础设施代码/Web 逻辑,因此不属于业务逻辑,因此可以愉快地引用 System.Web 等。
说明 - 实际上,这是您的选项 2 的变相,您使用模型绑定器将不友好的数据映射到代码的其余部分可以识别的内容,但您这样做的方式不会损害您的清洁控制器。
I'd just use a custom model binder if you want to keep the logic out of your controllers - then you can stick the extracted data into the model which you can do what you want with.
http://www.heartysoft.com/post/2010/05/06/ASPNET-MVC-ModelBinding-Multiple-File-Uploads-to-an-Array.aspx
Clarification - I consider model binders and other interception techniques to be a convenient way of splitting logic out of controllers and into separately maintainable containers - this is infrastructure code/web logic and thus does not belong in the business logic and thus can reference System.Web etc happily.
Clarification - Effectively this is your Option 2 in disguise, you use model binders to map the unfriendly data into something the rest of your code can recognise, but you do it in a way that doesn't compromise your clean controllers.
假设您无法轻松模拟 HttpFileCollectionBase 您可以传入类似 Dictionary的内容其中 MyFile 包装 HttpPostedFile。如果 HttpFileCollectionBase 可以轻松地模拟单元测试,我认为没有任何意义。
assuming you can't easily mock up HttpFileCollectionBase you could pass in something like Dictionary<String, MyFile> where MyFile wraps HttpPostedFile. if HttpFileCollectionBase can easily be mockable for unit tests i see no point.