Reputation: 16219
I have added repository code and invoked it into HomeController.cs
public class HomeController : Controller
{
private IPlatformRepository _platformRepository;
public HomeController()
{
this._platformRepository = new PlatformRepository(new IoTSimulatordbContext());
}
public HomeController(IPlatformRepository platformRepository )
{
this._platformRepository = platformRepository;
}
the above code is only for PlatformRepository If i want to add multiple repository can I do like below ?
public class HomeController : Controller
{
private IPlatformRepository _platformRepository;
private IDeviceRepository _deviceRepository;
public HomeController()
{
this._platformRepository = new PlatformRepository(new IoTSimulatordbContext());
this._deviceRepository = new DeviceRepository(new IoTSimulatordbContext());
}
public HomeController(IPlatformRepository platformRepository, IDeviceRepository deviceRepository)
{
this._platformRepository = platformRepository;
this._deviceRepository = deviceRepository;
}
is this correct way to do ?
public HomeController()
{
this._platformRepository = new PlatformRepository(new IoTSimulatordbContext());
this._deviceRepository = new DeviceRepository(new IoTSimulatordbContext());
}
Upvotes: 0
Views: 75
Reputation: 15559
If you are asking about the syntax, then I don't see any problem... if you are asking about design and best practices, then consider the following points:
It would be better to inject the repositories into the controller, because it is not controller's concern to initialize the repositories... your code is violating Single Responsibility Principle, because the controller is having the extra/unnecessary responsibility of initializing the repositories.
Another good point in your question is about the number of dependencies in the controller. For that, I would refer you to Nikola’s 2nd law of IoC: see here
Any class having more then 3 dependencies should be questioned for SRP violation
Your controller has dependency on 2 repositories, so its fine... but if you have more than 3 dependencies, it's good to consider refactoring. If you are wondering how to refactor repositories, consider the followings:
Identify the Aggregate roots of your domain and create a single repository per aggregate root... so in essence you are combing related types in one repository.
You could combine the repositories in a bigger block (service)... now the high level class (controller) has dependency on your service and the service in turn has dependency on one or more repositories.
Upvotes: 1
Reputation: 1327
Yes, It's ok to instantiate IoTSimulatordbContext twice. If your application becomes bigger you may get some errors because the same instance is used in multiple places and not thread saving. Also, managing instantiation in this way requires you to properly manage the disposal of instances.
I recommend you to use dependency injection (use Autofac or Ninject) - it will help you to manage class instantiation and do disposal of them. So your constructor with DI will look like this
public class HomeController : Controller
{
private readonly IPlatformRepository _platformRepository;
private readonly IDeviceRepository _deviceRepository;
public HomeController(IPlatformRepository platformRepository, IDeviceRepository deviceRepository)
{
this._platformRepository = platformRepository;
this._deviceRepository = deviceRepository;
}
Upvotes: 1