What I finished is a combination of a repository template with Pablo's answer. The bottom line is that my EF models are versioned, I use EF Code-First Migrations to transfer the database to new versions of models, mine DbContextalways works with the latest version of models, I have developed a number of specific repositories, each of which implements the interface IRepository<TItem>below.
public interface IRepository<TItem> : IQueryable<TItem>, ICollection<TItem>, IDisposable
where TItem : class
{
void Update(TItem item);
void SaveChanges();
}
One implementation IRepository<TItem>is one DbRepository<TItem>that wraps the entity frame code used to communicate with the database.
public class DbRepository<TItem> : IRepository<TItem>
where TItem : class
{
private MyDbContext _db;
public DbRepository()
{
_db = new MyDbContext();
}
}
Another implementation IRepository<TItem>is that TypeConversionRepository<TExternal,TInternal>, which is an abstract class that facilitates the conversion from one type of model to another.
public abstract class TypeConversionRepository<TExternal, TInternal> : IRepository<TExternal>
where TExternal : class
where TInternal : class
{
protected IRepository<TInternal> InternalRepository { get; set; }
protected abstract TInternal ConvertInbound(TExternal externalItem);
protected abstract TExternal ConvertOutbound(TInternal internalItem);
}
, , ConvertInbound() ConvertOutbound() TExternal TInternal . , 2 MyModel, 2 MyModelRepository; 2 , 1 2 1.
namespace Models.v1
{
public class MyModel
{
public int Id { get; set; }
public string MyProperty { get; set; }
}
public class MyModelRepository : TypeConversionRepository<Models.v1.MyModel,Models.v2.MyModel>
{
MyModelRepository()
{
this.InternalRepository = new Models.v2.MyModelRepository();
}
protected override TInternal ConvertInbound(TExternal externalItem)
{
return new Models.v2.MyModel
{
Id = externalItem.Id,
MyNewProperty = externalItem.MyProperty
};
}
protected override TExternal ConvertOutbound(TInternal internalItem)
{
return new Models.v1.MyModel
{
Id = internalItem.Id,
MyProperty = internalItem.MyNewProperty
};
}
}
}
namespace Models.v2
{
public class MyModel
{
public int Id { get; set; }
public string MyNewProperty { get; set; }
}
public class MyModelRepository : DbRepository<MyModel>
{
}
}
v1 ApiController v1 MyModelRepository, v2 ApiController v2 MyModelRepository, , v2.