简体   繁体   English

为用户控件之间的松散耦合提供建议

[英]Advise on loose coupling between user controls

I have 5 user controls on the page and each control implements it's own interface that contains properties and events. 我在页面上有5个用户控件,每个控件都实现它自己的包含属性和事件的界面。 In order to enable communication between user controls, I am creating a property of target user control inside other user control. 为了实现用户控件之间的通信,我在其他用户控件中创建了目标用户控件的属性。 Through this property, it's state can be altered and be able to register its events. 通过此属性,可以更改其状态并能够注册其事件。

Below is the pseudo code of results user control. 下面是结果用户控件的伪代码。 It subscribes to OnSearch event of Search user control. 订阅“搜索”用户控件的OnSearch事件。

public interface IResults
{
     //other fields
    ISearch SearchControl { get;}
}

public partial class Results : IResults
{
     //other fields

     public ISearch SearchControl
     {

        get{
        this.Page.Parent.FindControl("UCSearch") as ISearch;}
     }

     protected override void Page_Load(object sender, EventArgs e)
     {
        this.SearchControl.OnSearch += new EventHandler(testMethod);
     }
}
  1. Is it ok to have the reference properties inside user controls to subscribe to the events and manipulating the state. 在用户控件中具有引用属性可以订阅事件并操作状态是否可以。 Does it create any maintenance problem in future. 将来是否会造成任何维护问题。

  2. Does FindControl and type casting degrade the performance of the application. FindControl和类型转换是否会降低应用程序的性能。

1) It's OK to register events from one object from inside another object. 1)可以从另一个对象内部注册一个对象的事件。 That's what they are for. 那就是他们的目的。 :D :d

2) There would be a small amount of overhead involved performing the search each time SearchControl in accessed. 2)每次访问SearchControl ,执行搜索都会涉及少量开销。 I doubt it would be of any significance, depending, of course, how frequently SearchControl is called. 我怀疑这是否有意义,当然取决于SearchControl的调用频率。

However, how you have implemented SearchControl will present some maintenance problems in the future. 但是,您如何实现SearchControl将来会带来一些维护问题。 By violating the Law of Demeter in SearchControl you have tightly coupled your code to a Page class that has a control named "UCSearch" . 通过违反SearchControl 的Demeter定律,您已将代码紧密耦合到具有名为"UCSearch"的控件的Page类。

Instead why don't you include the a set accessor for the SearchControl property, and have the parent provide the value of SearchControl . 相反,为什么不为SearchControl属性包括一个set访问器,并让父级提供SearchControl的值。 Not only would this make the the code easier to maintain, but it is more loosely coupled, and also avoids your performance concerns! 这不仅使代码更易于维护,而且耦合更松散,并且避免了性能问题! As an added bonus it will be easy to create unit test for!!! 作为一个额外的好处,将很容易为创建单元测试!!!

public interface IResults
{
     //other fields
    ISearch SearchControl { get; set; }
}


public partial class Results : IResults
{
     //other fields

     private ISearch searchControl;

     public ISearch SearchControl
     {
        get
        {
            return this.searchControl;
        }
        set
        {
            if (this.SearchControl != null)
            {
                this.SearchControl.OnSearch -= new EventHandler(testMethod);
            }

            this.searchControl = value;

            this.SearchControl.OnSearch += new EventHandler(testMethod);
        }
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM