[英]Is there an easy way to register lots of classes in Unity container in a similar way?
我有大約40個接口,例如以下示例:
IDaProduct
IDaStudent
IDaUniversity
…
還有40個實現這些接口的存儲庫類:
RepProduct
RepStudent
RepUniversity
…
還有40個提供程序類也實現了這些接口:
PrvdProduct
PrvdStudent
PrvdUniversity
…
我使用Unity庫以以下方式在容器中注冊這些類型:
container.RegisterType<IDaProduct, RepProduct>("Repository");
container.RegisterType<IDaProduct, PrvdProduct>("Provider");
container.RegisterType<IDaStudent, RepStudent>("Repository");
container.RegisterType<IDaStudent, PrvdStudent>("Provider");
container.RegisterType<IDaUniversity, RepUniversity>("Repository");
container.RegisterType<IDaUniversity, PrvdUniversity>("Provider");
我想知道是否有更簡單,更自動的方式來注冊所有這80個類?
我需要這樣的東西:
container.RegisterType<"All interfaces started by IDa", "All Classes started by Rep">("Repository");
container.RegisterType<"All interfaces started by IDa", "All Classes started by Prvd">("Provider");
Unity可以按約定注冊類型(也稱為自動注冊)
var container = new UnityContainer();
container.RegisterTypes(
AllClasses.FromLoadedAssemblies(),
WithMappings.MatchingInterface,
WithName.Default);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.