Click here to Skip to main content
15,888,025 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
AnswerRe: Memento pattern and releasing resources Pin
Nathan Minier10-Jul-18 2:45
professionalNathan Minier10-Jul-18 2:45 
QuestionPython: how doI "select / switch in" a module/class to use at runtime Pin
ninjaef3-Jul-18 0:09
ninjaef3-Jul-18 0:09 
AnswerRe: Python: how doI "select / switch in" a module/class to use at runtime Pin
Richard MacCutchan3-Jul-18 2:13
mveRichard MacCutchan3-Jul-18 2:13 
GeneralRe: Python: how doI "select / switch in" a module/class to use at runtime Pin
ninjaef3-Jul-18 3:24
ninjaef3-Jul-18 3:24 
GeneralRe: Python: how doI "select / switch in" a module/class to use at runtime Pin
Richard MacCutchan3-Jul-18 4:26
mveRichard MacCutchan3-Jul-18 4:26 
GeneralRe: Python: how doI "select / switch in" a module/class to use at runtime Pin
ninjaef3-Jul-18 7:14
ninjaef3-Jul-18 7:14 
GeneralRe: Python: how doI "select / switch in" a module/class to use at runtime Pin
Richard MacCutchan3-Jul-18 8:25
mveRichard MacCutchan3-Jul-18 8:25 
QuestionMy Entity Framework Design Pin
Kevin Marois2-Jul-18 7:14
professionalKevin Marois2-Jul-18 7:14 
I'm starting a new project. I've always used Linq-to-SQL. This time I'm going to use EF and the Repository pattern. Also, I'm really new to Web API's, so if I made a mistake, please let me know.

So I spent the last few days researching EF and the Repository Pattern/Unit of Work. I understand the Unit of Work concept and that EF is by design a Unit of Work. Having said that, here is what I've come up with. I'd like your opinion.

Repository Interface
public interface IRepository<TEntity> where TEntity : class
{
    TEntity Get(int id);
    IEnumerable<TEntity> GetAll();
    IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);

    void Add(TEntity entity);
    void AddRange(IEnumerable<TEntity> entities);

    void Remove(TEntity entity);
    void RemoveRange(IEnumerable<TEntity> entities);
}
Repository Base Class
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
    protected DbContext Context { get; private set; }

    public Repository(DbContext context)
    {
        Context = context;
    }

    public TEntity Get(int id)
    {
        return Context.Set<TEntity>().Find(id);
    }

    public void Add(TEntity entity)
    {
        Context.Set<TEntity>().Add(entity);
    }

    // Other IRepository methods left out for brevity
}
Projects Interface and Repository
This class allows me to add specialized project-related data functions.
public interface IProjectsRepository : IRepository<Project>
{
    IEnumerable<Project> FindProjectsByName(string name);
}

public class ProjectsRepository : Repository<Project>, IProjectsRepository
{
    FalconContext _context;

    public ProjectsRepository(FalconContext context)
        : base(context)
    {
        _context = context;
    }

    public IEnumerable<Project> FindProjectsByName(string name)
    {
        Expression<Func<Project, bool>> nameFilter = e => e.ProjectName.Contains(name);
        return Find(nameFilter);
    }
}
Biz Layer
This class is the only class that knows about the data later. The controllers all call into here. That allows me to apply business logic in true Units of Work:
public class BizObject : IBizObject
{
    private IProjectsRepository projectsRepository;

    public void AddProject(Project entity)
    {
        using (var dc = new FalconContext())
        {
            var projectRepo = new ProjectsRepository(dc);
            var projectTaskRepo = new ProjectsTaskRepository(dc);

            var test = projectRepo.FindProjectsByName(entity.ProjectName);

            if (test != null)
            {
                projectRepo.Add(entity);

                foreach (var task in entity.Tasks)
                {
                    projectTaskRepo.Add(task);
                }

                dc.SaveChanges();
            }
            else
            {
                throw new DuplicateProjectException(entity.ProjectName);
            }
        }
    }
}
Controller
public class ProjectsController : ApiController
{
    private IBizObject _bo;

    public IHttpActionResult AddProject(Project entity)
    {
        _bo = new BizObject();
        _bo.AddProject(entity);

        return Ok();
    }
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

AnswerRe: My Entity Framework Design Pin
Richard Deeming2-Jul-18 8:17
mveRichard Deeming2-Jul-18 8:17 
GeneralRe: My Entity Framework Design Pin
Kevin Marois2-Jul-18 8:30
professionalKevin Marois2-Jul-18 8:30 
GeneralRe: My Entity Framework Design Pin
Richard Deeming2-Jul-18 9:00
mveRichard Deeming2-Jul-18 9:00 
GeneralRe: My Entity Framework Design Pin
Kevin Marois2-Jul-18 10:15
professionalKevin Marois2-Jul-18 10:15 
GeneralRe: My Entity Framework Design Pin
Kevin Marois4-Jul-18 17:33
professionalKevin Marois4-Jul-18 17:33 
GeneralRe: My Entity Framework Design Pin
Richard Deeming5-Jul-18 1:27
mveRichard Deeming5-Jul-18 1:27 
GeneralRe: My Entity Framework Design Pin
Kevin Marois9-Jul-18 6:17
professionalKevin Marois9-Jul-18 6:17 
GeneralRe: My Entity Framework Design Pin
Kevin Marois9-Jul-18 6:19
professionalKevin Marois9-Jul-18 6:19 
GeneralRe: My Entity Framework Design Pin
Richard Deeming9-Jul-18 8:20
mveRichard Deeming9-Jul-18 8:20 
GeneralRe: My Entity Framework Design Pin
Kevin Marois16-Jul-18 6:28
professionalKevin Marois16-Jul-18 6:28 
GeneralRe: My Entity Framework Design Pin
Richard Deeming16-Jul-18 8:16
mveRichard Deeming16-Jul-18 8:16 
QuestionEntity Framework Question Pin
Kevin Marois29-Jun-18 9:31
professionalKevin Marois29-Jun-18 9:31 
AnswerRe: Entity Framework Question Pin
Richard Deeming29-Jun-18 10:02
mveRichard Deeming29-Jun-18 10:02 
AnswerRe: Entity Framework Question Pin
Gerry Schmitz29-Jun-18 11:01
mveGerry Schmitz29-Jun-18 11:01 
GeneralRe: Entity Framework Question Pin
Kevin Marois29-Jun-18 11:26
professionalKevin Marois29-Jun-18 11:26 
GeneralRe: Entity Framework Question Pin
Gerry Schmitz29-Jun-18 11:59
mveGerry Schmitz29-Jun-18 11:59 
AnswerRe: Entity Framework Question Pin
jschell30-Jun-18 5:35
jschell30-Jun-18 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.