Sharepoint 2010 中的依赖注入

发布于 2024-10-20 18:48:34 字数 74 浏览 3 评论 0原文

我正在开发我的第一个共享点项目。有没有办法可以在共享点中使用依赖注入(如温莎城堡)? 如果可以,请提供示例代码。

谢谢

I'm working on my first sharepoint project. Is there a way I can use dependency injection (like castle windsor) in sharepoint?
If so can you please provide a samle code.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

躲猫猫 2024-10-27 18:48:34

使用 MS Patterns & 中的 SharePoint 服务定位器实践小组:http://spg.codeplex.com/

Use the SharePoint Service Locator from the MS Patterns & Practices group: http://spg.codeplex.com/

不奢求什么 2024-10-27 18:48:34

你可以像下面这样做。但这并不好,因为 SharepointClass 是由 sharepoint 实例化的,而不是由依赖注入容器实例化的。因此,现在,在 SharepointClass 中,您可以像 Ioc.Resolve() 一样解决依赖关系,并且 IService 实例的更深层次依赖关系将由 Windsor 注入。

public interface IMyCode
{
    void Work();
}

public class MyCode : IMyCode
{
    public void Work()
    {
        Console.WriteLine("working");
    }
}

public interface IService
{
    void DoWork();
}

public class MyService : IService
{
    private readonly IMyCode _myCode;

    public MyService(IMyCode myCode)
    {
        _myCode = myCode;
    }

    public void DoWork()
    {
        Console.WriteLine(GetType().Name + " doing work.");
        _myCode.Work();
    }
}

public class Ioc
{
    private static readonly object Syncroot = new object();
    private readonly IWindsorContainer _container;
    private static Ioc _instance;

    private Ioc()
    {
        _container = new WindsorContainer();
        //register your dependencies here
        _container.Register(Component.For<IMyCode>().ImplementedBy<MyCode>());
        _container.Register(Component.For<IService>().ImplementedBy<MyService>());
    }

    public static Ioc Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (Syncroot)
                {
                    if (_instance == null)
                    {
                        _instance = new Ioc();
                    }
                }
            }
            return _instance;
        }
    }

    public static T Resolve<T>()
    {
        return Instance._container.Resolve<T>();
    }
}

在你的共享点课程中

public class SharepointClass : SharepointWebpart //instantiated by sharepoint
{
    public IService Service { get { return Ioc.Resolve<IService>(); } }

    public void Operation()
    {
        Console.WriteLine("this is " + GetType().Name);
        Service.DoWork();
    }
}

You can do like below. But this is not awesome because, SharepointClass is instantiated by sharepoint not by the dependency injection container. So for now, in SharepointClass you can resolve your dependency like Ioc.Resolve() and deeper dependencies of IService instance will be injected by Windsor.

public interface IMyCode
{
    void Work();
}

public class MyCode : IMyCode
{
    public void Work()
    {
        Console.WriteLine("working");
    }
}

public interface IService
{
    void DoWork();
}

public class MyService : IService
{
    private readonly IMyCode _myCode;

    public MyService(IMyCode myCode)
    {
        _myCode = myCode;
    }

    public void DoWork()
    {
        Console.WriteLine(GetType().Name + " doing work.");
        _myCode.Work();
    }
}

public class Ioc
{
    private static readonly object Syncroot = new object();
    private readonly IWindsorContainer _container;
    private static Ioc _instance;

    private Ioc()
    {
        _container = new WindsorContainer();
        //register your dependencies here
        _container.Register(Component.For<IMyCode>().ImplementedBy<MyCode>());
        _container.Register(Component.For<IService>().ImplementedBy<MyService>());
    }

    public static Ioc Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (Syncroot)
                {
                    if (_instance == null)
                    {
                        _instance = new Ioc();
                    }
                }
            }
            return _instance;
        }
    }

    public static T Resolve<T>()
    {
        return Instance._container.Resolve<T>();
    }
}

And in your sharepoint class

public class SharepointClass : SharepointWebpart //instantiated by sharepoint
{
    public IService Service { get { return Ioc.Resolve<IService>(); } }

    public void Operation()
    {
        Console.WriteLine("this is " + GetType().Name);
        Service.DoWork();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文