Where to store dictionaries in an application using dependency injection

I have outdated code and I have a problem with its reconstruction.

At the beginning of my application, I load from WCFinto a property in App(this SLapplication) a list of users.

Then each control (to send emails, view the calendar and assign tasks) uses this property as

(App.Current as App).Users

Now I'm trying to create a Unit Test for one of the controls that use these lists, and I'm stuck.

Should I do a constructor injection (I use Unity) with a parameter App? Or maybe introduce some kind of class to store this list?

+5
source share
4

OP, .

.

Inject IApplicationService .

, (, ).

OP

 public interface IApplicationService
    {
        List<User> Users{get;set;}
    }

    public class ApplicationService : IApplicationService
    {
        public List<User> Users
        {
            get { return (App.Current as App).Users; }
            set { (App.Current as App).Users = value; }
        }
    }

    public partial class MainWindow : UserControl
    {
        readonly IApplicationService _applicationService
        public MainWindow(IApplicationService applicationService)
        {
            _applicationService=applicationService;
        }
    }
+4

-, . App.Current Unity.

App SUT.

- :

public interface IUserList
{
   List<User> Users { get; }
}

public class SUT
{
   private IUserList UserList { get; set; }

   public SUT(IUserList userList)
   {
     this.UserList = userList;
   }
}

public class AppUserList : IUserList
{
   public List<User> Users
   {
      get
      {
         return ((App)App.Current).Users;
      }
   }
}
+2

Silverlight , Application Extension Services.

, , App.Current .

- , . Users .

. , , . , . .

, , .

" ", Users , . , Users, , .

+1

, , . /:


: IUserService

public interface IUserService
{       
    // Implemented functionality as methods where possible for better
    // extendability (like IoC)
    IEnumerable<User> Users();

    // Add any other user service stuff as you see fit.
    void AddUser(User user);
}

UserService, IUserService

public class UserService : IUserService
{
    // If you need DI for this service, follow the same pattern of using 
    // fields and controller injection. I left examples in comment below.

    // private readonly IRepository _repository;

    // Constructor is unnecessary if you do not need DI example.
    public UserService(/* IRepository repository */) 
    {
        // _repository = repository;
    }

    // Methods
    public IEnumerable<User> Users()
    {
        return ((App)App.Current).Users;
    }
    public void AddUser(User user)
    {
        ((App)App.Current).Users.Add(user);
    }

}

IUserService

MainWindow :

public partial class MainWindow : UserControl
{
    private readonly IUserService _userService;

    public MainWindow(IUserService userService)
    {
        _userService = userService;
    }

    // Example method consuming the service
    public IEnumerable<User> GetUsers()
    {
        return _userService.Users();
    }
}

:

  • . , IApplicationService / , Api Keys, -, , ..

  • IEnumerable<T> List<T>

    , . /, .

  • , , , , - , GetUsers(string lastName), GetUsers(string lastName, string firstName) .

  • App.Current as

    This is good practice because using a keyword asmeans that if it fails, it returns null rather than throwing an exception. I prefer the exception, because in 99% of cases, if your roll fails, your subsequent operations will also be. :)

Enjoy it!

+1
source

All Articles