The classic way is to use Inversion of Control (that means that ServiceManager
is no longer in control of creating Repository
), commonly achieved by Dependency Injection (that means the dependency Repository
is "injected" into SericeManager
).
You seem to already know that, since you wrote
if possible i don't want create service like new ServiceManager(repository);
You can, to achieve this, use an optional argument in the constructor of ServiceManager
, like:
public class ServiceManager
{
private readonly IRepository rep;
public ServiceManager(IRepository rep=null)
{
_rep = rep ?? new Repository();
}
public bool Save(Customer customer)
{
bool res = _rep.Save((customer));
return res;
}
}
to be able to simply use new ServiceManager()
in your application and use new ServiceManager(repositoryMock)
in your tests.
However, if you use a IOC-Container (like e.g. StructureMap), injecting dependencies is plain easy: you can leave the creation of objects to the IOC-Container and keep your ServiceManager
and other classes "clean" of creating instances of dependend classes manually.