Hello,
I've learned to create multi-steps application from the Steve Sanderson's book (Pro ASP.Net MVC/APress p.477). Since then, I've adapted that Technique to many scenarios. Basically, it's about serialization/deserialization to keep data live between requests.
But the only problem is I cannot use model generated by Linq2Sql because MVC will complain that those classes are not marked [serializable].
So I do explicit conversion. I create the same class to match the one created by Linq2Sql: So, I get twice the same content with slightly different names. For instance, Client and ClientData, Product and ProductData, Registration and RegistrationData, etc. Each pair contains exactly the same properties.
Example: To process a client instance,
- I pull it from the DB using Linq2Sql relying on its id
- I convert the Client instance pulled from the DB into a ClientData instance using a static method, which simply assigns values from one instance's properties to the other's.
- After processing I do the reverse: I convert ClientData instance into Client and persist it in the DB using Linq2Sql.
Then I have to write useless code and start to forget which one does what.Question: Is there any techniques I can use to avoid explicit conversion and still be able to serialize data from instances of classes generated by Linq2Sql.
Below is the type of method I use to translate the instance of one type to that of a different one. I have 2 methods, one to get the data and the other to save to the DB.
public static Client Translate_Client_Into_ClientData(Client client)
{
return new ClientData()
{
ClientID = client.ClientID,
FirstName = client.FirstName,
LastName = client.LastName
//And so and so ...
}
}
It looks like I'm not drying up, rather getting wet... that way.
Thanks for helping