1

I have DataContext classes generated from dbml. Once I get data from the database, I need to transform them into DataContract classes so that the objects can be sent via WCF.

One way to do this is like this:

using (var dc = new TestDBL2SDataContext(Settings.Default.TestDBConnectionString))
        {
            var myEmp = from rec in dc.Employees
                    select new MyDataContracts.Employee 
                             { 
                               FirstName = rec.Name.Substring(0,10) 
                             };
            return myEmp.FirstOrDefault();;
        }

Is there a better way to do this via an XSD/XSLT file that I can define in my project and simply point to ?

3 Answers 3

2

Open the dbml file, select the designer and in the properties window set the Serialization Mode to Unidirectional, that's all you need to send the Employee records returned from the datacontext via WCF.

I hope that what you are looking for.

Sign up to request clarification or add additional context in comments.

1 Comment

Nope. The datacontract class may be different from the datacontext class - properties may be transformed, flattened out etc. I wanted to do this transformaton via an xml file rather than be code - I am assuming it is cleaner rather than writing my own or using some 3rd party code.
1

We've written Translator<FromDataModelType, ToDataContractType> classes for this, and are using AutoMapper as a quick-and-painless way to accomplish the mapping between properties.

This assumes that you need to apply transformations to the DataContext classes, as you're doing by assigning a substring of Name to FirstName.

4 Comments

Yes, I need to transform Name to Firstname via a substring (as an example). I see that the AutoMapper will surely help. But was wondering if I could use an XML file that defines the mappings and simply call it. Is that possible ?
@DeeStackOverflow not in Automapper, it isn't. From the lead dev: "No. XML is the devil." :) Another user in that thread suggests otis-lib as an alternative, but I've never tried it.
Hmm... you seem to indicate that using xml is not well advised by the community. Thanks for the tip.
My pleasure, though I didn't mean to imply that anyone other than the author of Automapper recommends avoiding XML for mapping. :)
0

In the dbml designer, set the serialization mode to unidirectional. Job done.

If you need a slightly different DTO layer, maybe AutoMapper is a good option.

1 Comment

I need to apply some transforms of properties. It has to be a new DataContract class that would probably flatten out data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.