Coder Social home page Coder Social logo

betterentityframework's Introduction

BetterEntityFramework

As a response to my blog post, this is a library demonstrating how projects can more cleanly work with Entity Framework without imposing artificial restrictions that limit the ability of the application to grow and always use the most efficient queries it needs. This is mostly an excercise on learning your tools and getting comfortable with them, only changing what needs changed, and ultimately trusting that teams of highly paid smart people might actually know what they're doing. Explanation can be found here.

betterentityframework's People

Contributors

hoagsie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

betterentityframework's Issues

Confused about implementation

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!

Z.EntityFramework.Plus.EFCore Cast error

I did not make any modifications to your sample but on line 27 I am getting the following cast error from Z.EntityFramework.Plus.EFCore

Line 27: data.Service.Category.UpdateWhere(category => category.Publish == false, category => new Category { Publish = true }).Wait();

"Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.InMemoryQueryContextFactory' to type 'Microsoft.EntityFrameworkCore.Query.Internal.RelationalQueryContextFactory'."}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.