2015-07-08 23 views
8

Mam aplikację, którą projektuję, która odwołuje się do biblioteki, którą również projektuję. W szczególności aplikacja musi wykonywać instancje klasy Sheathing zdefiniowanej w mojej bibliotece niższego poziomu.Dependency Injection z TypeConverters

[TypeConverter(typeof(SheathingOptionsConverter))] 
public class Sheathing : Lumber 
{ 
    public string Description { get; set; } 

    public Sheathing(string passedDescription) 
    { 
     Description = passedDescription; 
    } 
} 

Moja aplikacja zawiera listę różnych opcji poszycia w siatce właściwości. Ponieważ wyświetla je w rozwijanym menu, musiałem dwukrotnie rozszerzyć ExpandableObjectConverter. Pierwszy poziom w dół to moja SheathingObjectConverter który prawidłowo wyświetla pojedynczy obiekt poszycie

public class SheathingObjectConverter : ExpandableObjectConverter 
{ 
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     if (destinationType == typeof(Sheathing)) 
     { 
      return true; 
     } 
     return base.CanConvertTo(context, destinationType); 
    } 

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    { 
     if (sourceType == typeof(string)) 
     { 
      return true; 
     } 
     return base.CanConvertFrom(context, sourceType); 
    } 

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType) 
    { 
     if (destinationType == typeof(System.String) && value is Sheathing) 
     { 
      Sheathing s = (Sheathing)value; 
      return "Description: " + s.Description; 
     } 
     return base.ConvertTo(context, culture, value, destinationType); 
    } 

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
    { 
     if (value is string) 
     { 
      try 
      { 
       string description = (string)value; 
       Sheathing s = new Sheathing(description); 
       return s; 
      } 
      catch 
      { 
       throw new ArgumentException("Can not convert '" + (string)value + "' to type Sheathing"); 
      } 
     } 
     return base.ConvertFrom(context, culture, value); 
    } 
} 

a drugi poziom w dół rozciąga SheathingObjectConverter aby wyświetlić listę obiektów poszycie jako menu rozwijanego w siatce nieruchomości

public class SheathingOptionsConverter : SheathingObjectConverter 
{ 
    /// <summary> 
    /// Override the GetStandardValuesSupported method and return true to indicate that this object supports a standard set of values that can be picked from a list 
    /// </summary> 
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context) 
    { 
     return true; 
    } 

    /// <summary> 
    /// Override the GetStandardValues method and return a StandardValuesCollection filled with your standard values 
    /// </summary> 
    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) 
    { 
     List<Sheathing> sheathingDescriptions = SettingsController.AvailableSheathings; //ToDo: Fix needing a list from the application (higher level) 
     return new StandardValuesCollection(sheathingDescriptions.ToArray()); 
    } 
} 

Tutaj leży problem; cały powyższy kod znajduje się w mojej bibliotece niższego poziomu, ale SettingsController jest klasą w mojej aplikacji wyższego poziomu, ponieważ tam jest zdefiniowana lista osłon. Zwykle ten problem można rozwiązać za pomocą wtrysku zależności, ale ponieważ dotyczy to typekonwerterów, nie jestem pewien, czy możliwe jest użycie iniekcji zależności. Nie wiem, jak rozwiązać ten problem.

Odpowiedz

0

Argument context jest zamierzonym punktem wtrysku.

Utwórz klasę tak:

class SheathingContext : ITypeDescriptorContext 
{ 
    public List<Sheathing> AvailableSheathings 
    { 
     get { return SettingsController.AvailableSheathings; } 
    } 
} 

Następnie przekazać go jako context do konwertera typu. W konwerterze typów można użyć listy ((SheathingContext)context).AvailableSheathings.

+1

Nie musisz zaimplementować reszty interfejsu? –

+1

@Jake Smith Tak, ale te pola mogą po prostu zwrócić wartość null. Zobacz notatkę na stronie https://msdn.microsoft.com/en-us/library/system.componentmodel.itypedescriptorcontext(v=vs.110).aspx – dss539

Powiązane problemy