When I need some element, do I need to use its "int id" instead?

In my application, this InstrumentFactoryis the only place I create an instance Instrument. Each instance of the tool contains several fields, such as Ticker=MSFTand GateId=1, as well as unique Id =1.

And now I realized that I almost did not need a copy Instrument. In 90% of cases, I just need to Id. For example, now I have a method like this:

public InstrumentInfo GetInstrumentInfo(Instrument instrument)
{
    return instrumentInfos[instrument.Id];
}

We know that we should not pass parameters more information than required. Therefore, this code should probably be reorganized into:

public InstrumentInfo GetInstrumentInfo(int instrumentId)
{
    return instrumentInfos[instrumentId];
}

90% of my code can now be reorganized for use instrumentIdinstead Instrument.

? Instrument instrumentId ( ). ? " " ... (, ?) .

+5
5

ids , , , .

:

  • . - Person , person.Id .
  • . , long ids - Instrument, .

, , , - Dictionary<Instrument, InstrumentInfo>, Dictionary<int, InstrumentInfo>, . , . , Instrument, Equals() GetHashCode() IEquatable<Instrument>.

+4

, , . , , ID, Instrument .

+1
GetInstrumentInfo(int instrumentId);

, , , :

GetInstrumentInfo(instrument.Id);

. .

. Int .

, GetInstrumentInfo, , Int.

+1

, :

" ID = 53, , , , , , , , ?"

, " . - , , , , ( - , )" :

-, , , . , , , int, , Instrument . , Instrument internal private, factory, , Instrument , .

:

public class Instrument : IEquatable<Instrument>
{
  /* all the useful stuff you already have */
  public bool Equals(Instrument other)
  {
    return other != null && Id == other.Id;
  }
  public override bool Equals(object other)
  {
    return Equals(other as Instrument);
  }
  public override int GetHashCode()
  {
    return Id;
  }
}

, , , , , , ID , , .

:

public InstrumentInfo GetInstrumentInfo(Instrument instrument)
{
    return instrumentInfos[instrument];
}

:

public InstrumentInfo GetInstrumentInfo(Instrument instrument)
{
    return instrumentInfos[instrument.Id];
}

:

public InstrumentInfo GetInstrumentInfo(Instrument instrument)
{
    return GetInstrumentInfo(instrument.Id);
}
private InstrumentInfo GetInstrumentInfo(int instrumentID)
{
    return instrumentInfos[instrumentID]
}

, , . , , , . , .

, - ( ) . , , , (, , , ), ; .

, .

+1

You can overload every function that takes the tool and one that takes the identifier:

public InstrumentInfo GetInstrumentInfo(Instrument instrument)
{
    // call GetInstrumentInfo passing the id of the object
    return GetInstrumentInfo[instrument.Id];
}

public InstrumentInfo GetInstrumentInfo(int instrumentId)
{
    return instrumentInfos[instrumentId];
}

This will give you enough flexibility so that while you go through any place that calls GetInstrumentInfoto change it to pass id, the current code will still function.

As for whether you really ought to, it is up to you. You would have to weigh how long it would take to change it, in favor of making changes to the code.

-1
source

All Articles