public class TheController : Controller
{
IThe the;
public TheController(IThe the)
{
//when User.IsInRole("r1") The1 should be injected else r2
this.the = the;
}
}
public class The1 : IThe{}
public class The2 : IThe{}
//anybody knows a good way of doing this ?
1
A
回答
2
IHandlerSelector
是要走的路。有關使用示例,請參閱this post。
或者,如果你喜歡AutoFac般的體驗,您可以使用廠家爲:
container.Register(Component.For<IThe>().UsingFactoryMethod(
c => HttpContext.Current.User.IsInRole("r1") ?
c.Resolve<IThe>("r1") :
c.Resolve<IThe>("r2"));
或者,如果你想只在一個方面IThe
具體使用時,可以使用DynamicParameters:
container.Register(Component.For<TheFactory>().Lifestyle.Transient.DynamicParameters(
(c, d) => HttpContext.Current.User.IsInRole("r1") ?
d["the"] = c.Resolve<IThe>("r1") :
c.Resolve<IThe>("r2"));
但是最正確的正確這樣做的方式是IHandlerSelector
1
在Autofac:
var builder = new ContainerBuilder();
// Give the different implementations names
builder.RegisterType<The1>.Named<IThe>("r1");
builder.RegisterType<The2>.Named<IThe>("r2");
// Use a function for the default IThe
builder.Register(
c => HttpContext.Current.User.IsInRole("r1") ?
c.Resolve<IThe>("r1") :
c.Resolve<IThe>("r2"))
.As<IThe>()
.ExternallyOwned();
如果你有很多角色,你可以使用的方法,而不是內置表達式,如:
builder.Register(c => ChooseTheImplementation(c))
(順便說一句,在「ExternallyOwned」修飾符告訴容器函數的結果放置在別處,例如通過具體組件。通常你可以離開它,但它是一個很好的文件:))
1
的container-agnostic做法顯然採用了Abstract Factory:
public interface ITheFactory
{
IThe Create(IPrincipal user);
}
您可以採取ITheFactory的依賴,而不是i將的:
public class TheController : Controller
{
private readonly IThe the;
public TheController(ITheFactory theFactory)
{
if (theFactory == null)
{
throw new ArgumentNullException("theFactory");
}
this.the = theFactory.Create(this.User);
}
}
我真的不記得this.User
這個時候是否被填充,但如果不是,你可以保留對工廠的引用,並在第一次請求時懶散地解析你的依賴。
但是,Controller.User
有點特別,因爲它也應該可以作爲Thread.CurrentPrincipal
。這意味着在這種特殊情況下您實際上並不需要引入抽象工廠。取而代之的是,你可以寫一個Decorator執行的每一個選擇它的使用時間:
public class UserSelectingThe : IThe
{
private readonly IThe the1;
private readonly IThe the2;
public UserSelectingThe(IThe the1, IThe the2)
{
if (the1 == null)
{
throw new ArgumentNullException("the1");
}
if (the2 == null)
{
throw new ArgumentNullException("the2");
}
this.the1 = the1;
this.the2 = the2;
}
// Assuming IThe defines the Foo method:
public Baz Foo(string bar)
{
if (Thread.CurrentPrincipal.IsInRole("r1"))
{
return this.the1.Foo(bar);
}
return this.the2.Foo(bar);
}
}
在這種情況下,你將能夠使用原來的TheController類不變。
相關問題
- 1. 登錄 - 不同的用戶角色
- 2. JSP登錄用戶角色
- 3. 不同的用戶角色登錄使用PHP和Mysql
- 4. 如何爲不同用戶登錄的角色工作?
- 5. PHP根據用戶的「角色」登錄
- 6. 如何確定登錄用戶角色
- 7. 如何創建角色用戶登錄?
- 8. 登錄角色vs用戶密碼錶
- 9. 重定向到不同的URL爲不同的角色登錄
- 10. 針對不同登錄角色的不同頁面
- 11. Sql注入登錄繞過
- 12. 檢索在線用戶通過登錄和註銷記錄
- 13. 過濾用戶可以通過角色
- 14. ASP.NET登錄角色?
- 15. 通過PHP登錄到MySQL數據庫帳戶,同時實現行級安全
- 16. 爲通過Facebook登錄認證的用戶呈現Facebook關注按鈕?
- 17. 在wordpress中以用戶的用戶角色登錄
- 18. 根據用戶角色登錄重定向到不同的頁面後
- 19. 已登錄的用戶使用「FOSUser Bundle」而不先登出的「角色提升」
- 20. 向不同角色指定不同內容後未登錄的用戶顯示不同的內容
- 21. 註冊用戶角色
- 22. 訪問Wordpress用戶數據通過應用REST登錄/註冊
- 23. 角度不同的用戶角色的不同菜單項
- 24. 實現用戶登錄,JDBC Transactions問題
- 25. 如何創建一個擁有3個不同角色和一個地點的用戶來註冊/登錄?
- 26. 通過表單更改用戶角色
- 27. 如何通過角色獲取用戶
- 28. laravel 5根據用戶角色登錄後重定向用戶
- 29. 依賴注入的實現像角
- 30. 如何僅通過登錄頁面接受註冊用戶
據我瞭解這個答案,你可以在應用程序根目錄中執行此註冊。那時,'User'不可用(它是Controller類的一個屬性)。 – 2010-03-26 05:50:50
固定 - 現在使用HttpContext。謝謝馬克。 – 2010-03-26 10:06:10
是溫莎城堡能做這種東西嗎? – Omu 2010-03-26 19:10:02