Enter LINQ To XML, Microsoft's XML parsing extension to LINQ. It is not a replacement for the current XML API's, in fact it utilizes them under the hood, but for programmers like you and me, it makes parsing XML easy and fun.
So what is the problem? Take a look at the following LINQ query:
XMappingConfig Mapping2 = new XMappingConfig(
( from A in Mapping.Descendants( "Database" )
select new XMappingDatabaseConfig(
( from B in A.Descendants( "Lookup" )
select new XMappingDatabaseLookupConfig(
( from C in B.Descendants( "Table" )
select new XMappingDatabaseLookupTableConfig
(
C.Element( "Source" ).Value ,
C.Element( "Target" ).Value
) ).ToArray( )
) ).Single( ) ,
( from D in A.Descendants( "Transaction" )
select new XMappingDatabaseTransactionConfig(
( from E in D.Descendants( "Table" )
select new XMappingDatabaseTransactionTableConfig(
E.Element( "Condition" ).Value ,
E.Element( "FlagColumn" ).Value ,
E.Element( "PrimaryColumn" ).Value ,
E.Element( "Source" ).Value ,
E.Element( "Target" ).Value
) ).ToArray( )
) ).Single( )
) ).Single( ) ,
( from F in Mapping.Descendants( "Ldap" )
select new XMappingLdapConfig(
( from G in F.Descendants( "Lookup" )
select new XMappingLdapLookupConfig(
( from H in G.Descendants( "Table" )
select new XMappingLdapLookupTableConfig(
( from I in H.Descendants( "Attributes" ).Descendants( "Attribute" )
select new XMappingLdapAttributeConfig(
false ,
I.Element( "Source" ).Value ,
I.Element( "Target" ).Value ,
XMappingLdapAttributeTransformationConfig.None
) ).ToArray( ) ,
( from J in H.Descendants( "MultiAttribute" )
select new XMappingLdapAttributeConfig(
false ,
J.Element( "Source" ).Value ,
J.Element( "Target" ).Value ,
XMappingLdapAttributeTransformationConfig.None
) ).Single( ) ,
H.Element( "Target" ).Value
) ).ToArray( )
) ).Single( ) ,
( from K in F.Descendants( "Transaction" )
select new XMappingLdapTransactionConfig(
( from L in K.Descendants( "Table" )
select new XMappingLdapTransactionTableConfig(
( from M in L.Descendants( "Attributes" ).Descendants( "Attribute" )
select new XMappingLdapAttributeConfig(
false ,
M.Element( "Source" ).Value ,
M.Element( "Target" ).Value ,
XMappingLdapAttributeTransformationConfig.None
) ).ToArray( ) ,
L.Element( "Target" ).Value
) ).ToArray( )
) ).Single( )
) ).Single( )
);
God in heaven, that is the ugliest code I have ever seen! Yet it gets the job done in about 63 lines of code. There is no comparison with the native APIs and in case you are wondering, performance wise it is extremely fast.
So if it gets the job done what is the problem? If you are a professional coder and you would enjoy looking at such a wonderful code segment, consider getting a new job. Just because LINQ is so powerful, it does not mean we have to abuse it. Break up your queries in smaller segments so that they are easier to follow. Believe me, you will appreciate yourself even more when you go back and review or modify your code a couple of months down the road.
No comments:
Post a Comment
Feel free to write any comments or ideas!