C # - type parameters in the constructor - no generics

I have a class I'm trying to do unit tests on. A class is a WCF service class. (Making a generic class is not my goal.)

I have a data access level (DAL) type (called UserDAL) that is created in several ways. To test these methods, I need these local variables to laugh. (Each instance of UserDAL has a method value defined in it, so changing its class level variable will result in messy code, so I would not do that.)

What I think would be nice is to overload the constructor and pass in the type that will be used in local methods. An empty pair constructor will still create a regular UserDAL, but the overloaded one will have a layout of the type that implements IUserDAL.

I am not sure of the syntax to say that I want to pass a type. Please note that I am not trying to pass a variable, but a type.

Example:

public class MyWCFClass: IMyWCFClass
{
    private TypeParam _myUserDALType;
    public MyWCFClass()
    {
        _myUserDALType = UserDAL;
    }
    public MyWCFClass(TypeParam myUserDALType)
    {
        _myUserDALType = myUserDALType;
    }

    //methods to use it
    public MyMethod()
    {
        IUserDAL userDAL = new _myUserDALType();
        //Call method in IUserDAL
        userDAL.CreateUser();
    }


    // Several similar methods that all need a different UserDAL go here
    .....
}

So, I do not know what type of TypeParam (I did this), or if such an approach is even possible.

If you have a non generics solution that would be great.

+3
source share
8 answers

You mean Type, using Activator.CreateInstanceto create instances:

public class MyWCFClass: IMyWCFClass
{
    private Type _myUserDALType;
    public MyWCFClass()
    {
        _myUserDALType = typeof(UserDAL);
    }
    public MyWCFClass(Type myUserDALType)
    {
        _myUserDALType = myUserDALType;
    }

    //methods to use it
    public void MyMethod()
    {
        IUserDAL userDAL = (IUserDAL) Activator.CreateInstance(_myUserDALType );
        //Call method in IUserDAL
        userDAL.CreateUser();
    }
}
+5
source

, , - , , , Activator.CreateInstance(Type), , .

DI ( ), , Spring.Net .

+8

Activator.CreateInstance :

private Type _myUserDALType;

IUserDAL userDAL = Activator.CreateInstance(_myUserDALType) as IUserDAL;
+5

. , MyWFCClass new, . Misko Hevery , , new , . , MyWFCClass - , , IUserDal , , .

- , , . Type MyWFCClass, , .

+3

, Type:

public class A
{
  public A(Type classType)
  {
    object myObject = Activator.CreateInstance(...classType...);
  }
}

public class B
{
...
}

public class C
{
  public static void main(string[] args)
  {
    A a = new A(typeof(B));
  }

}

+2

IUserDAL , WCF , ? WCF , ?

public class MyWCFClass : IMyWCFClass
{
    private readonly IUserDAL _userDAL;

    public MyWCFClass()
        : this(new DefaultUserDAL())
    {
    }

    public MyWCFClass(IUserDAL userDAL)
    {
        _userDAL = userDAL;
    }
}

, , :

public MyWCFClass()
    this(Container.Instance.Resolve<IUserDAL>())
{
}

WCF , . , , .

+1

, , UserDal, - :

public MyWCFClass() : this(new UserDAL())
{
}
public MyWCFClass(IUserDal userDAL)
{
    _myUserDAL = myUserDAL;
}

, , , ,

(, , )

If your DAL is practically useless after use because it is mutated, take a constructor with IUserDalFactory in one method instead Create().

+1
source

In C # there is a type called "Type". With it, you can create a parameter and pass any valid type.

private void MyMethod(Type myType)
{
  //Do something
}
0
source

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


All Articles