6 c# dependency-injection castle-windsor unity-container
我不知道这是不是一个特定的问题,如果可能的话,但是我不得不移植一个使用Castle Windsor到Unity的应用程序,这样就不会依赖非Microsoft认可的库了.我知道我知道但是你打算做什么.
无论如何我已经设法了,但我对我所拥有的东西不满意.在温莎我有这个:
Register(
AllTypes.Of(typeof(AbstractPresenter<>)).FromAssemblyNamed("Links.Mvp"),
AllTypes.Of(typeof(IView)).FromAssemblyNamed("Links.WinForms").WithService.FromInterface());
Run Code Online (Sandbox Code Playgroud)
我已经团结一致地转变为这个
RegisterType<IMainView, MainView>();
RegisterType<IConfigureLinkView, ConfigureLinkView>();
RegisterType<IConfigureSourceView, ConfigureSourceView>();
RegisterType<IConfigureSinkView, ConfigureSinkView>();
RegisterType<MainPresenter, MainPresenter>();
RegisterType<ConfigureLinkPresenter, ConfigureLinkPresenter>();
RegisterType<ConfigureSourcePresenter, ConfigureSourcePresenter>();
RegisterType<ConfigureSinkPresenter, ConfigureSinkPresenter>();
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我必须注册每一件事,而不是能够使用某种自动配置.所以我的问题是:在团结中有更好的方法吗?
谢谢,
亚当.
看看这个:
var container = new UnityContainer();
container
.ConfigureAutoRegistration()
.LoadAssemblyFrom("Plugin.dll")
.IncludeAllLoadedAssemblies()
.ExcludeSystemAssemblies()
.ExcludeAssemblies(a => a.GetName().FullName.Contains("Test"))
.Include(If.Implements<ILogger>, Then.Register().UsingPerCallMode())
.Include(If.ImplementsITypeName, Then.Register().WithTypeName())
.Include(If.Implements<ICustomerRepository>, Then.Register().WithName("Sample"))
.Include(If.Implements<IOrderRepository>,
Then.Register().AsSingleInterfaceOfType().UsingPerCallMode())
.Include(If.DecoratedWith<LoggerAttribute>,
Then.Register()
.AsInterface<IDisposable>()
.WithTypeName()
.UsingLifetime<MyLifetimeManager>())
.Exclude(t => t.Name.Contains("Trace"))
.ApplyAutoRegistration();
Run Code Online (Sandbox Code Playgroud)
凉爽的。此功能尚未统一,但如果您觉得有点雄心勃勃,您可以设置自己的基于约定的注册。下面是一个适用于执行程序集和接口的片段。祝你好运。
PS 这感觉像是一个大黑客,我可能会继续手动注册所有类型。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Forum
{
class Program
{
static void Main(string[] args)
{
// get your assemblies and types you can register
Assembly a = Assembly.GetExecutingAssembly();
var types = a.GetTypes();
var bindTo = from t in types
where t.IsAbstract || t.IsInterface
select t;
// apply your conventions to filter our types to be registered
var interfacePairs = from t in bindTo.Where(x => x.IsInterface)
let match = types.FirstOrDefault(x => x.Name == t.Name.Substring(1))
where match != null
select new Pair { To = t, From = match };
var abstractPairs = new Pair[] {};
// setup the generic form of the method to register the types
var thisType = typeof(Program);
var bindings = BindingFlags.Static | BindingFlags.Public;
MethodInfo genericMethod = thisType.GetMethod("RegisterType", bindings);
// register all your types by executing the
// specialized generic form of the method
foreach (var t in interfacePairs.Concat(abstractPairs))
{
Type[] genericArguments = new Type[] { t.To, t.From };
MethodInfo method = genericMethod.MakeGenericMethod(genericArguments);
method.Invoke(null, new object [] {});
}
Console.ReadKey();
}
public static void RegisterType<To, From>()
{
Console.WriteLine("Register { To: {0} From: {1} }", typeof(To), typeof(From));
}
// Test classes that should be picked up
interface ITest { }
class Test : ITest { }
class Pair
{
public Type To { get; set; }
public Type From { get; set; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3058 次 |
最近记录: |