I want to decouple some things in my code so that I don’t have to include DLLs that are used in the main project also in the sub-project. For this i have created the following method to register services (which uses the MS Common Practices Service Locator):
public static void RegisterService<TInterface>(SPSite site) where TInterface : IServiceLocatorRegisterable, new()
{
GetServiceLocatorConfig(site).RegisterTypeMapping<IServiceLocatorRegisterable, TInterface>(typeof(TInterface).FullName);
InvalidateCache();
So, as you see I created the Interface “IServiceLocatorRegisterable” so that I’m not bound to a specific interace yet.
In the sub project I have a specific interface I want to register with the service locator, so I added the “IServiceLocatorRegisterable” to the declaration:
public interface ISapProcess : IServiceLocatorRegisterable
{ // details omitted.. }
And this is the code where I try to register this interface:
public static void RegisterSapProcess(SPSite site)
{
ServiceLocator.RegisterService<ISapProcess>(site);
}
But I cannot compile it as I get the follwing compiler error:
ISapProcess must be a non-abstract type with a public parameterless
constructor in order to use it as parameter ‘TInterface’ in the
generic type or method ‘….RegisterService(SPSite)’
.. and as I know see it doesn work either when I would try to register the “base interface” directly (which of course wouldn’t make any sense, as I want to register and locate the specific interfaces/implemenations):
ServiceLocator.RegisterService<IServiceLocatorRegisterable>(site);
I have the feeling that I’m missing something important here.
Well yes – look at your constraint:
You can’t write:
can you? That’s what the constraint requires.
You either need to ditch the constraint, or change your type argument. (It’s not clear what you’re trying to do given the code you’ve provided.)