MVC 自定义属性和绑定
我有一个项目,我们有自己的客户注册和帐户管理系统,但应用程序的某些元素链接到第三方服务。这些服务具有共同的功能,例如在自己的数据库中创建帐户,但底层实现在如何与第三方服务交互方面会有所不同。
到目前为止我所做的是创建一个实现 ICustomerRepository 的 CustomerRepository。这包含我们自己的所有具体要求。 ICustomerRepository 还定义了所有第三方都会有的通用方法,但这些方法在 CustomerRepository 类中设置为 virtual,如果调用它们,则会引发异常,要求您在第三方类中实现它们。
然后,据此,我得到:
ThirdPartyACustomer : CustomerRepository, IThirdPartyACustomer
ThirdPartyBCustomer : CustomerRepository
正如您可能猜到的那样,这两个子类都继承并重写了虚拟方法,但 ThirdPartyACustomer 除外,它还实现了特定于该特定类型的第三方用户的其他方法(例如,可能有是用户可以编辑与第三方 A 相关的特定功能的地方,而第三方 B 不提供这些功能。
现在,我的问题的真正基础是:
我的一些进程(控制器) 。应用程序可以使用 CustomerRepository,无需任何问题,因为
应用程序中的其他进程需要传递特定类型的 ICustomerRepository ,任何调用在 CustomerRepository 中定义为虚拟的方法都需要传递 ThirdPartyACustomer 或 ThirdPartyBCustomer ,以便正确实现。 最初在这种类型的控制器的初始化中,
我会执行以下操作:
public RegistrationController()
{
ICustomerRepository _customerRepository = GetCustomerRepository();
}
其中 GetCustomerRepository() 有一些逻辑根据子域确定要使用哪种类型的第三方, 例如。
现在,我的想法是,我通过创建一个自定义属性来改进这一点,按照以下方式:
[ThirdPartyDependent]
class RegistrationController
{
public RegistrationController(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
}
并将 customerRepository 参数的数量移到该属性中,例如 GetCustomerRepository 中的逻辑将发生在那里。
我相当确定这样的事情是可行的,并且对于测试目的似乎有意义,但不太确定我应该谷歌搜索什么,或者是否有更好的方法来做事情,所以寻找更多人的指导具有 MVC 经验。
I've got a project where we have our own customer registration and account management system, but certain elements of the application link to 3rd party services. These services have common functionality e.g. creating an account in their own DB, but the underlying implementation will be different for how to interactive with the third party services.
What I've done so far is create a CustomerRepository which implements ICustomerRepository. This contains all our own specific requirements. ICustomerRepository also has definitions for the common methods that all third parties will have, but these methods are set to virtual in the CustomerRepository class, which throws exceptions if they're called, requiring you to implement them in the third party classes.
Then from this, I have:
ThirdPartyACustomer : CustomerRepository, IThirdPartyACustomer
ThirdPartyBCustomer : CustomerRepository
As you can probably guess, both of those sub classes inherit and override the virtual methods, with the exception of ThirdPartyACustomer which also implements additional methods that are specific to that particular type of third party user (e.g. there might be a place where the user can edit specific features related to third party A, which third party B doesn't offer.
Now, with that out of the way, the real basis of my question:
Some of the processes (controllers) in my application can use the CustomerRepository without any problems as they only need our core functionality.
Other processes in the app require a particular type of ICustomerRepository to be passed. Anything that calls a method that was defined as virtual in CustomerRepository will need to pass either ThirdPartyACustomer or ThirdPartyBCustomer so that the correct implementation is called.
Originally in this initialisation of this type of controller I'd do something like:
public RegistrationController()
{
ICustomerRepository _customerRepository = GetCustomerRepository();
}
where GetCustomerRepository() had some logic that determined which type of ThirdParty to use, based on the subdomain, for example.
Now, what I'm thinking is that I improve this by creating a custom attribute, along the lines of this:
[ThirdPartyDependent]
class RegistrationController
{
public RegistrationController(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
}
and move the population of customerRepository parameter into that attribute, e.g. the logic in GetCustomerRepository would happen in there.
I'm fairly sure something like this is doable and seems to make sense for testing purposes, but not quite sure of what I should be googling for, or whether there is a better way to do things, so looking for some guidance from someone more experienced with MVC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 DI 框架的职责。例如,Ninject 在配置依赖项时为您提供对 HttpContext 的访问,因此您可以根据某些 HttpContext 值选择正确的实现。例如:
当然你的控制器将是完全不可知的。控制器应该关心的是它被注入一些遵守给定契约的存储库:
That's the responsibility of your DI framework. For example Ninject provides you access to the HttpContext when configuring the dependencies, so you could pick the proper implementation based on some HttpContext value. For example:
and then of course your controller will be totally agnostic. All that a controller should care is that it gets injected some repository which obeys a given contract: