Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

New Post: Automatically adding EntityTypeConfiguration

$
0
0


Here is a usability improvement I would be happy to contribute to EF, and I want to hear from the EF team what they think.
 
Despite Code First having a convention system for configuration of the entities and its attributes, most of the applications I see everyday leverage the pattern of defining derived EntityTypeConfiguration and then adding them to the ConfigurationRegistrar. The main reason for that is that this pattern provides a good way of encapsulating the configuration code for each type and remove the “code smell” resulting in having all the configurations in the OnModelCreating. However, the usage of EntityTypeConfiguration still suffers from requiring too much ceremony, since besides creating the configuration by instantiating a class, there is the need of registering it. This process could be done automatically and that would free the developers of this task, which is repetitive and does not add any value. Having 50 or more lines registering configurations on the OnModelCreating method is a “code smell” that negatively impacts the legibility of our class and, in doing so, hampers its maintenance.
 
I’d like to suggest an implementation that would be like this:
 
 
a. Create a new type e.g. AutoEntityTypeConfiguration<T> that inherits from EntityTypeConfiguration<T> and serves as a marker for opting-in automatic registration.


b. Add a new method or set of methods to ConfigurationRegistrar to trigger the automatic discovery of these configurations from a specific assembly, e.g.:
• AutoAddFrom(Assembly assembly)
• AutoAddFrom(string assemblyName)
• AutoAddForm().ThisAssembly()
 
These kind of pattern is common in other frameworks, e.g. in Castle – with its container.Install(FromAssembly.This()) for discovering installers – or AllTypes.This() for automatic type discovery.
 
What do you think of this? Would it be interesting to push for this feature?
 

Thanks in advance..
Unai


Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>