<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: Using The Dynamic Keyword To Avoid Difficulties With Generics</title> <atom:link href="http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/feed/" rel="self" type="application/rss+xml" /><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/</link> <description>inquisitive: adjective. given to inquiry, research, or asking questions; eager for knowledge; intellectually curious</description> <lastBuildDate>Tue, 22 May 2012 17:40:00 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>By: Stacy</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-48144</link> <dc:creator>Stacy</dc:creator> <pubDate>Tue, 20 Jul 2010 12:09:26 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-48144</guid> <description>I&#039;ve found it useful to make my constructors private and expose a static factory method that returns an interface, it forces use of the interface and takes care of the stupid ternery casting problem.</description> <content:encoded><![CDATA[<p>I&#8217;ve found it useful to make my constructors private and expose a static factory method that returns an interface, it forces use of the interface and takes care of the stupid ternery casting problem.</p> ]]></content:encoded> </item> <item><title>By: frederikm</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-48025</link> <dc:creator>frederikm</dc:creator> <pubDate>Mon, 19 Jul 2010 18:21:00 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-48025</guid> <description>hmz, looks like the generic tags disappeared..it&#039;s off course iidgenerator &lt;Id&gt; and GetIdGenerator &lt;Tentity &gt;</description> <content:encoded><![CDATA[<p>hmz, looks like the generic tags disappeared..</p><p>it&#8217;s off course iidgenerator &lt;Id&gt; and GetIdGenerator &lt;Tentity &gt;</p> ]]></content:encoded> </item> <item><title>By: frederikm</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-48024</link> <dc:creator>frederikm</dc:creator> <pubDate>Mon, 19 Jul 2010 18:19:19 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-48024</guid> <description>Hmz, i don&#039;t get why you have to use dynamic..
if the method needs to have an idication of the type of id, just pass in your entitypublic abstract class TestEntityBuilder where TEntity : Entity{
public TEntity Build()
{
var entity = CreateEntityWithDefaultProperties();
entity.Id = GetIdGeneratorFor(entity).GenerateId();return entity;}
private IIdGenerator GetIdGeneratorFor (TEntity entity) where TEntity : Entity{
if (type == typeof(int))
{
return new  IntIdGenerator() as  IdGenerator;
}
...</description> <content:encoded><![CDATA[<p>Hmz, i don&#8217;t get why you have to use dynamic..<br
/> if the method needs to have an idication of the type of id, just pass in your entity</p><p>public abstract class TestEntityBuilder where TEntity : Entity</p><p> {<br
/> public TEntity Build()<br
/> {<br
/> var entity = CreateEntityWithDefaultProperties();<br
/> entity.Id = GetIdGeneratorFor(entity).GenerateId();</p><p> return entity;</p><p> }</p><p> private IIdGenerator GetIdGeneratorFor (TEntity entity) where TEntity : Entity</p><p> {<br
/> if (type == typeof(int))<br
/> {<br
/> return new  IntIdGenerator() as  IdGenerator;<br
/> }<br
/> &#8230;</p> ]]></content:encoded> </item> <item><title>By: Julian Birch</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47726</link> <dc:creator>Julian Birch</dc:creator> <pubDate>Sun, 18 Jul 2010 06:47:37 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47726</guid> <description>The cast idea can be made to work, and it&#039;s not that bad.  I mean, it may look ugly, but it&#039;s hardly inefficient: it&#039;s going to do that cast anyway.However, in general terms what you&#039;re doing is just plain nasty.  You could make the get generator method abstract and have subclasses for int and guid, or take the generator as a constructor dependency.  In production code I&#039;d probably favour the latter (NH anyone?) but the convenience of the former is likely to be a win for testing code.And I&#039;ve no particular objection to using dynamic, it just feels like overkill.</description> <content:encoded><![CDATA[<p>The cast idea can be made to work, and it&#8217;s not that bad.  I mean, it may look ugly, but it&#8217;s hardly inefficient: it&#8217;s going to do that cast anyway.</p><p>However, in general terms what you&#8217;re doing is just plain nasty.  You could make the get generator method abstract and have subclasses for int and guid, or take the generator as a constructor dependency.  In production code I&#8217;d probably favour the latter (NH anyone?) but the convenience of the former is likely to be a win for testing code.</p><p>And I&#8217;ve no particular objection to using dynamic, it just feels like overkill.</p> ]]></content:encoded> </item> <item><title>By: Davy Brion</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47524</link> <dc:creator>Davy Brion</dc:creator> <pubDate>Sat, 17 Jul 2010 00:53:23 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47524</guid> <description>@Phoi wouldn&#039;t say that it&#039;s wrong... i think most of us can agree by now (given the success of weakly typed languages over the past couple of years) that weak typing can&#039;t be as bad as we were always told it wasand yeah, it is possible (apparently) to return the specific type in this situation... but even then it just feels so cumbersome to do so. You&#039;re basically paying lip service to the compiler and that is truly the only reason why you&#039;d use that cast.</description> <content:encoded><![CDATA[<p>@Pho</p><p>i wouldn&#8217;t say that it&#8217;s wrong&#8230; i think most of us can agree by now (given the success of weakly typed languages over the past couple of years) that weak typing can&#8217;t be as bad as we were always told it was</p><p>and yeah, it is possible (apparently) to return the specific type in this situation&#8230; but even then it just feels so cumbersome to do so. You&#8217;re basically paying lip service to the compiler and that is truly the only reason why you&#8217;d use that cast.</p> ]]></content:encoded> </item> <item><title>By: Mark Knell</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47493</link> <dc:creator>Mark Knell</dc:creator> <pubDate>Fri, 16 Jul 2010 20:41:15 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47493</guid> <description>It&#039;s overkill for this particular problem, but I&#039;ve sometimes found it useful to declare explicit types for primary keys.  E.g.,// marker interface
public interface IKey {}public struct IntKey : IKey {...}public struct GuidKey : IKey {...}Now you can be generic on IKey.If you want to be relaxed about it, you can provide implicit conversion operators to go to/from the underlying types.  But if you keep things strict, you can do stuff like make your keys immutable.Not simple to introduce to an existing app, though.</description> <content:encoded><![CDATA[<p>It&#8217;s overkill for this particular problem, but I&#8217;ve sometimes found it useful to declare explicit types for primary keys.  E.g.,</p><p>// marker interface<br
/> public interface IKey {}</p><p>public struct IntKey : IKey {&#8230;}</p><p>public struct GuidKey : IKey {&#8230;}</p><p>Now you can be generic on IKey.</p><p>If you want to be relaxed about it, you can provide implicit conversion operators to go to/from the underlying types.  But if you keep things strict, you can do stuff like make your keys immutable.</p><p>Not simple to introduce to an existing app, though.</p> ]]></content:encoded> </item> <item><title>By: pho</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47492</link> <dc:creator>pho</dc:creator> <pubDate>Fri, 16 Jul 2010 20:30:13 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47492</guid> <description>Since it&#039;s a private method so its scope is pretty limited i guess it&#039;s debatable.
But to be consequent, returning a dynamic when it&#039;s definitely possible to return a specific type (or relevant abstraction thereof) feels wrong</description> <content:encoded><![CDATA[<p>Since it&#8217;s a private method so its scope is pretty limited i guess it&#8217;s debatable.<br
/> But to be consequent, returning a dynamic when it&#8217;s definitely possible to return a specific type (or relevant abstraction thereof) feels wrong</p> ]]></content:encoded> </item> <item><title>By: Davy Brion</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47481</link> <dc:creator>Davy Brion</dc:creator> <pubDate>Fri, 16 Jul 2010 19:00:50 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47481</guid> <description>I just think it&#039;s kinda stupid to repeat the same cast in both branches even though you already know it&#039;s of the correct type</description> <content:encoded><![CDATA[<p>I just think it&#8217;s kinda stupid to repeat the same cast in both branches even though you already know it&#8217;s of the correct type</p> ]]></content:encoded> </item> <item><title>By: pho</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47478</link> <dc:creator>pho</dc:creator> <pubDate>Fri, 16 Jul 2010 18:26:01 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47478</guid> <description>Tommy&#039;s suggestion is what came to mind here too; was getting ready to reply until i figured out the solution was already here ;-)Why do you prefer the dynamic version though? I would even say it&#039;s utterly useless here</description> <content:encoded><![CDATA[<p>Tommy&#8217;s suggestion is what came to mind here too; was getting ready to reply until i figured out the solution was already here <img
src='http://d18sni7re4ly7f.cloudfront.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /></p><p>Why do you prefer the dynamic version though? I would even say it&#8217;s utterly useless here</p> ]]></content:encoded> </item> <item><title>By: Davy Brion</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47453</link> <dc:creator>Davy Brion</dc:creator> <pubDate>Fri, 16 Jul 2010 15:04:48 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47453</guid> <description>@Tommyhuh... i could&#039;ve sworn we&#039;ve tried that, but you&#039;re right... that works :)though i still prefer the dynamic version :p</description> <content:encoded><![CDATA[<p>@Tommy</p><p>huh&#8230; i could&#8217;ve sworn we&#8217;ve tried that, but you&#8217;re right&#8230; that works <img
src='http://d18sni7re4ly7f.cloudfront.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p>though i still prefer the dynamic version :p</p> ]]></content:encoded> </item> <item><title>By: Tommy Carlier</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47452</link> <dc:creator>Tommy Carlier</dc:creator> <pubDate>Fri, 16 Jul 2010 14:59:35 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47452</guid> <description>@Davy, the &lt; and &gt; must not have come through; I meant this:
&lt;code&gt;private static IIdGenerator&lt;TId&gt; GetIdGenerator()
{
return typeof(TId) == typeof(int)
? new IntIdGenerator() as IIdGenerator&lt;TId&gt;
: new GuidIdGenerator() as IIdGenerator&lt;TId&gt;;
}&lt;/code&gt;</description> <content:encoded><![CDATA[<p>@Davy, the &lt; and &gt; must not have come through; I meant this:<br
/> <code>private static IIdGenerator&lt;TId&gt; GetIdGenerator()<br
/> {<br
/> return typeof(TId) == typeof(int)<br
/> ? new IntIdGenerator() as IIdGenerator&lt;TId&gt;<br
/> : new GuidIdGenerator() as IIdGenerator&lt;TId&gt;;<br
/> }</code></p> ]]></content:encoded> </item> <item><title>By: Tommy Carlier</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47450</link> <dc:creator>Tommy Carlier</dc:creator> <pubDate>Fri, 16 Jul 2010 14:57:49 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47450</guid> <description>A better design could be to create a static dictionary with IdGenerators (so it&#039;s easier to add types in the future):&lt;code&gt;private static readonly IDictionary&lt;Type, object&gt;
fIdGenerators = InitializeIdGenerators();private static IDictionary&lt;Type, object&gt; InitializeIdGenerators()
{
var generators = new Dictionary&lt;Type, object&gt;();
generators.Add(typeof(int), new IntIdGenerator());
generators.Add(typeof(Guid), new GuidIdGenerator());
return generators;
}private static IIdGenerator&lt;TId&gt; GetIdGenerator()
{
object generator;
return fIdGenerators.TryGetValue(typeof(TId), out generator)
? generator as IIdGenerator&lt;TId&gt;
: null;
}&lt;/code&gt;</description> <content:encoded><![CDATA[<p>A better design could be to create a static dictionary with IdGenerators (so it&#8217;s easier to add types in the future):</p><p><code>private static readonly IDictionary&lt;Type, object&gt;<br
/> fIdGenerators = InitializeIdGenerators();</p><p>private static IDictionary&lt;Type, object&gt; InitializeIdGenerators()<br
/> {<br
/> var generators = new Dictionary&lt;Type, object&gt;();<br
/> generators.Add(typeof(int), new IntIdGenerator());<br
/> generators.Add(typeof(Guid), new GuidIdGenerator());<br
/> return generators;<br
/> }</p><p>private static IIdGenerator&lt;TId&gt; GetIdGenerator()<br
/> {<br
/> object generator;<br
/> return fIdGenerators.TryGetValue(typeof(TId), out generator)<br
/> ? generator as IIdGenerator&lt;TId&gt;<br
/> : null;<br
/> }</code></p> ]]></content:encoded> </item> <item><title>By: Davy Brion</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47449</link> <dc:creator>Davy Brion</dc:creator> <pubDate>Fri, 16 Jul 2010 14:56:45 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47449</guid> <description>@Tommythere is no IIdGenerator type... and AFAIK, you can&#039;t cast it explicitly to a generic instance of IIdGenerator of the correct type (either long or guid)so your code wouldn&#039;t compile either@Adamyeah you could... but this class was meant to be used during testing so maybe IOC is a bit over the top then ;)</description> <content:encoded><![CDATA[<p>@Tommy</p><p>there is no IIdGenerator type&#8230; and AFAIK, you can&#8217;t cast it explicitly to a generic instance of IIdGenerator of the correct type (either long or guid)</p><p>so your code wouldn&#8217;t compile either</p><p>@Adam</p><p>yeah you could&#8230; but this class was meant to be used during testing so maybe IOC is a bit over the top then <img
src='http://d18sni7re4ly7f.cloudfront.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p> ]]></content:encoded> </item> <item><title>By: Adam</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47448</link> <dc:creator>Adam</dc:creator> <pubDate>Fri, 16 Jul 2010 14:52:44 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47448</guid> <description>In this particular case I would consider using IoC for looking up generators. The GetIdGeneratorFor method is like a lookup call.
Otherwise I see how the dynamic keyword can help you avoid problems in similar cases. Neat solution, lot less ugly than writing casts all around, even though it lacks type safety in the same way.</description> <content:encoded><![CDATA[<p>In this particular case I would consider using IoC for looking up generators. The GetIdGeneratorFor method is like a lookup call.<br
/> Otherwise I see how the dynamic keyword can help you avoid problems in similar cases. Neat solution, lot less ugly than writing casts all around, even though it lacks type safety in the same way.</p> ]]></content:encoded> </item> <item><title>By: Tommy Carlier</title><link>http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/comment-page-1/#comment-47447</link> <dc:creator>Tommy Carlier</dc:creator> <pubDate>Fri, 16 Jul 2010 14:52:05 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/07/using-the-dynamic-keyword-to-avoid-difficulties-with-generics/#comment-47447</guid> <description>First of all, the GetIdGenerator-function doesn&#039;t really need the type-parameter: it already knows that (typeof(TId)).Second, the error message gives a tip of how you can solve it: you can cast it explicitely, like this:&lt;code&gt;private static IIdGenerator&lt;TId&gt; GetIdGenerator()
{
return typeof(TId) == typeof(int)
? new IntIdGenerator() as IIdGenerator
: new GuidIdGenerator() as IIdGenerator;
}&lt;/code&gt;</description> <content:encoded><![CDATA[<p>First of all, the GetIdGenerator-function doesn&#8217;t really need the type-parameter: it already knows that (typeof(TId)).</p><p>Second, the error message gives a tip of how you can solve it: you can cast it explicitely, like this:</p><p><code>private static IIdGenerator&lt;TId&gt; GetIdGenerator()<br
/> {<br
/> return typeof(TId) == typeof(int)<br
/> ? new IntIdGenerator() as IIdGenerator<br
/> : new GuidIdGenerator() as IIdGenerator;<br
/> }</code></p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 3/20 queries in 0.013 seconds using disk: basic
Object Caching 567/570 objects using disk: basic
Content Delivery Network via Amazon Web Services: CloudFront: d18sni7re4ly7f.cloudfront.net

Served from: davybrion.com @ 2012-05-23 12:25:20 -->
