samedi 13 août 2022

Error in CreateInstance() while dynamically creating object of concrete type in Factory Pattern

I am actually new to design patterns and trying to implement factory pattern with .net core.

I tried to see couple of posts related to factory pattern and trying to implement it, I have added the concrete types in the config and reading it as dictionary in my code -

My Factory Interface -

public interface IEmpFactory
{
    public BaseEmployee CreateEmployeeType<EmpType>() where EmpType : BaseEmployee, new();
}

Implementation -

public class EmpFactoryImpl : IEmpFactory
{
    public BaseEmployee CreateEmployeeType<EmpType>() where EmpType: BaseEmployee, new()
    {
        return new EmpType();
    }
}

Below are my services which are using the Factory as dependency -

public interface IEmpService
{
    public string GetEmployeeBonus();
}

public class ContractEmpService : IEmpService
{
    IEmpFactory _empFactory;

    public ContractEmpService(IEmpFactory empFactory)
    {
        _empFactory = empFactory;
    }
    private BaseEmployee CreateMyEmployee()
    {
        return _empFactory.CreateEmployeeType<ContractEmp>();
    }

    public string GetEmployeeBonus()
    {
        return CreateMyEmployee().GetBonus();
    }
}

public class PermEmpService : IEmpService
{
    private readonly IEmpFactory _empFactory;

    public PermEmpService(IEmpFactory empFactory)
    {
        _empFactory = empFactory;
    }

    private BaseEmployee CreateMyEmployee()
    {
        return _empFactory.CreateEmployeeType<PermEmp>();
    }

    public string GetEmployeeBonus()
    {
        return CreateMyEmployee().GetBonus();
    }
}

Added these concrete types in the config -

enter image description here

Created the class to create a instance of the concrete type based on the type i.e, PermEmp or ContractEmp dynamically -

public class EmployeeTypeRouter : IEmployeeTypeRouter
{
    private readonly ConfigurationProps _props;

    public EmployeeTypeRouter(ConfigurationProps props)
    {
        _props = props;
    }

    public IEmpService GetInstance(string key)
    {
        string className = _props.EmpServices.Where(k => k.Key.Equals(key)).FirstOrDefault().Value;

        Type t = Type.GetType(className);

        return (IEmpService)Activator.CreateInstance(t);
    }
}

This is my calling method -

    [HttpGet(Name = "GetEmployeeBonus")]
    public string Get()
    {
        string type = "PermEmp";
        IEmpService empService = _empRouter.GetInstance(type);

        return empService.GetEmployeeBonus();
    }

based on the type passed here i want to fetch the concrete type and call the method.

I am getting the error like this on CreateInstance method -

enter image description here

Which is very clear, but i dont want to create a parameterless constructor.

Since i am registering the dependencies in .net core, do i need to pass it again here ? (which does not make sense for me)

Any help is really appreciated or if you feel i am doing something wrong please let me know.

Aucun commentaire:

Enregistrer un commentaire