1

I have a GenericController which has the following methods:

public class GenericGridController<T, TKey>:Controller
    where T : class
    where TKey : IComparable
{
    string GetFilterParam();
    string GetGridPartialName();
    List<T> GetGridModel(TKey param);
    List<T> GetGridModel(T entity);
    ActionResult GetGrid(TKey param);
    ActionResult Add(T entity);
    ActionResult Edit(T entity);
    ActionResult Delete(T entity);
}

When ever I need to create a grid, I am creating a new controller, inherited from the GenericGridController and I need to override the GetFilterParam and GetGridParialName, to provide the specific names.

This is working just fine. Now I want to not override the 2 methods, and:

I was trying to do "something which I do not understand": - I made 2 string properties in the generic controller - I initialized them from the constructor, something like:

    public string FilterParam { get; set; }
    public string GridPartialName { get; set; }

    public GenericGridController(string filterParamName, string partialName)
    {
        FilterParam = filterParamName;
        GridPartialName = partialName;
    }

then I create a new TestController, inherited from the GenerigGridController, and I saw that he ask to implement the missing contructor, something like this:

public class TestController : GenericGridController<Candidat,int>
    {
        public TestController(string filterParamName, string partialName)
            : base(filterParamName, partialName)
        {

        }        
    }

I was expecting to do something like this:

 public class TestController : GenericGridController<Candidat,int>("param","PartialView"){}

My question is: How to provide the 2 parameters need it in the GenericGridController constructor.

Maybe is a stupid question, I am just trying to understand how this works.

3
  • Found the GenericGridController. Now what is the GenericGridView you were talking about. Commented Mar 21, 2016 at 20:56
  • Sorry, is GenericGridController Commented Mar 21, 2016 at 20:59
  • @LucianBumb you cannot pass what i see, may be writing custom controller facotry may work, not sure Commented Mar 21, 2016 at 21:14

3 Answers 3

2

You cannot have a parameter value as part of a class declaration.

Two solutions come to my mind:

  1. Use properties with default values and overwrite in derived classes public string FilterParam { get; set; } public string GridPartialName { get; set; }

    public GenericGridController() { this.FilterParam = "defaultFilterParamValue"; this.GridPartialName = "defaultGridPartialNameValue"; }

and in the derived class

public DerivedGridController()
{
    this.FilterParam = "foo";
    this.GridPartialName = "derived";
}
  1. declare those properties as abstract

public abstract string FilterParam { get; } public abstract string GridPartialName { get; } Using abstract forces every derived class to implement those properties returning a constant or computed value

public override string FilterParam { get { return "myFilterParam"; } }
Sign up to request clarification or add additional context in comments.

Comments

2

To restate my understanding of this issue: you are looking to pass static values to the base constructor from a derived class.

It looks like you attempted to do this in the class definition. This will not work. However, you can declare your own constructor and change the parameters. Here is an example:

public class TestController : GenericGridController<Candidat,int>
{
    public TestController()
        : base("param","PartialView")
    {

    }        
}

Now you can instantiate this class with new TestController() and it will automatically provide "param" and "PartialView" values to the base constructor for you.

Comments

0

I reckon you're looking for something like this:

public class TestController : GenericGridController<Candidat,int>
{
    public TestController()
        : base("param","PartialView")
    {

    }
}

Having defined a parameterless constructor, you can now create instances of TestController simply as new TestController().

However, creating a whole another class only to hardcode different values for constructor parameters of a base class (or indeed override 2 getters to return some hardcoded value) is questionable to say the least.

I was expecting to do something like this:

class TestController : GenericGridController("param","PartialView"){}

And this seems to show a lack of understanding on a deeper level. Constructor is invoked when creating an instance of a class, you cannot provide constructor parameters like this, in the declaration of the base class for TestController, as no instance is being created at this point.

1 Comment

The test controller I can use it in a view to get a grid, in this way:@Html.Action("GetGrid","Test", new {candidatId=5})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.