For an example of what I am talking about, check out the Biggy project, where I recently had to do this very thing. How you do this and where would depend heavily on what you are doing, and the larger structure of your application.
Then, anywhere in your code where the mapping is needed, you can access the primary key name for a specific object by use of the property as a key. Except when they are.įor example, if the above code were used in a larger application context repeatedly, it might be better to walk through the object properties at object initialization (or even at application load) and map the object properties for each to its respective column name and stash them all in a Dictionary. Overall, machines these days are fast, and generally, the odd call to GetType() and GetCustomAttributes() are not all that significant. Calls to using reflection can be expensive. Cache Results from Calls Using Reflection Where AppropriateĪ quick note, which is only marginally applicable to the examples above, but important in the design of a real-world application. Also, you WILL run into databases “in the wild” where column naming conventions do not align with C# Class and property naming conventions (Work with a Postgresql database for five minutes, and get back to me). Believe me, you may not always have EF at your disposal.
“But John,” you say, “Entity Framework already does this!” Var mapsto = columnMapping as DbColumnAttribute Ĭonsole.WriteLine(msg, property.Name, mapsto.Name) FirstOrDefault(a => a.GetType() = typeof(DbColumnAttribute)) For example, if we wanted to implement our own Attribute to indicate that a particular property on a class in our application represents the Primary Key in our database, we might create the following Custom Attribute: public class PrimaryKeyAttribute : Attribute database column" To create a Custom Attribute in C#, we simply create a class which inherits from System.Attribute. Let’s see how we might implement our own version of these data annotations as Custom Attributes. ComponentModel.DataAnnotations namespace provide a ready-made means to do this, but for one, you may find yourself building your own data access layer or tool, and for another, this is an easy-to-understand example case. Use Custom Attributes to Give Hints or Property Metadata Additional Resources and Items of Interest.Custom Attributes Can Be an Architectural Trade-Off.Cache Results from Calls to Reflection Where Appropriate.Use Custom Attributes to Map Properties to Database Columns.Use Reflection to Examine the Properties and Attributes of an Object.Here, we’re going to take a quick look at creating our own custom attributes. In EF, we often utilize to decorate the properties of our data objects. You have no doubt seen this before when using Entity Framework. Other times, though, custom attributes can be just the tool for the job, and sometimes, the only practical way to solve a problem.Īn easy to understand use case for Custom Attributes might be the mapping of object properties to database fields in a data access layer. Most of the time, I can’t help but feel like there must be a better way to accomplish what I am trying to do, and/or that I have somewhere sprung a leak in what should be a helpful abstraction. I have a general distaste for decorating my code with Attributes and Annotations. Image by Elizabeth Briel | Some Rights Reserved