Generate Business Entity from Xml

Topics: General Discussion Forum, Service Factory Modeling Edition Forum
Feb 15, 2007 at 4:24 PM
Hello daveloper!!

I make excuses myself for my English but I do not speak English well.
I'm italian and i'm a student! I'm study the Service Factory for the university and I address you because I have a problem to resolve.

I want to create a recipe in order to generate business entity from an XML schema.
It's possible?

I have seen the "CreateBEFromDB" recipe and I'm look at the "GenerateBusinessEntities" action.
I have understood that the classes C# code comes generated gives the "BusinessEntity.t4" template, but I don't understand as the entities come associated to the object "Entities".
How I fill up the object “Entities” with the contained entities in the XML file?
It's possible?

Moreover, I would want to know if there is a documentation that explains where it comes created and filled the object "Entities" with the entities.

Please you answer to me.

Tanks,
Riccardo.
Developer
Feb 15, 2007 at 6:39 PM
As AlexanderQX said, you may check this video from Don (http://blogs.msdn.com/donsmith/archive/2006/04/20/579735.aspx), and check his post about WSDL and WCF here: http://blogs.msdn.com/donsmith/archive/2006/10/06/Handcrafting-WCF2D00friendly-WSDLs.aspx that may help you when creating the WSDL for your business entities.

Charly.
Feb 16, 2007 at 7:51 AM

charlyfriend thanks for your answer.
I will go to see the link that you have said to me, I hope to find what I try them.
Tanks,
Riccardo.
Coordinator
Feb 16, 2007 at 7:55 AM
Riccardo, to answer your first question, yes, it is possible. I even have another approach you might try. Instead of starting from the CreateBEFromDB recipe, start with the recipe that creates data types from XSD. This approach won't use the t4 template engine (which is unfortunate) but it does use the XML serialization APIs. What you get back is code DOM. I think it would be relatively easy to walk the DOM and just remove the XML serialization attributes. You would effectively be left with a business entity - or at least the start of one.

If you still want to use the t4 approach, let us know, because I don't know the answer to those questions off the top of my head. I can answer your last question. No, there are no docs on how the internals of the code work. Sorry.
Feb 16, 2007 at 9:12 AM
Thanks much Don.
I still do not know if the code generated from this recipe is adapted to my scope, however thanks in order to have itself given an other road to follow. Now I will analyze to the code and the operation of this recipe in order to see if it can it are useful to me, or if I can however modify it.


Thanks still Don,
Riccardo.
Feb 20, 2007 at 9:13 AM
Hello Don!

I'm sorry but I do not succeed to understand as I can modify the Software Factories!! I have analyzed the files and I have inasmuch as all recipes, the wizards, and the action they refer to DLL that I cannot modify. Therefore, as I make to modify a part of the ASMX Package? In particular my problem, as I have said to you, is to create Business Entities coded in C# leaving from an XML file! How I can make this?

I make excuses myself for the disturbance, aspect one your answer.
Tanks,
Riccardo.
Developer
Feb 21, 2007 at 2:56 PM
Edited Feb 21, 2007 at 3:00 PM
Riccardo,

Following Don's suggestion, you can take for example "GenerateASMXDataContractFromSchema" recipe that will create all the classes that maps to the different types defined in your xsd. If you take a look at the TypeGeneratorFromSchemaAction class, you will notice that all the hard work is done by the SchemaTypeGenerator class (GenerateTypes function) that will receive the xsd file (wrapped by the ProjectItemFileSource class). The "GenerateTypes(IEnumerable<SchemaItem> items)" function is the meat of the real work. You may customize the schema import operation and xml code generation with the CodeGenerationOptions enums in the XmlSchemaImporter class and XmlCodeExporter class. Then you will generate the cs file (same place as your xsd file, notice lines 211,212) from the CodeCompileUnit and CodeNamespace classes with the help of the CodeDomProvider (CSharp in this case).

Now if you want to remove all the XML attributes from your generated classes, you can traverse the Types collection and Clear the CustomAttributes collection from each type. For example you may add the following code just before line 214 (CodeDomProvider provider = new CSharpCodeProvider();)

// remove the custom attributes from all the types in the namespace
foreach (CodeTypeDeclaration codeType in ns.Types)
{
    codeType.CustomAttributes.Clear();
    // we may also clear any member custom attribute
    foreach (CodeTypeMember member in codeType.Members)
    {
        member.CustomAttributes.Clear();
    }
}

After this minor update, you may generate your business entities out of your schema files.
Hope this helps,

Charly

Feb 21, 2007 at 3:35 PM
Hello Charly,
thanks in order to have to me answered.
I will take in consideration that that you have suggested to me and will begin to work on the plan being hoped to succeed to bring the modifications necessary in order to catch up my objective.

Thanks of the aid,
Riccardo.