开发者

Passing HttpFileCollectionBase to the Business Layer - Bad?

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:

开发者_如何学C
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:

  1. the web project handling this, and my controllers getting fatter
  2. mapping the HttpFileCollectionBase to something my business layer likes
  3. 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜