构造函数链接中间变量
我有以下的重载构造函数,我正在努力找到一个很好的解决方案。 我看不到如何使用构造函数链接的中间赋值。
以下内容无效,但显示了我想要做的事情
public MyThing(IServiceLocator services, int? userId) { // blah.... } public MyThing(IServiceLocator services, string userName) { User user = services.UserService.GetUserByName(userName); int userId = user == null ? null : (int?)user.Id; // call the other constructor this(services, userId); }
我知道在有效代码中编写上述内容的唯一方法是
public MyThing(IServiceLocator services, string userName) : this(services, services.UserService.GetUserByName(userName) == null ? null : (int?)services.UserService.GetUserByName(userName).Id)
这不仅是丑陋的代码,而且还需要数据库调用两次(除非编译器足够聪明地工作,我怀疑)。
有没有更好的方法来写上面的?
那这个呢:
public MyThing(IServiceLocator services, string userName) { User user = services.UserService.GetUserByName(userName); int? userId = user == null ? null : (int?)user.Id; Initialize(services, userId); } public MyThing(IServiceLocator services, int? userId) { Initialize(services, userId); } private void Initialize(IServiceLocator services, int? userId) { // initialization logic }
编辑
如果我是你,我会用这样的工厂方法替换构造函数:
private MyThing(IServiceLocator services, int? userId) { // blah.... } public static Create(IServiceLocator services, int? userId) { return new MyThing(services, userId); } public static Create(IServiceLocator services, string userName) { User user = services.UserService.GetUserByName(userName); int userId = user == null ? null : (int?)user.Id; return new MyThing(services, userId); }
用法:
var myThing = MyThing.Create(services, 123); var myOtherThing = MyThing.Create(services, "userName");
用工厂方法替换构造函数(refactoring.com)
有,是的。 我知道这个例子是用Java编写的,但对于你的问题来说这是一个很好的解决方案,一些移植到C#的工作确实有意义。
您可以使用静态帮助器方法:
public MyThing(IServiceLocator services, int? userId) { // blah.... } public MyThing(IServiceLocator services, string userName) : this(services, GetUserId(services, userName)) { } private static int? GetUserId(IServiceLocator services, string userName) { User user = services.UserService.GetUserByName(userName); return (user == null) ? (int?)null : user.Id; }