views:

147

answers:

1

I´m trying to use an external XML file to map the output from a stored procedure into an instance of a class. The problem is that my class is of a generic type:

public class MyValue<T>
{
  public T Value
  {
    get;
    set;
  }
}

Searching through a lot of blogs an articles I've managed to get this:

<?xml version="1.0" encoding="utf-8" ?>
<Database Name="" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007"&gt;
    <Table Name="MyValue" Member="MyNamespace.MyValue`1" >
     <Type Name="MyNamespace.MyValue`1">
      <Column Name="Category" Member="Value" DbType="VarChar(100)" />
     </Type>
    </Table>
    <Function Method="GetResourceCategories" Name="myprefix_GetResourceCategories" >
     <ElementType Name="MyNamespace.MyValue`1"/>
    </Function>
</Database>

The MyNamespace.MyValue`1 trick works fine, and the class is recognized. I expect four rows from the stored procedure, and I'm getting four MyValue<string> instances, but the big problem is that the property Value for the all four instances is null. The property is not getting mapped and I don't really get why. Maybe worth noting that the property Value is generic, and that when the mapping is done using attributes it works perfect.

Anyone have a clue?

BTW the method GetResourceCategories:

public ISingleResult<MyValue<string>> GetResourceCategories()
{
  IExecuteResult result = this.ExecuteMethodCall(
    this,
    (MethodInfo)MethodInfo.GetCurrentMethod());

  return (ISingleResult<MyValue<string>>)result.ReturnValue;
}
A: 

I am curious as to the reason you have decided to use this mapping approach. In most scenarios I have seen with Linq2SQL and using a repository type pattern you would have a generic type of Entity returned, and you would use partial classes to extend the behavior of the Linq2Sql classes.

To make this work effectively you could use the T4 Linq2Sql template that Damien Guard created here to force the interface implementation on your Linq2Sql classes.

If you are forced into this implementation approach then we would need to see the code that performs the mapping.

Michael Mann
Hi Michael, thanks for your answer.Let's say I'm just experimenting with the concept of xml mapping in Linq to SQL. I assume that what I'm trying to do is possible because it can be achived through attribute mapping, hence there must be a way to do it through xml mapping.The code that performs the mapping is the xml that I posted, since I'm creating my context passing the xml as mapping source. Anyway if the code that creates the context and call the GetResourceCategories method (a method of the context, I forgot to mention) helps to figure out the problem I'll be glad to post it.
Manuel Navarro