How do you make a Generic Generic Factory?

I am working on a client interface (Silverlight) for a collection of a web method. And I try to avoid writing any kind of custom code for every web method. Therefore, I created ServiceCall<TResult>to handle each call, it TResultindicates the type of service return (I use XmlSerializerto create the returned instance). The client class provides a function that corresponds to each web method, and all that needs to be done is to create a new instance ServiceCall<TResult>, and TResultis indicated as the expected type of the returned method. And it works great.

I also use Ninject (dependency injector) to try to keep everything independent. Ninject supports open generics that are great for mine ServiceCall<TResult>.

But it also means that I entered a reference to the Ninject container. Which hides the dependency on ServiceCall<TResult>associated with the container. Therefore, I would like to introduce factory instead to create instances ServiceCall<TResult>. Which is not difficult, but I would like to make it a generic generic factory. Meaning I would like to have something like Factory<T<>>that would have a method public T<TU> Createinstance<TU>().

But I have no idea how to create a universal class with a type parameter, which itself is open genric.

Is this event possible in .Net? - Or do I need to create a specific ServiceCallFactory?

Edit: I use interfaces for dependencies, but there is no need to mix them with this question, so I edited this without a question.

Timwi: (DI) , . . . , . ( ). ( Ninject) factory .
public static class Factory<T>, , , T, , .
, " " ( pass in ServiceCall<MyResult>, MyResult)), , ( ), Ninject Get, .
; -, (Ninject), . , , Ninject. , , ServiceCall, Ninject, .
- Factory > , .

, , , , - ;) , .

+3
2

, : Factory

namespace GenericFactoryPatternTestApp
{
    public class Factory< T >
    {
        private readonly Dictionary< string, Type > _factoryDictionary = new Dictionary< string, Type >();

        public Factory()
        {    
            Type[] types = Assembly.GetAssembly(typeof (T)).GetTypes();

            foreach (Type type in types)
            {
                if (!typeof (T).IsAssignableFrom(type) || type == typeof (T))
                {
                    // Incorrect type
                    continue;
                }

                // Add the type
                _factoryDictionary.Add(type.Name, type);
            }
        }

        public T Create< V >(params object[] args)
        {
            return (T) Activator.CreateInstance(_factoryDictionary[typeof (V).Name], args);
        }
    }
}
+4

, , , , :

public static class Factory<T<>>
{
    public static T<TU> CreateInstance<TU>() { /* ... */ }
}

? ?

var myServiceCall = Factory<ServiceCall<>>.CreateInstance<MyResult>();

factory ...

public static class Factory<T>
{
    public static T CreateInstance() { /* ... */ }
}

... ...

public static class Factory
{
    public static T CreateInstance<T>() { /* ... */ }
}

... ?

var myServiceCall = Factory<ServiceCall<MyResult>>.CreateInstance();
var myServiceCall = Factory.CreateInstance<ServiceCall<MyResult>>();

, Im , Id, , , Ninject factory.

+1

Source: https://habr.com/ru/post/1757699/


All Articles