3

Tried to search the web but found nothing so far so here my question:

I want to index model-information via attributes on the different members. To do this i created a function in a base class that gathers all needed information when called. This method is derived into the different models so that they can all be indexed.

Base()
{
    public virtual void Index() {...}
}

In the base class I'm calling a generic method that gives me acces to the indexing server for the specific model that I want to save there

using (var indexFacade = IndexFacadeFactory.GetIndexFacade(configuration, this))
{
    indexFacade.Execute(this, operation);
}

The issue I'm currently having is that when calling the factory it retrieves the information for the base-class.

What I want to acomplish is something like this:

Derived : Base
{
   [IndexingKey]
   long Id { get; set; }

   [IndexingField]
   string SomeValue { get; set; }
}

var derived = new Derived();
derived.Index();

My indexFacade holds the type of

IndexFacadeBase<Base>

I'm aware of the polimorphism here and why this happens.

My question is: How can i call

derived.Index();

so that the context from which it is called is not from the base-class without overwriting it?

Further information:

The method that is called looks like this:

public static IndexFacadeBase<T> GetIndexFacade<T>(IndexInfo.IndexConfiguration config, T model)
        {
            IndexFacadeBase<T> retVal;
            .....
            return retVal;
        }

The T has the type of Base.

The model has the type of Derived.

Maybe that clears up some of the Problems.

I get back:

  IndexFacadeBase<Base>

I would need back:

   IndexFacadeBase<Derived>

Thanks in advance for all the help.

Care.Inc
  • 31
  • 3
  • 1
    What do you mean by "derived context"? – Matthew Watson Mar 08 '17 at 12:38
  • @MatthewWatson exactly. What kind of information you want to pass? You can always do this.GetType() and retrieve an actual derived type. – MistyK Mar 08 '17 at 12:48
  • I have the IndexFacadeBase when i call derived.Index() I would need IndexFacadeBase. The type comes from a generic and since it's called from the base the context results in IndexFacadeBase – Care.Inc Mar 08 '17 at 12:49

2 Answers2

2

I may not be fully understanding your question, but aren't you simply looking to override the method in the derived class?

class Base
{
    public virtual void Index() { }
}

class Derived : Base
{
    public override void Index() { } // here is the override.
    long Id { get; set; }
    string SomeValue { get; set; }
}

Then when you do this:

var derived = new Derived();
derived.Index();

The derived class' Index method is called.

rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • It would work that way, yes. The Problem just would be that i would have to copy/paste the whole block into the different models. they way it currently is would do everything without the need to copy it – Care.Inc Mar 08 '17 at 12:46
0

Maybe it would work if your IndexFacadeBase<Base> was changed to IndexFacadeBase<T> where T:Base.

Connell.O'Donnell
  • 3,603
  • 11
  • 27
  • 61
  • IndexFacadeBase is the type resulting from the call ' public static IndexFacadeBase GetIndexFacade(IndexInfo.IndexConfiguration config, T model) { IndexFacadeBase retVal; } ' this is the method in short that is called. T is of type Base model is of type Derived – Care.Inc Mar 08 '17 at 13:11
  • I'm sorry the formatting in the answer is ugly i will post a regular answer to get the formatting right... – Care.Inc Mar 08 '17 at 13:13