2

I'm trying to pass a parameter for database connection purposes in my apiController constructor. for example by default in my baseAPIController i neeed the code to be '1'. But in this specific controller I need to change that parameter to '7'. The problem is that my BaseAPIController instanciates with '1' and after instantiated even if I send '7' in the property it doesn't change. How can I set "7" in this specific controller before the baseAPI is initialized?

public class ListagemProjetoEletricoWebController :BaseAPIController<ListagemProjetoEletricoBusiness>
{       
    public List<ListagemProjeto> Get(String cpf)
    {
        business.idEmpresa = 7;
        List<ListagemProjeto> listProjetoEletricoWeb = business.GetProjetoEletricoLista(cpf);
        return listProjetoEletricoWeb;
    }
}

and in my baseAPIcontroller I have.

protected override void Initialize(HttpControllerContext controllerContext)
{
    this.business = new TBusiness();
    this.business.idEmpresa = 1;

    this.business.db = new BaseBusiness(this.business.idEmpresa).db; 

    base.Initialize(controllerContext);
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
Ana
  • 41
  • 1
  • 7

1 Answers1

0

Create an virtual property in your base class to represent the idEmpresa

public class BaseAPIController
{
     //Set the default to whatever it is used normally
     public virtual int idEmpresa { get {return 1;} }
}

then in your derrived class you override that property

public class ListagemProjetoEletricoWebController :BaseAPIController<ListagemProjetoEletricoBusiness>
{
    public override int idEmpresa { get { return 7;} } 
}

Then when you set your business idEmpresa use the property instead

this.business.idEmpresa = this.idEmpresa;
johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • Hi Johnny, I did as instructions and I get the error "The modifier 'virtual' is not valid for this item". – Ana Mar 28 '18 at 21:18
  • sorry the derrived class should use the keyword override – johnny 5 Mar 28 '18 at 21:18
  • @ana I've updated my answer, If that works for you please upvote and mark as correct ;p – johnny 5 Mar 28 '18 at 21:19
  • 1
    Also, I've changed the => to just '=' because of sintax errors; – Ana Mar 28 '18 at 21:26
  • @Ana that syntax is for properties I guess your not on C# 7 Hold on Ill make another update – johnny 5 Mar 28 '18 at 21:27
  • @Ana when you switched to use = that changed the properties to a field. The => is short hand for Property Getter syntax in c# 7.0 – johnny 5 Mar 28 '18 at 21:29
  • I've market as useful, but I don't have enought reputation for now. As soon I get more points I will be back. promisse – Ana Mar 28 '18 at 21:44
  • @Ana there should be there is a checkmark next to the answer you can click, which marks it as answered. there also is an up arrow to upvote the question – johnny 5 Mar 28 '18 at 21:53