Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to appropriate encapsulation similar objects into one?
    primarykey
    data
    text
    <p>I'm doing some VBA stuff, but I'll write examples with C# for better understanding</p> <p>I want to encapsulation a <code>MetaData</code> Object for easier metadata access, however, there are two types of metadata table in the application, let's call them <code>MetaA</code> and <code>MetaB</code></p> <p>The ideal <code>MetaData</code> should have an list member contains all data entries. </p> <p>Each data entry contains some attributes. </p> <p>Each attributes contains a <code>Name</code> used to find the corresponding attribute in <code>MetaA</code> or <code>MetaB</code>, a <code>Value</code> returns the value of the named attribute. Maybe a <code>Reference</code> links to the related object in <code>MetaA</code> or <code>MetaB</code> for further usage.</p> <pre><code>public class MetaData { // All entries the metadata table have is in this List public List&lt;DataEntry&gt; Data { get; set; } public bool CreateInstance () { // Make a MetaData instance from MetaA or MetaB } } public class DataEntry { public DataAttr Id { get; set; } public DataAttr Content { get; set; } // many other attributes... } public class DataAttr { // This is used to find the real attributes of the MetaA or MetaB public string Name { get; set; } public string Value // This is the value we interested { get { /* Logic to get from the real object */ } set { /* Logic to set the real object */ } } // I don't know should I make this, the reference is a ref to the // related attribute object in the real MetaA or MetaB object public object Reference { get; set; } } </code></pre> <p>The <code>Reference</code> is the real object in <code>MetaA</code> or <code>MetaB</code> related to the attribute, I make this because my <code>MetaData</code> is a very simplified abstract of <code>MetaA</code>/<code>MetaB</code>, actual attribute may have many many other properties can't access from <code>DataAttr</code>, if I want access something other than the 'Name' and 'Value', I can do it via the <code>Reference</code> without redesigned the <code>DataAttr</code>. </p> <p>What's bad is that the reference is different in <code>MetaA</code> and <code>MetaB</code>, that's why its type is <code>object</code>.</p> <p>With this object, I can using the something like the following code to access the (limited) <code>MetaA</code> or <code>MetaB</code>.</p> <pre><code>MetaData meta = new MetaData(); meta.CeateInstance(application); Console.WriteLine(meta.Id.value); meta.Content.Value = "blah blah"; meta.Content.Reference.Data.A.B.C = "blah blah"; </code></pre> <p>But <code>MetaA</code> and <code>MetaB</code> are rather complicated, I don't know how I can make such <code>MetaData</code> as a abstract of them.</p> <pre><code>// MetaA public class MetaA { public List&lt;DataList&gt; Data { get; set; } // Other members } public class DataList { // All attributes are stored in a 2D-array // Attrs[k, 0] is the k-th attribute's Name // Attrs[k, 1] is the Value public double SomeNumber { get; set; } public string[,] Attrs { get; } // Other members } // MetaB public class DataEntryOfMetaB { public MetaBAttr SomeNumber { get; set; } public MetaBAttr SomeName { get; set; } public MetaBAttr SomeSize { get; set; } // Other attributes } public class MetaBAttr { public string Name { get; set; } public string Value { get; set; } // Other members } </code></pre> <p>There is even no such <code>MetaB</code> thing, what I can access is lots of separated <code>DataEntryOfMetaB</code>. But find them is not hard, I can just using a <code>List&lt;DataEntryOfMetaB&gt;</code> as a (pseudo) <code>MetaB</code>.</p> <p>As you see, the <code>Attrs</code> of <code>MetaA.Data</code> is readonly. To set the value, I must using the following code:</p> <pre><code>DataAttrMgr mgr = new DataAttrMgr() MetaA metaA = application.GetMetaASomehow(); string[,] dataToSet = new string[,]{ {"attr1", "value of attr1"}, {"attr2", "value of attr2"} }; mgr.SetData(metaA.Data, dataToSet); </code></pre> <p>Yes, there is also an exception: SomeNumber. </p> <p>In <code>MetaB</code> it just like other attributes, but in <code>MetaA</code> it was separated from the <code>Attrs</code> array, stored directly in the <code>DataList</code>.</p> <p>Can anybody help me to create an appropriate abstract of <code>MetaA</code> and <code>MetaB</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload