2

I'm currently building an ASP.Net MVC 4 SAAS application (C#) and am stuck on designing the plans. I mean if a customer picks Plan A they should have access to some things and if they pick Plan B they get access to others and so on.

The Part that I'm stuck on is be best practice of sharing the account's plan with all of the actions. I realize having global variables are bad practice and all but I really don't want to take round trips to the DB to get the plan on every action.

What I'm thinking of doing is something like This SO answer where they just declare a static model and set it at some point and access it later on. In your opinion, is this the best way of doing this? is there a better way?

Community
  • 1
  • 1
hjavaher
  • 2,589
  • 3
  • 30
  • 52
  • **but I really don't want to take round trips to the DB to get the plan on every action** Below approach is going to make db call on each action. Correct ? – Imad Alazani Jul 27 '13 at 13:52
  • @PKKG: there is a check to ensure that we load only once. – Khanh TO Jul 27 '13 at 14:13
  • @Khanh : That is the memory of Interface not the memory of data. right ? So, each request for action will go to db – Imad Alazani Jul 27 '13 at 14:17
  • @PKKG: there is only 1 instance of IConfigService interface if we configure this instance as singleton in our IoC (our IoC holds a reference to it) and reuse it throughout the app. – Khanh TO Jul 27 '13 at 14:21

1 Answers1

5

I think best practice is you should include an IoC in your project and inject a configuration object to your controller.

Example code of a controller:

public class YourController : Controller
 {
     private IConfigService _configService;

     //inject your configuration object here.
     public YourController(IConfigService configService){
           // A guard clause to ensure we have a config object or there is something wrong
           if (configService == null){
               throw new ArgumentNullException("configService");
           }
           _configService = configService;
     }
 }

You could configure your IoC to specify singleton scope for this configuration object. In case you need to apply this pattern to all your controllers, you could create a base controller class to reuse code.

Your IConfigService

public interface IConfigService
{
    string ConfiguredPlan{ get; }
}

Your ConfigService:

public class ConfigService : IConfigService
{
    private string _ConfiguredPlan = null;

    public string ConfiguredPlan
    {
        get
        {
            if (_ConfiguredPlan == null){
                    //load configured plan from DB
            }
            return _ConfiguredPlan;
        }
    }
}
  • This class is easily extended to include more configurations like connection String, Default timeout,...
  • We're passing in an interface to our controller class, it's easy for us to mock this object during unit testing.
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • Wow, I really like this solution. I will try to implement it tomorrow. Thanks! – hjavaher Jul 27 '13 at 09:35
  • me too :) I'm really a newbee when it comes to DI. I really have to start thinking that way – hjavaher Jul 27 '13 at 09:45
  • You are saying IOC, but this is `Dependency Injections`? You are injecting the `Interface`(dependency) in `Constructor`. – Imad Alazani Jul 27 '13 at 13:23
  • 1
    @PKKG: you should know the differences between these terms. Dependency Injection is a set of software design principles and patterns that enable us to develop loosely coupled code. IoC or DI container is a software library that provides DI functionality. Hope it's clear. Have a nice day. – Khanh TO Jul 27 '13 at 14:07
  • So internally, Inversion of Control does Singleton Pattern Class Implementation ? – Imad Alazani Jul 27 '13 at 14:09
  • @PKKG: no, it's much more than that. We can do more with IoC. I think you could take a look on some documentations to get more information – Khanh TO Jul 27 '13 at 14:12
  • @PKKG: it's about using an IoC to achieve DI. Remember that DI is just a concept, not an actual implementation. – Khanh TO Jul 27 '13 at 14:15
  • @PKKG: NInject, Castle Windsor, Unity,.. are all examples of IoC (they are software libraries that provide DI functionality) – Khanh TO Jul 27 '13 at 14:18
  • Will you like to share the documentation that support your last comment ? – Imad Alazani Jul 27 '13 at 14:19
  • @PKKG: I read all that information from this book: http://books.google.com.vn/books/about/Dependency_Injection_in_NET.html?id=lnOqcQAACAAJ&redir_esc=y – Khanh TO Jul 27 '13 at 14:36
  • No need to go through all this complex logics. Just use the singleton pattern on a class(with whatever you want in it) and use it as a 'Global thing' – Parth Dec 17 '14 at 05:26