2011-06-22 8 views
1

で抽象クラスを構成する方法は、このような構造マップによる登録に問題はありますか?構造マップ

static public class ContainerBootstrapper 
{ 
    static public void BootstrapDefaultContainer(bool test = false) 
    { 
     StructureMap.ObjectFactory.Initialize(x => 
     { 
      x.Scan(p => 
      { 
       p.AssemblyContainingType<IPropertyType>(); 
       p.AddAllTypesOf<IPropertyType>(); 
       //      p.AddAllTypesOf<IPropertyType>().NameBy(c => c.Name); 
      }); 

     }); 
    } 





public interface IPropertyType : IIdentityObject, IPriority 
{ 
    string PropertyName { get; set; } 

    ObjectType ObjectType { get; } 

    string DisplayName { get; set; } 

    IEntityType EntityType { get; set; } 

    IList<IPropertyRuleObject> RuleObjects { get; set; } 

    void AddRuleObject(IPropertyRuleObject ruleObject); 

} 



public abstract class PropertyTypeBase : PersistentObject, IPropertyType 
{ 
    public PropertyTypeBase() 
    { 

    } 

    public PropertyTypeBase(string propertyName, string displayName) 
    { 
     PropertyName = propertyName; 
     DisplayName = displayName; 
    } 

    .... 

} 



public class StringType : PropertyTypeBase 
{ 
    private ObjectType _objectType; 

    public StringType() 
    { 
     _objectType = new ObjectType(typeof(string)); 
    } 

    public StringType(string propertyName, string displayName) 
     : base() 
    { 
     PropertyName = propertyName; 
     DisplayName = displayName; 
    } 

    public override ObjectType ObjectType 
    { 
     get { return _objectType; } 
    } 
} 

ContainerBootstrapper.BootstrapDefaultContainer();私は、エラーの行を参照して実行します。

public IPropertyType GetPropertyType(IIdentityObject identityObject, string name) 
    { 
     string[] Properties = name.Split('.'); 

     object Result = identityObject; 

     foreach (var Property in Properties) 
      Result = Result.GetType().GetProperty(Property).PropertyType.Name; 

     IPropertyType propertyType = StructureMap.ObjectFactory.GetNamedInstance<IPropertyType> (Result + "Type"); 
     if (propertyType==null) 
      throw new Exception("Property type not found"); 

     return propertyType; 
    } 

StructureMap Exception Code: 200 

はPluginType Azarakhsh.Domain.Core.AdaptiveObjectModel.Interface.IPropertyType

、呼び出し元のコードのための "StringType" という名前のインスタンスが見つかりませんでした

何が問題ですか?

+0

を説明するようにあなたは、呼び出し元のコードを追加できるカスタムregistrationconventionを書き込むことができますか? 'ObjectFactory.GetInstance ()'のようなことをしていると思いますか? – thekip

+0

もっとコードを表示できますか?あなたがstructuremapからインスタンスを取得する方法と同様に? 2つのコンストラクタに問題があるかもしれませんが、構造マップで使用するコンストラクタを選択するのが難しくなります。コンストラクタの1つをテストとして削除して、それがうまく動作するかどうかを確認したり、コンストラクタ引数に空の文字列を使用するようにstructuremapを設定したりすることができます。 – Bassetassen

+0

@thekip:IPropertyType propertyType = StructureMap.ObjectFactory.GetNamedInstance (結果+タイプ); // Result = "String" – Adrakadabra

答えて

2

名前付きインスタンスを取得しようとしていますが、指定したコードが表示されているので、インスタンスの名前を付けないでください。インスタンスに名前を付けるコード行がコメントアウトされています。

ここでObjectFactory.GetInstance<IPropertyType>();を使用しても、使用するコンストラクタがわからないので、エラーが発生します。この問題に対する解決策がいくつかあります。あなたが唯一のコンストラクタ

  • マーク[DefaultConstructor]属性を使用して、既定のコンストラクタを持っているので、

      デザインを変更し
    1. 、それが動作します。
    2. あなたはこのようなものを使って手動でのObjectFactoryに登録することができます。。。

      x.For())(使用CTOR( "プロパティ名は")( "someValueの")ですCTOR( "のdisplayName")。 .Is( "someValue");

    3. here

  • 関連する問題