Data Contract Nullable Enum Member - How To?

Topics: General Discussion Forum, Service Factory Modeling Edition Forum
Jun 24, 2008 at 4:56 PM
Using the modeling edition how can I create a Data Contract where one of its members can be a Nullable Enum Type?
I have a Data Contract called "Phone Number"
It has two Members:
A Member of Type  "PhoneNumberType" called "PhoneType" and
A Member of Type "String" called "Number"

Have a Data Contract Enumeration called "PhoneNumberType" with Values "Business", "Home" and "Mobile"

What I want is the member PhoneType to be nullable. In code would be 
private PhoneNumberType? PhoneType;
However when you generate code you get 
private PhoneNumberType PhoneType;

How can I specifiy that the Member enum is nullable?

In the Property Window for PhoneType in the Type attribute value it does not let me put a ? after the type name PhoneNumberType.

I can create a nullable enum member in straight C# code but can not figure out how to do this through the Data Contract Modeling front end.

Thanks for any help,
Mark
Jun 25, 2008 at 8:57 PM
Approach mentioned in followingt thread should be of help to you..

http://www.codeplex.com/servicefactory/Thread/View.aspx?ThreadId=23616

You can always edit the cs file generated by the service factory...( might not be good approach as files are auto generated...)
Developer
Jun 26, 2008 at 4:51 PM
Hi mmilchuk, as of the current WSSF version (v.3), only PrimitiveType data members can be set as nullable. But you can always extend the factory to add custom functionallities. In this case, the changes required involve modifying the DataContractEnum extender by adding the IsNullable bool property and some changes in the DataContract text template to make use of this new property.

However, in this particular scenario, there are some considerations that must be addressed in order to make minimum changes to the factory. I am in the process of reproducing this extension to identify them. I'll let you know the results and provide a detailed steps list as soon as possible.

Thanks


mmilchuk wrote:
Using the modeling edition how can I create a Data Contract where one of its members can be a Nullable Enum Type?
I have a Data Contract called "Phone Number"
It has two Members:
A Member of Type  "PhoneNumberType" called "PhoneType" and
A Member of Type "String" called "Number"

Have a Data Contract Enumeration called "PhoneNumberType" with Values "Business", "Home" and "Mobile"

What I want is the member PhoneType to be nullable. In code would be 
private PhoneNumberType? PhoneType;
However when you generate code you get 
private PhoneNumberType PhoneType;

How can I specifiy that the Member enum is nullable?

In the Property Window for PhoneType in the Type attribute value it does not let me put a ? after the type name PhoneNumberType.

I can create a nullable enum member in straight C# code but can not figure out how to do this through the Data Contract Modeling front end.

Thanks for any help,
Mark


Developer
Jun 26, 2008 at 9:17 PM
Edited Jun 27, 2008 at 2:06 PM

Hi again mmilchuk, the following steps describe how to extend the WSSF to support nullable enumerations. To make these changes you will need to work over the WSSF source code version; requirements and download can be found in the following link: Web Service Software Factory: Modeling Edition Source Code - February 2008

Nullable Enums - WSSF Extension Steps:

1.       Once you have the source code version up and running, open the solution Software Factory Guidance Package located in the directory chosen during the WSSF source installation.

2.       In the Solution Explorer, go to Extenders --> Microsoft.Practices.ServiceFactory.Extenders.DataContract.Wcf project and open the WCFDataContractEnum.cs file.

3.       Add the new property IsNullable to the WCFDataContractEnum class:

 

private bool isNullable;

 

[Category(DataContractWcfExtensionProvider.ExtensionProviderPropertyCategory,Description("Specifies whether the enumeration supports null values or not"),

XmlElement("isNullable"),

DisplayName("Is Nullable"),

ReadOnly(false),

BrowsableAttribute(true)]

public bool IsNullable

{

get { return isNullable; }

set { isNullable = value; }

}

4.       Open the file DataContract.tt located in the DataContractDSL project-->TextTemplates-->WCF-->CS directory and modify the PopulateMemberVars method  by replacing the else if (member is ModelElementReference) code block to look as follows:

 

    else if (member is ModelElementReference)

    {            

           ModelElementReference dcde = (ModelElementReference)member; 

           string type = dcde.Type;    
           memberType = Utility.GetCSharpTypeDeclaration(dcde.CollectionType, type);

          

           Contract found = CurrentElement.DataContractModel.Contracts.Find(delegate(Contract contract)

                  {

                        return contract.Name == type;

                  });

 

        if (found != null && found is DataContractEnum)

        {

                  WCFDataContractEnum wcfEnum = GetObjectExtender(found);

                 

                  if(wcfEnum != null && wcfEnum.IsNullable)

                  {

                        memberType = string.Format("System.Nullable<{0}>", type);

                  }

        }     

    }                                                           

 

5.       Compile  the 2 modified projects and the Guidance Package:

1.       Microsoft.Practices.ServiceFactory.Extenders.DataContract.Wcf

2.       DataContractDSL

3.       Service Factory Guidance Package

That’s about it,  you can try this custom WSSF version in the Visual Studio Experimental Hive and deploy it as per the Extensibility Hands on Lab documentation.

Thanks

 

 

 

Jun 27, 2008 at 1:14 PM
Thanks for the solution!

Will there be a new version of Modeling Edition someday?
If so is there somewhere we can make requests for added functionality,
such as:
1) Nullable Data Contract Enumeration Members
2) Integration of Enterprise Library for Exception Shielding

Thanks,
Mark
Developer
Jun 27, 2008 at 1:39 PM
We are planning posting these and other features to the upcoming WSSF Community site.
You can also try #2 with this extension published here with some further details here.
Jun 27, 2008 at 3:22 PM
That Service Station Article is incorrect. I have tried that approach and contacted the authors of the article.
They were mistaken about the amount of the WSSF that needed to be rebuilt.

Here was my letter to the authors:

I have read your article Service Station: Web Service Software Factory Modeling Edition and implemented

Customizing the Service Factory: Enterprise Library Exception Shielding.

http://msdn2.microsoft.com/en-us/magazine/cc164250.aspx

 

So I have Rebuilt the Microsoft.Practices.ServiceFactory.Extenders.ServiceContract.Wcf.dll

and modified the file ServiceImplementation.tt as mentioned in the article. When Generating Code for the

Service Contract Model it works great, just as mentioned in your article.

 

Of course to build the new DLL I had to strong name it and generate a new key pair.

Original PublicKeyToken=31bf3856ad364e35 for Original Microsoft.Practices.ServiceFactory.Extenders.ServiceContract.Wcf.

My newly modified and compiled version of Microsoft.Practices.ServiceFactory.Extenders.ServiceContract.Wcf has PublicKeyToken= 47d34bdda48d75a2

Now comes the problem:

When I go to the Host Model and come to the step where I need to Generate Service :Generates the code for the service

I get the following error:

“Could not load file or assembly 'Microsoft.Practices.ServiceFactory.Extenders.ServiceContract.Wcf,

Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.

The system cannot find the file specified.”

 

I tried editing the file ruleset.config which had references to the DLL Microsoft.Practices.ServiceFactory.Extenders.ServiceContract.Wcf

and changed the PublicKeyToken value to the new key value 47d34bdda48d75a2 but that did not correct the problem.

 

Using Lutz Roeder’s .Net Reflector Tool I noticed that Microsoft.Practices.ServiceFactory.Extenders.HostDesigner.Wcf.dll

References Microsoft.Practices.ServiceFactory.Extenders.ServiceContract.Wcf, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

So the HostDesigner.WCF DLL references the original version of the DLL NOT my modified version.



The Authors only tested the Service Contract code generation which works correctly and

never tested the Host Model Generate Service code generation.