Table names and unmapped properties

Sep 29, 2008 at 6:11 PM
First off, thanks for the great work on this! I've done a lot with code generators and I've kinda grown to dislike them. Personal taste, I guess. Anyhoots, Close2Poco is great for

I have a situation where my SQL table name is "d_lookup_data" but I want my class to be named "LookupData". There is already an attribute that can be used for this: System.Data.Linq.Mapping.TableAttribute; Close2Poco.MappingGenerator.GenerateTable() simply needs to look for it, but I'm not sure if Close2Poco should have an attribute for this or if it should reuse the Microsoft provided one.

I also have a situation where I have public properties in my classes that I do not want mapped (they don't exist in the database) - for example, I have a TimeSpan property that I'm storing in the database as a long. I can't find an existing attribute for this (mebbe I'm not looking enough?) so I created an UnmappedAttibute in Close2Poco and handled that in Close2Poco.MappingGenerator.GenerateTable() as well.

Code changes are below - if you think they're worth including in your project, please do so. Again, thanks for the great work on this!


string GenerateTable(Type t)
{
StringBuilder result = new StringBuilder();
object[] attributes;

foreach (PropertyInfo p in t.GetProperties().Where(p => IsPersistent(p)))
{
attributes = p.GetCustomAttributes(typeof(UnmappedAttribute), true);
if (attributes == null || attributes.Length == 0)
{
if (RelationshipFinder.IsEntityList(p))
result.AppendLine(GetListMapping(p));
else if (RelationshipFinder.IsEntityReference(p))
result.AppendLine(GetReferenceMapping(p));
else
result.AppendLine(GetStandardMapping(p));
}
}
string pluralEnding = string.Empty; //TODO: drive this off a "pluralization" setting, to be "s" or String.Empty

string name = null;

attributes = t.GetCustomAttributes(typeof (System.Data.Linq.Mapping.TableAttribute), true);
if (attributes != null && attributes.Length > 0)
name = ((System.Data.Linq.Mapping.TableAttribute)attributes[0]).Name;

if (name == null)
name = t.Name + pluralEnding;

return string.Format(_tableTemplate, name, t.FullName, result);
}




[AttributeUsage(AttributeTargets.Property)]
public class UnmappedAttribute : Attribute
{
}



        
    
Coordinator
Oct 3, 2008 at 12:58 AM
Hi,

Thanks for your interest in the project.  I too have grown to dislike generators.

Yes, controlling the table name is a good idea, and you are correct that the current code base doesn't support it.

As for properties that are not stored, you should be able to mark them with Close2Poco's existing TransientAttribute.

Thanks for sharing your patches here.  I'll integrate the table name one (possibly with a Close2Poco attribute rather than a Linq one) if/when I do the next release.  I say if, because so far interest in Close2Poco has been very light - and I feel like I can only justify spending more time on it if it will be of interest to a larger number of people.  I think the concept is great, but the project doesn't seem to have been found by a very large number of like-minded people :-(    Which makes your comments all the more appreciated - thanks!

Finally, if you are going to use the current code in a production app, please do check the list of known limitations on the project home page.  Right now, I'm not working on fixing them, so if you use this code in production you'll need to be prepared to work with the code yourself if necessary, both for the known limitations and any "unknown" ones that you may find ;-)   One other thing you might like to watch out for relates to performance - with a large enough object model, what will be the performance hit of making the whole XML mapping in one hit?  It might be fine, but I haven't verified that yet.

Finally, if you do decide to make more changes, you're most welcome email me first, so we can sort out a way to let you get them commited into the codebase more easily.

Regards,

John