I'm working in a web solution with C#, MVC4, StructureMap, etc.
In the solution I have services for controllers. By example :
public class ServiceA{
private readonly IRepository _repository1;
private readonly IRepository _repository2;
public ServiceA(IRepository1 repository1, IRepository2 repository2){
_repository1=repository1;
_repository2=repository2;
}
public void DoSomethingA(){
_repository1.DoSomething();
}
public void DoSomethingB(){
_repository2.DoSomething();
}
}
public class ServiceB{
private readonly IRepository _repository3;
private readonly IRepository _repository4;
public ServiceB(IRepository3 repository3, IRepository4 repository4){
_repository3=repository3;
_repository4=repository4;
}
public void DoSomethingA(){
_repository3.DoSomething();
}
public void DoSomethingB(){
_repository4.DoSomething();
}
}
It is good practice to do this? :
public abstract class ServiceBase(){
public IRepository1 Repository1 { get { return instanceOf<IRepository1>(); }}
public IRepository2 Repository2 { get { return instanceOf<IRepository2>(); }}
public IRepository3 Repository3 { get { return instanceOf<IRepository3>(); }}
public IRepository4 Repository4 { get { return instanceOf<IRepository4>(); }}
private T instanceOf<T>()
{
return ServiceLocator.Current.GetInstance<T>();
}
}
And then create the services in this way?
public class ServiceA : ServiceBase
{
public void DoSomethingA(){
Repository1.DoSomething();
}
public void DoSomethingB(){
Repository2.DoSomething();
}
}
public class ServiceB : ServiceBase
{
public void DoSomethingA(){
Repository3.DoSomething();
}
public void DoSomethingB(){
Repository4.DoSomething();
}
}
With the second option I see certain advantages:
- It is not necessary to have a private variable for each repository.
- I do not need a constructor for services, making them smaller and easier to read.
- All repositories will be available in any service.
- The service does not get unnecessary instances. By example, calling in
ServiceA
the methodDoSomethingA
theServiceLocator
get onlyRepository1
instance. ( using the first method would receive two instances: forRepository1
andRepository2
)
In both cases I can make the appropriate tests:
- In the first case, sending the mocked object through the constructor.
- In the second case configuring StructureMap to use the mocked object when is necesary.
Do you think? I'm going against some principles? (sorry my english)