想将所有业务逻辑移至BLL层,但需要传入formcollection,这样干净吗?
我的 asp.net 控制器操作将 FormCollection 作为参数。
然后,我循环遍历我自己的集合,在表单集合中查找值等。
我将所有代码移到我的业务逻辑层,然后在我的操作中调用我的业务逻辑层,例如:
SomeManager.Update(formCollection);
但我感觉有点疲倦将表单集合传递到我的业务逻辑层。
我真的有选择吗?还有其他想法吗?
还是完全没问题?
My asp.net controllers action takes the FormCollection as a parameter.
I then loop through my own collection, do a lookup in the form collection for a value etc.
I moved all my code to my business logic layer, and just call my business logic layer in my action like:
SomeManager.Update(formCollection);
But I am feeling a bit weary passing in a formcollection to my business logic layer.
Do I really have a choice here? any other ideas?
or is it perfectly fine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的业务逻辑层实际上是您的域模型,并且域模型应该以与技术无关的方式表达;即不依赖于任何特定技术,例如 ASP.NET MVC、WPF、WCF、EF、NHibernate 等。
最好将域模型表示为 POCO(普通旧 CLR 对象),但是,正如您已经怀疑的那样,这排除了 FormCollections 和其他 ASP.NET MVC 特定类型。
如果您能够将 Update 方法表示为将域对象列表作为输入的方法,那就更好了。
这意味着您需要先将 FormCollection 转换为强类型域对象,然后再将其传递给更新方法。你需要一个映射器来做到这一点。您可以编写自己的自定义映射器,也可以使用 AutoMapper 来实现此目的。
You Business Logic Layer is really your Domain Model, and a Domain Model should be expressed in a technology-agnostic way; i.e. without dependencies on any particular technologies such as ASP.NET MVC, WPF, WCF, EF, NHibernate or whatnot.
It's best if you can express your Domain Model as POCOs (Plain Old CLR Objects), but, as you are already suspecting, this precludes FormCollections and other ASP.NET MVC-specific types.
It would be better if you were able to express the Update method as a method that takes a list of Domain Objects as input.
This would mean that you need to translate the FormCollection to strongly typed Domain Objects before you pass it on to the update method. You would need a Mapper to do that. You can either write your own custom mapper or use AutoMapper for this.
经验法则是,您不应在业务层或更低层中引用 System.Web.XXX 中的任何内容。在这种情况下,您可以使用自定义模型绑定器来避免使用 FormCollection。
有关创建模型绑定程序的更多信息,请参阅这篇文章。
The rule of thumb is that you shouldn't reference anything from System.Web.XXX in your business layer(s) or lower. In this case you could rather use custom model binders to get away from using FormCollection.
See this post for more on creating model binders.
为什么不创建某种 dto(数据传输对象),然后填充并传递它。会干净一点。
why not create some kind of dto (data transfer object) then populate and then pass that through. it would be a little cleaner.
我将创建一个映射器类,它采用 FormCollection 并返回一个实例或类的更新实例。
ManagerMapper.Create(formcollection)
或者
ManagerMapper.Update(SomeManager, formcollection)
I would create a mapper class that takes a FormCollection and returns an instance, or updated instance of a class.
ManagerMapper.Create(formcollection)
or
ManagerMapper.Update(SomeManager, formcollection)