views:

84

answers:

1

Hello,

Wherein lies the advantage of the first approach getting customer data?

ICustomerService customerService = MyService.GetService<ICustomerService>();
ICustomerList customerList = customerService.GetCustomers();

vs.

ICustomerRepository customerRepo = new CustomerRepository();
ICustomerList customerList = customerRepo.GetCustomers();

If you understand my question you will not ask how the implementation of the MyService class looks like ;-)

here is the implementation of the Repo...

interface ICustomerRepository
{
    ICustomerList GetCustomers();
}

class CustomerRepository : ICustomerRepository
{
    public ICustomerList GetCustomers()
    {...}
}
+3  A: 

The advantage of the first approach is that by using a service locator you can easily swap out the implementation of ICustomerService if needed. With the second approach, you would have to replace every reference to the concrete class CustomerRepository in order to switch to a different ICustomerService implementation.

An even better approach is to use a dependency injection tool such as StructureMap or Ninject (there are many others) to manage your dependencies.

Edit: Unfortunately many typical implementations look like this which is why I recommend a DI framework:

public interface IService {}
public interface ICustomerService : IService {}
public class CustomerService : ICustomerService {}
public interface ISupplerService : IService {}
public class SupplierService : ISupplerService {}

public static class MyService
{
    public static T GetService<T>() where T : IService
    {
        object service;
        var t = typeof(T);
        if (t == typeof(ICustomerService))
        {
            service = new CustomerService();
        }
        else if (t == typeof(ISupplerService))
        {
            service = new SupplierService();
        }
        // etc.
        else
        {
            throw new Exception();
        }
        return (T)service;
    }
}
Jamie Ide
@Jamieor LightCore http://lightcore.peterbucher.ch/ ;-)Well for a desktop app having 10k LoC I do not want to use a DI tool also Lightcore is damn small ;-)Can you show me a typical implemenation of such ServiceLocator? I guess MyService is a static class right?
msfanboy
ah... so in the end i would have 20 else if`s running 20 different services ?? hell this is like coding vb :Pbtw. if your interested in more points :P http://stackoverflow.com/questions/3181522/c-services-access-the-dataprovider-class-running-the-sql-statements-correct-a
msfanboy
I think there are good implementations of this pattern but this is one I've seen (and written myself).
Jamie Ide
so why have you not used a DI tool and wrote that stuff for yourself?
msfanboy
I was young and foolish. I wouldn't write that sort of code today but I've written similar in the past.
Jamie Ide
at least a honest anwser for this you get the solution :P
msfanboy
but still a last side question Jamie:When would you use a DI tool? Can you make a rule concerning the lines of code?Can there be a rule at all?
msfanboy
I use DI for services and repositories. I also use it in our Windows Forms app. to locate forms -- one of the advantages of DI is that you can easily change activation behavior, so I can switch a form from singleton pattern to getting a new instance each time.
Jamie Ide
yeah thats nice something like that create form/get existing form I use in CAB which really sucks ;-). We want to get rid of it, slows all down hehe.Well in the other thread you wrote you would not use anymore this:"...I haven't found much value in declaring interfaces for business classes or for custom collections since extension methods became available. I would have GetCustomers return IEnumerable<Customer>"but why use a DI tool with static dataservice classes? Is that your way?
msfanboy
No, I use repository classes with NHibernate, so the ISession is injected into the repository constructor.
Jamie Ide
The methods of your repository classes are implemented by a interface?NHibernate + sqlite db makes me feel cold, no designer, no support etc... even worse than EF 4.0 with sqlite.Using a local database with orm tools is often frustrating if you ask me. 50% of the sql features are not implemented in the orm tool.
msfanboy