Note that there are some explanatory texts on larger screens.

plurals
  1. POclass with valueTypes fields and boxing
    primarykey
    data
    text
    <p>I'm experimenting with generics and I'm trying to create structure similar to Dataset class.<br> I have following code</p> <pre><code>public struct Column&lt;T&gt; { T value; T originalValue; public bool HasChanges { get { return !value.Equals(originalValue); } } public void AcceptChanges() { originalValue = value; } } public class Record { Column&lt;int&gt; id; Column&lt;string&gt; name; Column&lt;DateTime?&gt; someDate; Column&lt;int?&gt; someInt; public bool HasChanges { get { return id.HasChanges | name.HasChanges | someDate.HasChanges | someInt.HasChanges; } } public void AcceptChanges() { id.AcceptChanges(); name.AcceptChanges(); someDate.AcceptChanges(); someInt.AcceptChanges(); } } </code></pre> <p>Problem I have is that when I add new column I need to add it also in HasChanges property and AcceptChanges() method. This just asks for some refactoring.<br> So first solution that cames to my mind was something like this:</p> <pre><code>public interface IColumn { bool HasChanges { get; } void AcceptChanges(); } public struct Column&lt;T&gt; : IColumn {...} public class Record { Column&lt;int&gt; id; Column&lt;string&gt; name; Column&lt;DateTime?&gt; someDate; Column&lt;int?&gt; someInt; IColumn[] Columns { get { return new IColumn[] {id, name, someDate, someInt}; }} public bool HasChanges { get { bool has = false; IColumn[] columns = Columns; //clone and boxing for (int i = 0; i &lt; columns.Length; i++) has |= columns[i].HasChanges; return has; } } public void AcceptChanges() { IColumn[] columns = Columns; //clone and boxing for (int i = 0; i &lt; columns.Length; i++) columns[i].AcceptChanges(); //Here we are changing clone } } </code></pre> <p>As you can see from comments we have few problems here with struct cloning. Simple solution to this is to change Column to class, but from my tests it seems that it increases memory usage by ~40% (because of each object metadata) which is not acceptable for me. <br> <br> So my question is: does anyone have any other ideas how to create methods that can work on different structured objects/records? Maybe someone from F# community can suggest how such problems are solved in functional languages and how it impacts performance and memory usage. <br> <br> <strong>Edit:</strong><br> sfg thanks for suggestion about macros.<br> In Visual Studio 2008 there is built-in (but not so known) template engine called T4. Tha whole point is to add '.tt' file to my project and create a template that will search all my classes, recognize somehow the ones that are records (for example by some interface they implement) and produce partial classes with HasChanges and AcceptChanges() that will call only Columns the class contain.<br><br> Some usefull links:<br> <a href="http://www.t4editor.net/" rel="nofollow noreferrer" title="T4 Editor for VS">T4 Editor for VS</a><br> <a href="http://www.olegsych.com/2007/12/text-template-transformation-toolkit/" rel="nofollow noreferrer" title="Blog with links and tutorials about T4">Blog with links and tutorials about T4</a><br> <a href="http://www.olegsych.com/2008/07/t4-template-for-generating-sql-view-from-csharp-enumeration/" rel="nofollow noreferrer" title="Blog entry with example that uses EnvDTE to read project files">Blog entry with example that uses EnvDTE to read project files</a></p>
    singulars
    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.
 

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