C# 从命令处理程序传递变量
我的命令处理程序中有一个从另一个方法获取变量的方法,我试图将这些变量传递给C# 从命令处理程序传递变量,c#,C#,我的命令处理程序中有一个从另一个方法获取变量的方法,我试图将这些变量传递给CreateUser(NewAccount)方法,但它总是返回为null public async Task ExecuteAsync(CreateUserAccountCommand command) { var result = await _client.CreateUser(GetAccountFrom(command)); // so this line gets the variables from G
CreateUser(NewAccount)代码>方法,但它总是返回为null
public async Task ExecuteAsync(CreateUserAccountCommand command)
{
var result = await _client.CreateUser(GetAccountFrom(command)); // so this line gets the variables from GetAccountFrom(command)
_httpContextAccessor.HttpContext.Items["CreateUserAccountCommand"] = result;
}
private Account GetAccountFrom(CreateUserAccountCommand command)
{
var NewAccount = new Account();
NewAccount.FirstName = command.FirstName;
NewAccount.LastName = command.LastName;
return NewAccount()
}
public System.Threading.Tasks.Task<Account> CreateUser(Account NewAccount,)
{
return base.Channel.CreateUser(NewAccount);
}
但是,当我调用CreateUser
将变量传入新帐户时,它将以null
的形式传入
public async Task ExecuteAsync(CreateUserAccountCommand command)
{
var result = await _client.CreateUser(GetAccountFrom(command)); // so this line gets the variables from GetAccountFrom(command)
_httpContextAccessor.HttpContext.Items["CreateUserAccountCommand"] = result;
}
private Account GetAccountFrom(CreateUserAccountCommand command)
{
var NewAccount = new Account();
NewAccount.FirstName = command.FirstName;
NewAccount.LastName = command.LastName;
return NewAccount()
}
public System.Threading.Tasks.Task<Account> CreateUser(Account NewAccount,)
{
return base.Channel.CreateUser(NewAccount);
}
public System.Threading.Tasks.Task CreateUser(Account NewAccount,)
{
返回base.Channel.CreateUser(NewAccount);
}
我做错了什么?您正在return语句中创建NewAccount
的新实例
private Account GetAccountFrom(CreateUserAccountCommand command)
{
var newAccount = new Account();
newAccount.FirstName = command.FirstName;
newAccount.LastName = command.LastName;
return newAccount; // <- Return the variable
}
私人帐户GetAccountFrom(CreateUserAccountCommand命令)
{
var newAccount=新账户();
newAccount.FirstName=command.FirstName;
newAccount.LastName=command.LastName;
return newAccount;//您正在return语句中创建newAccount
的新实例
private Account GetAccountFrom(CreateUserAccountCommand command)
{
var newAccount = new Account();
newAccount.FirstName = command.FirstName;
newAccount.LastName = command.LastName;
return newAccount; // <- Return the variable
}
私人帐户GetAccountFrom(CreateUserAccountCommand命令)
{
var newAccount=新账户();
newAccount.FirstName=command.FirstName;
newAccount.LastName=command.LastName;
return newAccount;//您的代码有很多反模式,但似乎您在基本newAccount();
这就是为什么应该避免继承(对于初学者和中级用户)
还有私有局部变量的约定,小写..以免混淆自己
private Account GetAccountFrom(CreateUserAccountCommand command)
{
var newAccount = new Account();
newAccount.FirstName = command.FirstName;
newAccount.LastName = command.LastName;
return newAccount;
}
或者,为了完全避免混淆,只需这样做
private Account GetAccountFrom(CreateUserAccountCommand command)
{
return new Account{
FirstName = command.FirstName,
LastName = command.LastName,
}
}
但是,为了避免反模式和意大利面代码,您应该真正创建一个扩展方法,它更具S.O.L.I.D
namespace you.company
{
public static CommandExtensions{
public static Account GetAccountFrom(this CreateUserAccountCommand command)
{
return new Account
{
FirstName = command.FirstName,
LastName = command.LastName,
};
}
}
您的代码有许多反模式,但似乎在basenewAccount();
中的某个地方有一个方法,这就是为什么应该避免继承(对于初学者和MID)
还有私有局部变量的约定,小写..以免混淆自己
private Account GetAccountFrom(CreateUserAccountCommand command)
{
var newAccount = new Account();
newAccount.FirstName = command.FirstName;
newAccount.LastName = command.LastName;
return newAccount;
}
或者,为了完全避免混淆,只需这样做
private Account GetAccountFrom(CreateUserAccountCommand command)
{
return new Account{
FirstName = command.FirstName,
LastName = command.LastName,
}
}
但是,为了避免反模式和意大利面代码,您应该真正创建一个扩展方法,它更具S.O.L.I.D
namespace you.company
{
public static CommandExtensions{
public static Account GetAccountFrom(this CreateUserAccountCommand command)
{
return new Account
{
FirstName = command.FirstName,
LastName = command.LastName,
};
}
}
您正在使用new
关键字创建对象。只需通过简单调用从方法返回此对象:
return NewAccount;
现在的方法是返回NewAccount()
方法的结果(不管它是什么,显然null
),这不是您想要的结果
另外,您可能需要检查为什么NewAccount()
总是返回null
您正在使用new
关键字创建对象。您只需通过简单调用从方法返回此对象:
return NewAccount;
现在的方法是返回NewAccount()
方法的结果(不管它是什么,显然null
),这不是您想要的结果
另外,您可能需要检查为什么NewAccount()
总是返回null
返回returnnewaccount())
如果是这样的话?没有新的
关键字。我的意思是,除非它隐式地将编译时间转换为匿名函数任务,否则编译时间应该会爆炸。@Magnus好的,在这行的末尾还应该有一个;
,所以我只是猜测代码没有被复制,而是被重写了d空间消失了。一定是某个地方有一个方法NewAccount,因为我将其复制到myu Visual studio中,并得到了一个编译错误。方法名称应为
,也不会导致null
被传递到CreateUser()
它不会被返回返回新帐户()
如果是这样的话?没有新的
关键字。我的意思是,除非它隐式地将编译时间转换为匿名函数任务,否则编译时间应该会爆炸。@Magnus好的,在这行的末尾还应该有一个;
,所以我只是猜测代码没有被复制,而是被重写了d空间消失了。一定是某个地方有一个方法NewAccount,因为我将其复制到myu Visual studio中,并得到了一个编译错误。方法名称应为,也不会导致null
被传递到CreateUser()
这就是您的代码外观吗?尤其是这一行return NewAccount()
不是。因为有很多语法错误。它被复制和修改粘贴以保护IP。lol这篇文章非常需要。你的代码就是这样的吗?尤其是这一行return NewAccount()
不是。因为有很多语法错误。它被复制和修改粘贴以保护IP。lol这篇文章非常需要。