patterncsharpMinor
First time using entity framework with simple repository
Viewed 0 times
simplewithtimefirstusingrepositoryframeworkentity
Problem
I'm working on updating a GitHub project that I use at work, but I think the person abandoned the project as there is a pull request for it since 2014. At work we use a Repository pattern with NHibernate and mappings and so forth but this project they use Entity 4.? I had to update the project to use .Net 4.6.1, and because of that I had to update to Entity 5.0. (I know I could have done 6.0 or greater, but I don't want to break the existing code and 6.0 gave me compiler errors which I don't know how to fix yet). The existing DataContext has a DBSet for all 3 tables (which I don't like) so I'm slowly splitting that into individual repositories. Here is what I have thus far. First the POCO, and interface for said repository
I made it like that so I can easily mock out the repository without having to drag Entity with me. Now the implementation. Sorry if it makes your eyes bleed from any blaring code smells.
LogSubscriptionDataContext
SubscriptionRepository
```
public class SubscriptionRepository : ISubscriptionRepository
{
private readonly DbContext _context;
private readonly IDbSet _logSubscriptions;
public SubscriptionRepository()
{
_context = new Log
public class LogSubscription
{
public int LogSubscriptionId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string PluginTypeName { get; set; }
}
public interface ISubscriptionRepository: IDisposable
{
LogSubscription GetSubscription(string name);
void SaveSubscription(LogSubscription logSubscription);
void DeleteSubscription(LogSubscription logSubscription);
}I made it like that so I can easily mock out the repository without having to drag Entity with me. Now the implementation. Sorry if it makes your eyes bleed from any blaring code smells.
LogSubscriptionDataContext
internal class LogSubscriptionDataContext : DbContext
{
public LogSubscriptionDataContext()
:base("SourceLog")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().ToTable("LogSubscription");
}
}SubscriptionRepository
```
public class SubscriptionRepository : ISubscriptionRepository
{
private readonly DbContext _context;
private readonly IDbSet _logSubscriptions;
public SubscriptionRepository()
{
_context = new Log
Solution
The existing DataContext has a DBSet for all 3 tables (which I don't like) so I'm slowly splitting that into individual repositories.
This is a really bad idea. A repository should encapsulate the data access and make it easier to use.
Usually tables in a database are related but if you split it into single table per repository you won't be able to use the relationsships. How are you going to join any tables?
A repository represents a storage like a database not a single table. I really discourange from splitting it.
This is a really bad idea. A repository should encapsulate the data access and make it easier to use.
Usually tables in a database are related but if you split it into single table per repository you won't be able to use the relationsships. How are you going to join any tables?
A repository represents a storage like a database not a single table. I really discourange from splitting it.
Context
StackExchange Code Review Q#124626, answer score: 3
Revisions (0)
No revisions yet.