{"id":94,"date":"2008-12-01T08:51:00","date_gmt":"2008-12-01T08:51:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2008\/12\/01\/persistor-of-a-serializer\/"},"modified":"2008-12-01T08:51:00","modified_gmt":"2008-12-01T08:51:00","slug":"persistor-of-a-serializer","status":"publish","type":"post","link":"http:\/\/www.panahy.nl\/index.php\/2008\/12\/01\/persistor-of-a-serializer\/","title":{"rendered":"Persistor of a Serializer"},"content":{"rendered":"<p>We had this situation in my last project where we wanted to have some of our objects to get easily serialized into a specific table. Therefor we had to add up to the Serializer an new generic class that uses the Serializer to get the sream to put into a table.<br \/>In this example we have several classes like Article, Order, etc. and we wanted to give them a specific addition that would implement the Save and Load for all these classes. So we wanted to have something like this :<\/p>\n<p><span style=\"font-family:courier new;\">[Serializable]<br \/>public class Article : Persistor<\/span><\/p>\n<article>\n<p>and then later use it as follows:<\/p>\n<p><span style=\"font-family:courier new;\">\/\/ Read the last saved Article filter<br \/>private Article filter = Article.Load();<\/p>\n<p>&#8230;<br \/>\/\/ and do something with it and finally save it back to Database<\/p>\n<p>this.filter.Remove(e.Item.Key);<br \/>&#8230;<br \/>this.filter.Save();<br \/><\/span><br \/>Well, to achive that we implemented the Persistor class as follows:<\/p>\n<p><\/p>\n<pre><br \/><span style=\"color: #0000FF; \">using<\/span><span style=\"color: #000000; \"> System;<br \/><\/span><span style=\"color: #0000FF; \">using<\/span><span style=\"color: #000000; \"> System.Collections.Generic;<br \/><br \/><\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> <\/span><span style=\"color: #808080; \">&lt;summary&gt;<\/span><span style=\"color: #008000; \"><br \/><\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> Provides the persistance methods for the filter classes.<br \/><\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> It reads and writes the content in an xml format into the Filter table<br \/><\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> <\/span><span style=\"color: #808080; \">&lt;\/summary&gt;<\/span><span style=\"color: #008000; \"><br \/><\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> <\/span><span style=\"color: #808080; \">&lt;typeparam name=\"T\"&gt;<\/span><span style=\"color: #008000; \">The type of the filter. This type should derive from Persistor&amp;lt;T&amp;gt; and have an empty constructor<\/span><span style=\"color: #808080; \">&lt;\/typeparam&gt;<\/span><span style=\"color: #808080; \"><br \/><\/span><span style=\"color: #000000; \">[Serializable]<br \/><\/span><span style=\"color: #0000FF; \">public<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">class<\/span><span style=\"color: #000000; \"> Persistor<\/span><span style=\"color: #000000; \">&lt;<\/span><span style=\"color: #000000; \">T<\/span><span style=\"color: #000000; \">&gt;<\/span><span style=\"color: #000000; \"><br \/>    where T : Persistor<\/span><span style=\"color: #000000; \">&lt;<\/span><span style=\"color: #000000; \">T<\/span><span style=\"color: #000000; \">&gt;<\/span><span style=\"color: #000000; \">, <\/span><span style=\"color: #0000FF; \">new<\/span><span style=\"color: #000000; \">()<br \/>{<br \/>    <\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> <\/span><span style=\"color: #808080; \">&lt;summary&gt;<\/span><span style=\"color: #008000; \"><br \/>    <\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> Saves this filter as the default filter for the current user.<br \/>    <\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> <\/span><span style=\"color: #808080; \">&lt;\/summary&gt;<\/span><span style=\"color: #808080; \"><br \/><\/span><span style=\"color: #000000; \">    <\/span><span style=\"color: #0000FF; \">public<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">void<\/span><span style=\"color: #000000; \"> Save()<br \/>    {<br \/>        <\/span><span style=\"color: #008000; \">\/\/<\/span><span style=\"color: #008000; \"> Serialize the current filter instance<\/span><span style=\"color: #008000; \"><br \/><\/span><span style=\"color: #000000; \">        Serializer<\/span><span style=\"color: #000000; \">&lt;<\/span><span style=\"color: #000000; \">T<\/span><span style=\"color: #000000; \">&gt;<\/span><span style=\"color: #000000; \"> serializer <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">new<\/span><span style=\"color: #000000; \"> Serializer<\/span><span style=\"color: #000000; \">&lt;<\/span><span style=\"color: #000000; \">T<\/span><span style=\"color: #000000; \">&gt;<\/span><span style=\"color: #000000; \">();<br \/>        <\/span><span style=\"color: #0000FF; \">string<\/span><span style=\"color: #000000; \"> xml <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> serializer.Serialize(<\/span><span style=\"color: #0000FF; \">this<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">as<\/span><span style=\"color: #000000; \"> T);<br \/><br \/>        <\/span><span style=\"color: #008000; \">\/\/<\/span><span style=\"color: #008000; \"> The filter class is a data class that handles the saveing of a record<\/span><span style=\"color: #008000; \"><br \/><\/span><span style=\"color: #000000; \">        Data.Tables.Filter filter <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> Persistor<\/span><span style=\"color: #000000; \">&lt;<\/span><span style=\"color: #000000; \">T<\/span><span style=\"color: #000000; \">&gt;<\/span><span style=\"color: #000000; \">.LoadFilter(<\/span><span style=\"color: #0000FF; \">true<\/span><span style=\"color: #000000; \">);<br \/><br \/>        <\/span><span style=\"color: #008000; \">\/\/<\/span><span style=\"color: #008000; \"> Actual save to database<\/span><span style=\"color: #008000; \"><br \/><\/span><span style=\"color: #000000; \">        filter.Value <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> xml;<br \/>        filter.Save();<br \/>    }<br \/><br \/>    <\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> <\/span><span style=\"color: #808080; \">&lt;summary&gt;<\/span><span style=\"color: #008000; \"><br \/>    <\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> Loads the default filter for the current table and user.<br \/>    <\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> <\/span><span style=\"color: #808080; \">&lt;\/summary&gt;<\/span><span style=\"color: #808080; \"><br \/><\/span><span style=\"color: #000000; \">    <\/span><span style=\"color: #0000FF; \">public<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">static<\/span><span style=\"color: #000000; \"> T Load()<br \/>    {<br \/>        Tables.Filter filter <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> Persistor<\/span><span style=\"color: #000000; \">&lt;<\/span><span style=\"color: #000000; \">T<\/span><span style=\"color: #000000; \">&gt;<\/span><span style=\"color: #000000; \">.LoadFilter(<\/span><span style=\"color: #0000FF; \">false<\/span><span style=\"color: #000000; \">);<br \/><br \/>        <\/span><span style=\"color: #008000; \">\/\/<\/span><span style=\"color: #008000; \"> Deserialize<\/span><span style=\"color: #008000; \"><br \/><\/span><span style=\"color: #000000; \">        T currentFilter <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">null<\/span><span style=\"color: #000000; \">;<br \/>        <\/span><span style=\"color: #0000FF; \">if<\/span><span style=\"color: #000000; \"> (filter <\/span><span style=\"color: #000000; \">!=<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">null<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #000000; \">&amp;&amp;<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #000000; \">!<\/span><span style=\"color: #0000FF; \">string<\/span><span style=\"color: #000000; \">.IsNullOrEmpty(filter.Value))<br \/>        {<br \/>            Serializer<\/span><span style=\"color: #000000; \">&lt;<\/span><span style=\"color: #000000; \">T<\/span><span style=\"color: #000000; \">&gt;<\/span><span style=\"color: #000000; \"> serializer <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">new<\/span><span style=\"color: #000000; \"> Serializer<\/span><span style=\"color: #000000; \">&lt;<\/span><span style=\"color: #000000; \">T<\/span><span style=\"color: #000000; \">&gt;<\/span><span style=\"color: #000000; \">();<br \/>            currentFilter <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> serializer.Deserialize(filter.Value);<br \/>        }<br \/>        <\/span><span style=\"color: #0000FF; \">else<\/span><span style=\"color: #000000; \"><br \/>        {<br \/>            currentFilter <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">new<\/span><span style=\"color: #000000; \"> T();<br \/>        }<br \/><br \/>        <\/span><span style=\"color: #0000FF; \">return<\/span><span style=\"color: #000000; \"> currentFilter;<br \/>    }<br \/><br \/>    <\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> <\/span><span style=\"color: #808080; \">&lt;summary&gt;<\/span><span style=\"color: #008000; \"><br \/>    <\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> Loads the Filter for the current user and table from the database.<br \/>    <\/span><span style=\"color: #808080; \">\/\/\/<\/span><span style=\"color: #008000; \"> <\/span><span style=\"color: #808080; \">&lt;\/summary&gt;<\/span><span style=\"color: #808080; \"><br \/><\/span><span style=\"color: #000000; \">    <\/span><span style=\"color: #0000FF; \">protected<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">static<\/span><span style=\"color: #000000; \"> Tables.Filter LoadFilter(<\/span><span style=\"color: #0000FF; \">bool<\/span><span style=\"color: #000000; \"> createNew)<br \/>    {<br \/>        <\/span><span style=\"color: #0000FF; \">string<\/span><span style=\"color: #000000; \"> tableId <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">typeof<\/span><span style=\"color: #000000; \">(T).Name;<br \/>        <\/span><span style=\"color: #0000FF; \">int<\/span><span style=\"color: #000000; \"> userId <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> UserContext.Current.User.ID;<br \/><br \/>        <\/span><span style=\"color: #008000; \">\/\/<\/span><span style=\"color: #008000; \"> Load current filter, if available, otherwise create a new filter instance.<\/span><span style=\"color: #008000; \"><br \/><\/span><span style=\"color: #000000; \">        Tables.Filter filter <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> Tables.Filter.GetForUserTable(userId, tableId);<br \/>        <\/span><span style=\"color: #0000FF; \">if<\/span><span style=\"color: #000000; \"> (filter <\/span><span style=\"color: #000000; \">==<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">null<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #000000; \">&amp;&amp;<\/span><span style=\"color: #000000; \"> createNew)<br \/>        {<br \/>            filter <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">new<\/span><span style=\"color: #000000; \"> Data.Tables.Filter();<br \/>            filter.UserID <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> userId;<br \/>            filter.TableID <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> tableId;<br \/>        }<br \/><br \/>        <\/span><span style=\"color: #0000FF; \">return<\/span><span style=\"color: #000000; \"> filter;<br \/>    }<br \/>}<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We had this situation in my last project where we wanted to have some of our objects to get easily serialized into a specific table. Therefor we had to add up to the Serializer an new generic class that uses the Serializer to get the sream to put into a table.In this example we have &hellip; <a href=\"http:\/\/www.panahy.nl\/index.php\/2008\/12\/01\/persistor-of-a-serializer\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Persistor of a Serializer&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/www.panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"We had this situation in my last project where we wanted to have some of our objects to get easily serialized into a specific table. Therefor we had to add up to the Serializer an new generic class that uses the Serializer to get the sream to put into a table.In this example we have&hellip;","_links":{"self":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/94"}],"collection":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":0,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}