As an example, let's say that I have many possible connection strings, and in some cases when I want an instance of DataContext I have to initialize it with one of those. Since my ServiceLocator doesn't accept functions with parameters, I cannot write
- var dc = ServiceLocator.GetInstanceOf<DataContext>(connectionString);
Here's a way of transforming a function with parameters to one without: create a function that returns the original function. That is, given
- Func<T1, T> f;
I need another function that will return f:
- Func<Func<T1, T>> g = () => (a) => f(a);
So, in order to register my DataContext I am now going to use the following syntax:
- Bind<Func<string, DataContext>>.To(() => (connectionString => new DBDataContext(connectionString)));
and to retrieve the instance and initialize it correctly, I must use:
- var dc = ServiceLocator.GetInstanceOf<Func<string, DataContext>>()(connectionString);
Not extremely clear, so some helper functions (and confining in a library of sorts) are probably needed.
Edit: I just made a change to the ServiceLocator code; one can now write:
- var dc = ServiceLocator.GetInstanceOf<string, DataContext>("connectionString");
This is the overload for one argument:
- public static T GetInstanceOf<T1, T>(T1 arg1) where T : class
- {
- return GetInstanceOf<Func<T1, T>>()(arg1);
- }
I only have 3 overloads (up to Func
0 comments:
Post a Comment