Thema: Delphi Unsatisfied Constructor

Einzelnen Beitrag anzeigen

ngott2

Registriert seit: 26. Aug 2013
81 Beiträge
 
Delphi 10 Seattle Professional
 
#1

Unsatisfied Constructor

  Alt 23. Mär 2017, 05:02
Guten Morgen,
Ich ärgere mich gerade mit ein Problem rum, wo ich einfach nicht weiter komme...
Ich habe eine Klasse geschrieben TMatchStorage. Die hat einige Methode und auch ein Constructor. Nun kommen wir zu dem Problem. Wenn ich über GlobalContainer.Resolve<IMatchStorage>; mir eine Instanz holen möchte bekomme ich die angehängte Fehlermeldung. Wenn ich in einer der Implementierten Funktionen ein Haltepunkt setze sieht es so aus wie wenn man ein Haltepunkt setzt in einer Zeile wo gar kein Quellcode ist. Seht ihr wo mein dummer Fehler ist ? Ich habe eine Klasse die genau gleich aufgebaut ist und dort funktioniert es... Das einzige was unterschiedlich zwischen den Klassen ist die GUID beim Interface, der Klassenname und TMach ist durch TTeam an jeder stelle ausgetauscht.

Delphi-Quellcode:
unit SEP.Storage.Match;

interface

uses
  SEP.Match,
  System.Generics.Collections,
  Spring.Container,
  XML.XMLDoc,
  XMLIntf,
  Winapi.ActiveX,
  System.SysUtils;

type
  IMatchStorage = interface
    ['{0541D06C-3C1B-40B6-923F-AA715713CDB9}']
    function AddMatchToStorage: TMatch;
    function GetMatchList: TObjectList<TMatch>;
    function GetMatchListAsDictionary: TDictionary<Integer, TMatch>;
    function GetMatchByID(aID: Integer): TMatch;
    function DeleteMatchFromStorage(aID: Integer): Boolean;
    procedure Save;
    procedure Load;
  end;

  TMatchStorage = class(TInterfacedObject, IMatchStorage)
  strict Private
    fMatch: TDictionary<Integer, TMatch>;
    fXML: IXMLDocument;
  public
    function AddMatchToStorage: TMatch;
    function GetMatchList: TObjectList<TMatch>;
    function GetMatchListAsDictionary: TDictionary<Integer, TMatch>;
    function DeleteMatchFromStorage(aID: Integer): Boolean;
    function GetMatchByID(aID: Integer): TMatch;
    procedure Save;
    procedure Load;
    constructor Create;
    destructor Destroy; override;
  end;

implementation

{ TMatchStorage }

function TMatchStorage.AddMatchToStorage: TMatch;
var
  lIndex: Integer;
begin
  Result := TMatch.Create;
  Result.ID := fMatch.Count + 1;
  if not fMatch.ContainsKey(Result.ID) then
  begin
    fMatch.Add(Result.ID, Result);
    Exit;
  end;
  for lIndex := 0 to fMatch.Count do
  begin
    if not fMatch.ContainsKey(lIndex + 1) then
    begin
      Result.ID := lIndex + 1;
      fMatch.Add(Result.ID, Result);
      Exit;
    end;
  end;
end;

constructor TMatchStorage.Create;
begin
  fMatch := TDictionary<Integer, TMatch>.Create();
  OleInitialize(nil);
  fXML := NewXMLDocument();
end;

function TMatchStorage.DeleteMatchFromStorage(aID: Integer): Boolean;
begin
  fMatch.Remove(aID);
  Result := true;
end;

destructor TMatchStorage.Destroy;
var
  lIndex: Integer;
begin
  for lIndex := 0 to fMatch.Count - 1 do
    fMatch.Values.ToArray[lIndex].Free;
  fMatch.Free;
  inherited;
end;

function TMatchStorage.GetMatchByID(aID: Integer): TMatch;
begin
  Result := fMatch.Items[aID];
end;

function TMatchStorage.GetMatchList: TObjectList<TMatch>;
var
  lIndex: Integer;
begin
  Result := TObjectList<TMatch>.Create(False);
  for lIndex := 0 to fMatch.Count - 1 do
    Result.Add(fMatch.Values.ToArray[lIndex])
end;

function TMatchStorage.GetMatchListAsDictionary: TDictionary<Integer, TMatch>;
begin

end;

procedure TMatchStorage.Load;
var
  lMatch: TMatch;
  lNode: IXMLNode;
  lChildNode: IXMLNode;
  lIndex: Integer;
begin
  fMatch.Clear;
  fXML.LoadFromFile('Match.xml');
  lNode := fXML.DocumentElement;
  for lIndex := 0 to lNode.ChildNodes.Count - 1 do
  begin
    lChildNode := lNode.ChildNodes.Nodes[lIndex];
    lMatch := TMatch.Create;
    lMatch.ID := Integer(lChildNode.GetAttributeNS('ID', ''));
    lMatch.Team1.ID := Integer(lChildNode.GetAttributeNS('Team1', ''));
    lMatch.Team2.ID := Integer(lChildNode.GetAttributeNS('Team2', ''));
    lMatch.Goals1 := Integer(lChildNode.GetAttributeNS('Goals1', ''));
    lMatch.Goals2 := Integer(lChildNode.GetAttributeNS('Goals2', ''));
    fMatch.Add(lMatch.ID, lMatch);
  end;
end;

procedure TMatchStorage.Save;
var
  lMatch: TMatch;
  lNode: IXMLNode;
  lTeamNode: IXMLNode;
  lKey: string;
begin
  if not Assigned(fXML.DocumentElement) then
  begin
    lNode := fXML.AddChild('Match');
  end
  else
  begin
    lNode := fXML.DocumentElement;
  end;
  lNode.ChildNodes.Clear;
  for lMatch in fMatch.Values do
  begin
    lKey := 'Match' + intToStr(lMatch.ID);
    lTeamNode := lNode.ChildNodes.FindNode(lKey);
    if not Assigned(lTeamNode) then
      lTeamNode := lNode.AddChild(lKey);
    lTeamNode.SetAttributeNS('Team1', '', lMatch.Team1.ID);
    lTeamNode.SetAttributeNS('Team2', '', lMatch.Team2.ID);
    lTeamNode.SetAttributeNS('ID', '', lMatch.ID);
    lTeamNode.SetAttributeNS('Goals1', '', lMatch.Goals1);
    lTeamNode.SetAttributeNS('Goals2', '', lMatch.Goals2);
  end;
  fXML.SaveToFile('Match.xml');
end;

end.
Hier noch meine Container Registrierung

Delphi-Quellcode:
procedure RegisterTypesForSettings(const container: TContainer);
begin
  container.AddExtension<TDecoratorContainerExtension>;

  container.RegisterType<TOrderEntryTransactionDecorator>;
  container.RegisterType<TOrderEntryLoggingDecorator>;
  container.RegisterType<TSettingsEntry>;
  container.RegisterType<TOrderValidatorLoggingDecorator>;
  container.RegisterType<TOrderValidator>;
  container.RegisterType<TSettingsProcessor>;
  container.RegisterType<TUser>;
  container.RegisterType<TTeamStorage>.AsSingleton();
  container.RegisterType<IMatchStorage>.AsSingleton();
  container.Build;
end;
und der Aufruf


Delphi-Quellcode:
procedure TPresentGUI.SetupPesentMode;
var
  lGroup : TGroup;
  lMatch : TMatch;
  lTeam : TTeam;
  lPair,
  lBestValue,
  lValue : TPair<Integer,TGroupPlace>;
  lItem : TListItem;
  lIsAdded : TList<Integer>;
begin
  lGroup := TGroup.Create;
  fSettingsStorage := GlobalContainer.Resolve<ISettingsProcessor>;
  fTeamStorage := GlobalContainer.Resolve<ITeamStorage>;
  fMatchStorage := GlobalContainer.Resolve<IMatchStorage>; // Hier findet er den Consturctor nicht
  fMatchStorage.Load;
  fTeamStorage.Load;
  for lTeam in fTeamStorage.GetTeamList do
  begin
    lGroup.fMemberList.Add(lTeam);
  end;

....
Gruß,
ngott2
  Mit Zitat antworten Zitat