通过外部资源丰富班级成员 [英] Enriching class members with an outside source
问题描述
我将类放在数据层上,并且最初使用基本信息实例化了.
它公开了一个名为ExternalSourceData的子对象(或称为Enrich的方法),该子对象调用一个外部源(占用时间)并在同一对象内实例化更多数据成员.
澄清:我没有使用继承,因为此对象暴露给外部项目,该项目将其用作外部程序集.此外,我想清楚地区分常规"数据成员和外部数据源"成员.
因此,这就是我想出的:
Hi,
I have class placed on the data layer, and it is initially instantiated with basic information.
It exposes a sub object called ExternalSourceData (or a method called Enrich), that calls an external source (consumes time) and instantiates some more data members inside the same object.
Clarification: I haven''t used inheritance since this object is exposed to an external project, that uses it as an external assembly. Moreover, I want to clearly differ between the "regular" data members and the "external data source" members.
Thus, that''s what I came up with:
List list = new List();
ExposedClass a = new ExposedClass(list); // a has members such as size, date
int size = a.size;
string name = a.name;
.
.
if (LoadFromExternal)
a.ExternalSourcedData = new ExternalSourceData(externalSourceSessionParms)
用法将是:
and the usage will be:
if (a.ExternalSourcedData != null){
Guid id = a.ExternalSourcedData.ID;
.
.
.
}
我希望ExternalSourcedData ctor使用传递给的列表(它也传递给列表的元素).不幸的是,据我所知,没有办法在c#上执行此操作(在c ++中,它将被称为"Friend",而在Java中,它可以通过使用内部类来解决).
有谁熟悉该问题的解决方案?也许是一种设计模式,它将使它更易于接受,因为我不想将列表再次传递给ExternalSourceData ctor?
谢谢分配.
I want the ExternalSourcedData ctor to use the list passed to a (it also passes over the elements of the list). unfortunately, from what i gather, there''s no way to do it on c# (on c++ it would be called "Friend" and in java it could have been solved by using an inner class).
Is anyone familiar with a solution to that problem ? Maybe a design pattern that would make it more acceptable as i don''t want to pass the list again to the ExternalSourceData ctor?
Thanks allot.
推荐答案
You just need to add a public method for invoking the event. Microsoft already does this for some events such as PerformClick for controls that expose a Click event.
public class CustomGUIElement
{
public void PerformClick()
{
OnClick(EventArgs.Empty);
}
protected virtual void OnClick(EventArgs e)
{
if (Click != null)
Click(this, e);
}
}
You would then do the following inside your example event handler...
public void CustomForm_Click(object sender, MouseEventArgs e)
{
_elements[0].PerformClick();
}
这篇关于通过外部资源丰富班级成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!