Skip to content

Confused about implementation #3

Description

@ortund

Granted I'm no expert on development patterns (I usually stick to what gets the job done) but I'm really trying to focus on doing this properly. I loved the article about why we use EF wrong so I decided to implement this instead of the crappy repo pattern I was stuck in.

I'm not clear on implementing to manage my CRUDs though. So just as a learning exercise, I've basically copied the code to build the EfStoreContext and such (basically I copied everything done in the "Better Entity Framework" blog post with the intent of making changes when I understand it better if I need to.

The following methods are ones that I've actually written in my ASP.NET Web Forms app (the boss doesn't want me using anything else - yes, you can feel sorry for me #sadface).

private void LoadCompany(int id)
{
    var data = new DataService();
    var company = data.Service.Companies.Find(id);
    if (company == null) throw new NullReferenceException(nameof(company));

    Name.Text = company.Name;
}

private void LoadCompanies()
{
    // Get a list of Company entities for a DropDownList
    var data = new DataService();
    var companies = from c in data.Service.Companies where !c.Deleted select { c.Id, c.Name }
    CompaniesList.DataSource = companies.ToList();
    CompaniesList.DataValueField = "Id";
    CompaniesList.DataTextField = "Name";
    CompaniesList.DataBind();
}

private async Task<Company> SaveCompany()
{
    var data = new DataService();
    var company = new Company
    {
        Name = Name.Text
    }
    data.Service.Add(company);
    await data.Service.SaveChangesAsync();
    data.Service.ClearCache();
    return company;
}

private async Task UpdateCompany(int id)
{
    var data = new DataService();
    var company = data.Service.Companies.Find(id);
    if (company == null) throw new NullReferenceException(nameof(company));

    company.Name = Name.Text;

    data.Service.Update(company);
    await data.Service.SaveChangesAsync();
}

private async Task DeleteCompany(int id)
{
    var data = new DataService();
    data.Service.Remove(data.Service.Companies.Single(x => x.Id == id));
    await data.Service.SaveChangesAsync();
}

So what I'm confused about is that this still looks very "repo-like" to me in implementation so I'm 99% sure I'm implementing this incorrectly, I'm not clear on how to implement this correctly from the Program.cs included in this git repo.

Some issues I'm aware of right away:

  1. I'm not making use of the fact that the DataService class is disposable
  2. With the ability to attach entities to the DbContext, I probably don't need to manually map properties.

Could you provide some clarity? Specifically what would these methods looks like in an ideal world based on the use of this code you've provided?

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions