Note that there are some explanatory texts on larger screens.

plurals
  1. POHierarchical POCO objects & Repositories
    primarykey
    data
    text
    <p>Suppose I have a domain model that has 1:1 correspondence to the physical model. Also:</p> <ul> <li>all the tables in the physical model has the column name 'Id' as a primary key</li> <li>Many of the tables have 'LastChanged' timestamp column</li> <li>Some of the tables contain localized data</li> </ul> <p><strong>Objective</strong>: create domain model classes (POCO) and appropriate repositories. Tools - VS2010, EF4.1</p> <p>The <em>most obvious</em> approach to take is to generate EF model from the DB and then run T4 POCO Generator over it. The output is a set of POCO classes.</p> <pre class="lang-cs prettyprint-override"><code>//POCO classes public class Country { public int Id { get; set; } public string Name { get; set; } public DateTime LastModified { get; set; } ... } public class User { public int Id { get; set; } public string FirtsName { get; set; } public DateTime LastModified { get; set; } ... } </code></pre> <p>So far so good but we come across some code duplications when implementing repositories. Both on the interface definition level:</p> <pre class="lang-cs prettyprint-override"><code>public interface ICountryRepository { IEnumerable&lt;Country&gt; FindAll(); Country FindById(int id); void Add(Country country); void Delete(Country country); } //Here the ONLY difference is the type of the entity public interface IUserRepository { IEnumerable&lt;User&gt; FindAll(); User FindById(int id); void Add(User user); void Delete(User user); } </code></pre> <p>And on the implementation level:</p> <pre class="lang-cs prettyprint-override"><code>class CountryRepository : ICountryRepository { IEnumerable&lt;Country&gt; FindAll() { //implementation } Country FindById(int id) { //find by id logic } void Add(Country country) { //logic } void Delete(Country country) { //logic } } class UserRepository : IUserRepository { IEnumerable&lt;User&gt; FindAll() { //the only difference in the implementation //is the type of returned entity } User FindById(int id) { //the only difference in the implementation //is the type of returned entity } void Add(User user) { //the only difference in the implementation //is the type of returned entity } void Delete(User user) { //the only difference in the implementation //is the type of returned entity } } </code></pre> <p>Taking into account that <strong>most of the code above can be written more generically</strong>, we can also take the following approach.</p> <p>Create a POCO objects hierarchy:</p> <pre class="lang-cs prettyprint-override"><code>public class EntityBase { public int Id { get; set; } } public class TrackableEntity : EntityBase { public DateTime LastChanged { get; set; } } public class LocalizedEntity : TrackableEntity { public int ResourceId { get; set; } } public class Country : LocalizedEntity { } </code></pre> <p>And then repository hierarchy with a base implemetation:</p> <pre class="lang-cs prettyprint-override"><code>public interface IRepository&lt;TEntity&gt; where TEntity : EntityBase { IEnumerable&lt;TEntity&gt; FindAll(); TEntity FindById(int id); void Add(TEntity entity); void Delete(TEntity entity); } public interface ILocalizedRepository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : LocalizedEntity { IEnumerable&lt;TEntity&gt; FindByCultureIso2(string cultureIso2); } public interface ICountryRepository : ILocalizedRepository&lt;Country&gt; { } internal class RepositoryBase&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : EntityBase { private readonly IObjectSet&lt;TEntity&gt; _objectSet; public RepositoryBase(ObjectContext database) { _objectSet = database.CreateObjectSet&lt;TEntity&gt;(); } protected virtual IQueryable&lt;TEntity&gt; All() { return _objectSet; } public virtual IEnumerable&lt;TEntity&gt; FindAll() { return All().ToList(); } public virtual TEntity FindById(int id) { return All().Where(entity =&gt; entity.Id == id).SingleOrDefault(); } public virtual void Add(TEntity entity) { _objectSet.AddObject(entity); } public virtual void Delete(TEntity entity) { _objectSet.DeleteObject(entity); } } internal class LocalizedRepositoryBase&lt;TEntity&gt; : RepositoryBase&lt;TEntity&gt;, ILocalizedRepository&lt;TEntity&gt; where TEntity : LocalizedEntity { public LocalizedRepositoryBase(ObjectContext database) : base(database) { } protected override IQueryable&lt;TEntity&gt; All() { return (base.All() as ObjectSet&lt;TEntity&gt;).Include("Resource.LocalizedResources.Culture"); } public IEnumerable&lt;TEntity&gt; FindByCultureIso2(string cultureIso2) { IEnumerable&lt;TEntity&gt; entities = All().Where(...); return entities.ToList(); } } internal class CountryRepository : LocalizedRepositoryBase&lt;Country&gt;, ICountryRepository { public CountryRepository(ObjectContext database) : base(database) { } } </code></pre> <p>The compelling advantage of the latter approach is that the code is much more structured which allows to avoid code duplication. </p> <p>But this scenario is hardly amenable to T4 code gen which opens up a surface for a large amount of manual work.</p> <p>I would appreciate if you let me know you thinking on the following:</p> <ul> <li>How do you think, is the beauty of the code really worth the trouble of implementing it manually?</li> <li>Is there any other ways to get rid of the code duplication?</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload