Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 160079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:58:17+00:00 2026-05-11T10:58:17+00:00

I just finished James Kovac’s article on Inversion of Control and Dependency Injection and

  • 0

I just finished James Kovac’s article on Inversion of Control and Dependency Injection and then used what I learned to make my own IoC/DI example below.

I’m quite satisified with this example since it:

  • satisfies the testability aspect of IoC in that it instantiates Customers by passing both a Repository and a MockRepository
  • also the authorization service is decoupled which would allow you to e.g. write another authorization service with different rules, and then you could easily swap them out based on other conditions

However, looking toward progressing from here, some things seem odd:

  • I don’t seem to have a ‘container’. Is my Customer class ‘acting as a container’ in this sense?
  • if I were to port this to WPF, where would Modules (in terms of Prism) fit into this example (e.g. would AuthorizationService and Repository be modules?)
  • if I were to port this to WPF, where would MVVM fit in? Do have have parts of MVVM through having the dependency injection or is MVVM something separate altogether.

Thanks for any direction you can provide on this.

NEW CODE BASED ON COMMENTS:

using System; using System.Linq; using System.Collections.Generic;  namespace TestSimpleDependencyInjection1 {     class Program     {         static void Main(string[] args)         {             AuthorizationService authorizationService = new AuthorizationService();              //real example             Repository repository = new Repository(authorizationService);             for (int id = 1; id <= 3; id++)             {                 Customer customer = repository.GetCustomer(id);                 customer.Display();             }             Console.WriteLine();              //mock test example             MockRepository mockRepository = new MockRepository(authorizationService);             Customer mockCustomerAdministrator = repository.GetCustomer(1);             Customer mockCustomerSalesperson = repository.GetCustomer(2);             UnitTester.Assert('Administrators have access', mockCustomerAdministrator.GetAuthorizationMessage(), 'Access Granted');             UnitTester.Assert('Salespeople do not have access', mockCustomerAdministrator.GetAuthorizationMessage(), 'Access Granted');              Console.ReadLine();         }     }      public static class UnitTester     {         public static void Assert(string title, string value, string expectedResult)         {             Console.WriteLine(value == expectedResult ? String.Format('{0}: test succeeded', title) : String.Format('{0}: TEST FAILED!', title));         }     }      public class Customer     {          public int ID { get; set; }         public string FirstName { get; set; }         public string LastName { get; set; }         public AccessGroup AccessGroup { get; set; }         public AuthorizationService AuthorizationService { get; set; }          public string GetAuthorizationMessage()         {             return this.AuthorizationService.GetAccessMessage(this);         }          public Customer()         {          }          public void Display()         {             Console.WriteLine('Customer: {1}, {0} ({2}): {3}', this.FirstName, this.LastName, this.AccessGroup, this.GetAuthorizationMessage());         }     }      public class AuthorizationService     {         public string GetAccessMessage(Customer customer)         {             return customer.AccessGroup == AccessGroup.Administrator ? 'Access Granted' : 'Access Denied';         }     }       public class Repository : IRepository     {         private List<Customer> _customerSet = new List<Customer>();         private AuthorizationService _authorizationService;          public Repository(AuthorizationService authorizationService)         {             _authorizationService = authorizationService;             _customerSet.Add(new Customer {AuthorizationService = _authorizationService, ID = 1, FirstName = 'Jim', LastName = 'Smith', AccessGroup = AccessGroup.Administrator });             _customerSet.Add(new Customer {AuthorizationService = _authorizationService, ID = 2, FirstName = 'John', LastName = 'Johnson', AccessGroup = AccessGroup.Administrator });             _customerSet.Add(new Customer {AuthorizationService = _authorizationService, ID = 3, FirstName = 'Hank', LastName = 'Rivers', AccessGroup = AccessGroup.Salesperson });         }          public Customer GetCustomer(int id)         {             return (from c in _customerSet                     where c.ID == id                     select c).SingleOrDefault();         }     }      public class MockRepository : IRepository     {         private List<Customer> _customerSet = new List<Customer>();         private AuthorizationService _authorizationService;          public MockRepository(AuthorizationService authorizationService)         {             _authorizationService = authorizationService;             _customerSet.Add(new Customer { AuthorizationService = _authorizationService, ID = 1, FirstName = 'Test1AdministratorFirstName', LastName = 'Test1AdministratorLastName', AccessGroup = AccessGroup.Administrator });             _customerSet.Add(new Customer { AuthorizationService = _authorizationService, ID = 2, FirstName = 'Test2SalespersonFirstName', LastName = 'Test2SalesPersonLastName', AccessGroup = AccessGroup.Salesperson });         }          public Customer GetCustomer(int id)         {             return (from c in _customerSet                             where c.ID == id                             select c).SingleOrDefault();         }      }      public interface IRepository     {         Customer GetCustomer(int id);     }      public enum AccessGroup     {         Administrator,         Salesperson     } } 

AND PER REQUEST, HERE IS THE ORIGINAL CODE:

using System; using System.Collections.Generic;  namespace TestSimpleDependencyInjection1 {     class Program     {         static void Main(string[] args)         {             AuthorizationService authorizationService = new AuthorizationService();              //real example             Repository repository = new Repository();             for (int id = 1; id <= 3; id++)             {                 Customer customer = new Customer(id, authorizationService, repository);                 customer.Display();             }             Console.WriteLine();              //mock test example             MockRepository mockRepository = new MockRepository();             Customer mockCustomerAdministrator = new Customer(1, authorizationService, mockRepository);             Customer mockCustomerSalesperson = new Customer(2, authorizationService, mockRepository);             UnitTester.Assert('Administrators have access', mockCustomerAdministrator.GetAuthorizationMessage(), 'Access Granted');             UnitTester.Assert('Salespeople do not have access', mockCustomerAdministrator.GetAuthorizationMessage(), 'Access Granted');              Console.ReadLine();         }     }      public static class UnitTester     {         public static void Assert(string title, string value, string expectedResult)         {             Console.WriteLine(value == expectedResult ? String.Format('{0}: test succeeded', title) : String.Format('{0}: TEST FAILED!', title));         }     }      public class Customer     {         private AuthorizationService authorizationService;         private IRepository repository;          public int ID { get; set; }         public string FirstName { get; set; }         public string LastName { get; set; }         public AccessGroup AccessGroup { get; set; }          public string GetAuthorizationMessage()         {             return authorizationService.GetAccessMessage(this);         }          public Customer(int id, AuthorizationService authorizationService, IRepository repository)         {             this.authorizationService = authorizationService;             this.repository = repository;              CustomerDB customerDB = repository.GetCustomerDB(id);             this.ID = customerDB.ID;             this.FirstName = customerDB.FirstName;             this.LastName = customerDB.LastName;             this.AccessGroup = customerDB.AccessGroup;         }          public void Display()         {             Console.WriteLine('Customer: {1}, {0} ({2}): {3}', this.FirstName, this.LastName, this.AccessGroup, this.GetAuthorizationMessage());         }     }      public class AuthorizationService     {         public string GetAccessMessage(Customer customer)         {             return customer.AccessGroup == AccessGroup.Administrator ? 'Access Granted' : 'Access Denied';         }     }       public class Repository : IRepository     {         private List<CustomerDB> _customerDBSet = new List<CustomerDB>();          public Repository()         {             _customerDBSet.Add(new CustomerDB { ID = 1, FirstName = 'Jim', LastName = 'Smith', AccessGroup = AccessGroup.Administrator });             _customerDBSet.Add(new CustomerDB { ID = 2, FirstName = 'John', LastName = 'Johnson', AccessGroup = AccessGroup.Administrator });             _customerDBSet.Add(new CustomerDB { ID = 3, FirstName = 'Hank', LastName = 'Rivers', AccessGroup = AccessGroup.Salesperson });         }          public CustomerDB GetCustomerDB(int id)         {             CustomerDB customerDBchosen = null;             //this should be done with LINQ (couldn't get it CustomerDB to implement IEnumerable correctly)             foreach (CustomerDB customerDB in _customerDBSet)             {                 if (customerDB.ID == id)                 {                     customerDBchosen = customerDB;                     break;                 }             }             return customerDBchosen;         }     }      public class MockRepository : IRepository     {         public CustomerDB GetCustomerDB(int id)         {             switch (id)             {                 case 1:                     return new CustomerDB { ID = 1, FirstName = 'Test1AdministratorFirstName', LastName = 'Test1AdministratorLastName', AccessGroup = AccessGroup.Administrator };                 case 2:                     return new CustomerDB { ID = 2, FirstName = 'Test2SalespersonFirstName', LastName = 'Test2SalesPersonLastName', AccessGroup = AccessGroup.Salesperson };                 default:                     return null;             }         }     }      public interface IRepository     {         CustomerDB GetCustomerDB(int id);     }      public class CustomerDB     {         public int ID { get; set; }         public string FirstName { get; set; }         public string LastName { get; set; }         public AccessGroup AccessGroup { get; set; }     }      public enum AccessGroup     {         Administrator,         Salesperson     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:58:18+00:00Added an answer on May 11, 2026 at 10:58 am

    Your code violates the single responsibility principal in a pretty serious way.

    http://en.wikipedia.org/wiki/Single_responsibility_principle

    Your Customer object shouldn’t be loading itself from a repository, the repository should be loading the Customer object and handing it back to the caller.

    I would expect something more like:

    Customer customer = repository.GetCustomer(3); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just finished reading Jon Skeet's article about events and delegates and got a question.
I just finished the Django tutorial and started work on my own project, however,
I just finished essential part of my own personal 2D engine in C++ and
I just finished designing a webpage for my photography. I used Chrome as my
I just finished a computer organization course, in which we learned that all files
I just finished reading this article on the advantages and disadvantages of exceptions and
I just finished reading Mike's awesome tutorial over at: http://www.mikesdotnetting.com/Article/150/Web-Pages-Efficient-Paging-Without-The-WebGrid and I am using
Just finished the Django tutorial. Starting my own project. Using the ManyToMany Relationship example
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
Just finished up my first mvc4 app. Everything is working great until I deploy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.