3

I was wondering what situations a Ninject Singleton scope could be useful in ASP.NET MVC. I was thinking in situations when the dependency didn't have any properties/variables that could change. For example a mapper class that just takes in an object or two and maps it to a new object.

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;

} }

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
boff101
  • 75
  • 6

1 Answers1

2

Yes, you could use that class in SingletonScope() because it doesn't hold any properties or attributes that could be potentially affected by other threads, but you should also be aware that adding instance-data may break your code.

You can read more about Singleton Scope here: https://github.com/ninject/ninject/wiki/Object-Scopes and see other opinions: When to use Singleton vs Transient vs Request using Ninject and MongoDB

Community
  • 1
  • 1
Meryovi
  • 6,121
  • 5
  • 42
  • 65