2009-07-02 25 views
7

我想配置NCommon NHRepository我帶結構的地圖項目。我如何阻止它選擇最貪婪的構造函數?結構地圖 - 我不想使用貪婪的構造!

public class NHRepository<TEntity> : RepositoryBase<TEntity> 
{ 

    public NHRepository() {} 


    public NHRepository(ISession session) 
    { 
     _privateSession = session; 
    } 

    ... 
} 

我的結構圖配置

ForRequestedType(typeof (IRepository<>)) 
       .TheDefaultIsConcreteType(typeof(NHRepository<>)) 

乾杯 傑克

回答

8

您可以設置[DefaultConstructor]屬性爲要作爲默認構造函數。在你的情況下,將其設置在NHRepository()構造函數將其設置爲默認constuctor爲StructureMap初始化。

更新:嗯,在最新版本的StructureMap的,使用.NET 3.5,你也可以使用SelectConstructor方法指定:

var container = new Container(x => 
{ 
    x.SelectConstructor<NHRepository>(()=>new NHRepository()); 
}); 

最後,我敢肯定,你將能夠在定義它StructureMap的XML配置,但我沒有使用過。你可以對它進行一些搜索。有關上述方法的詳細信息,請參閱:http://structuremap.sourceforge.net/ConstructorAndSetterInjection.htm#section3

+0

金酸莓嗨,尋找答案的歡呼聲,不過NHRepository是我無法改變的NCommon.NHibernate.dll。那麼我應該從我的項目中的NHRepository繼承,並將其屬性放在其構造函數中嗎? – superlogical 2009-07-02 11:22:43

1

所以+1的金酸莓因爲如果NHRepository是我自己組裝這樣的工作,而不是我選擇我自己喜歡庫下面包裹NHRepository ..

public class Repository<T> : NHRepository<T> 
{ 
    [DefaultConstructor] 
    public Repository() 
    { 

    } 

    public Repository(ISession session) 
    { 

    } 
} 

ForRequestedType(typeof (IRepository<>)) 
       .TheDefaultIsConcreteType(typeof (Repository<>));