从控制器或服务层调用asp.net Membership 类?
我应该从控制器访问 asp.net 成员资格类并将结果传递到服务层,还是直接从服务层访问它?
我很困惑,因为一方面,这似乎是应该在服务层中处理的业务逻辑,但我不想将服务层绑定到 Web 命名空间,因为这可能会成为一个 Windows 应用程序。
Should I access the asp.net membership class from the controller and pass the results to the service layer, or access it directly from the service layer?
I'm torn because on one hand this seems like business logic that should be handled in the service layer, but I don't want to tie the service layer to the web namespace as this might become an windows app down the road.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案是,使用 IoC 创建服务层使用的成员接口。网站的实现可以使用 web 命名空间。 Windows 应用程序可以有不同的实现。由于您可以注入该依赖项,因此您的服务层不必更改:-)
the answer, use IoC to create a membership interface that the service layer uses. the website's implementation can use the web namespace. And the windows app can have a different implementation. and since you can inject that dependency, your service layer doesn't have to change :-)
ASP.NET 成员资格是特定于 Web 的,因此应在控制器中访问。 MHO 是服务层不应该硬连线到网络。因此,要添加/删除用户,请通过控制器执行此操作。
OTOH,在服务层,您可以读取 Thread.CurrentPrincipal.Identity,它不是特定于 Web 的,但恰好与 ASP.NET Membership 完全兼容。因此,如果您只需要获取当前用户,则可以在不破坏关注点分离的情况下做到这一点。
ASP.NET Membership is Web-specific, so that should be accessed in the Controller. MHO is that the service layer should not be hard-wired to the web. So for adding/removing users, do that via the Controller.
OTOH, in the service layer, you can read
Thread.CurrentPrincipal.Identity
, which is non-web-specific, but happens to be entirely compatible with ASP.NET Membership. So if you only need to get the current user you can do that without voilating separation of concerns.使用System.Web真的有问题吗?这与将其绑定到 System.Configuration 或 System.IO 没有什么不同。任何应用程序都可以使用它,无论它是否“离线”。
我经常将我的 Web 应用程序绑定到更经典的“winforms”程序集,以访问有用的集合对象等。
Is it really a problem to use System.Web? It's no different than tying it to System.Configuration, or System.IO. Any application can make use of it, whether it's "offline" or not.
I routinely tie my web apps to assemblies that are more classically though of as "winforms" assemblies, to get access to useful collection objects and such.