我想知道在ASP.NET MVC中使用Ninject单例作用域有哪些情况下会有用。我认为在依赖项没有任何可能更改的属性/变量的情况下会很有用,例如一个只传入一个或两个对象并将其映射到新对象的映射器类。
public class AccountMapper : IAccountMapper
{
public IList<WebAccount> Map(Account[] accounts)
{
if (accounts == null)
throw new ArgumentNullException("accounts");
Account acct;
List<WebAccount> authorizedAccounts = new List<WebAccount>();
foreach (var account in accounts)
{
if (account.active == 0)
{
acct = new WebAccount (
account.active == 0 ? true : false,
account.account_number,
account.agreement_no
);
if (!string.IsNullOrEmpty(acct.AccountNumber))
acct.AccountNumber = acct.AccountNumber.Trim();
if (!string.IsNullOrWhiteSpace(acct.ArgreementNo))
acct.ArgreementNo = acct.ArgreementNo.Trim();
authorizedAccounts.Add(acct);
}
}
return authorizedAccounts;
} }