Giter Club home page Giter Club logo

xpressionmapper.netcore's Introduction

XpressionMapper.NetCore

<style></style>

What is XpressionMapper?

XpressionMapper.NetCore leverages AutoMapper version 5.2 and .Net Standard Library version 1.6.1 to transform business model expressions into data model expressions.

Expression mapping provides a couple of advantages:

  1. Improved separation of concerns: The Entity Framework (or other ORM) layer has no knowledge of the business model.
  2. Removes the need for projection in the data layer or the need to return an IQueryable object from the data layer.

How it works?

The service layer references business model classes only and has no knowledge of the data model (POCOs) or the EF (Entity Framework) layer

public class PersonService : IPersonService
{
    private IPersonRepository repository;
public ICollection&lt;PersonModel&gt; GetList(Expression&lt;Func&lt;PersonModel, bool&gt;&gt; filter = null, Expression&lt;Func&lt;IQueryable&lt;PersonModel&gt;, IQueryable&lt;PersonModel&gt;&gt;&gt; orderBy = null, ICollection&lt;Expression&lt;Func&lt;IQueryable&lt;PersonModel&gt;, IIncludableQueryable&lt;PersonModel, object&gt;&gt;&gt;&gt; includeProperties = null)
{
    ICollection&lt;PersonModel&gt; list = repository.GetList(filter, orderBy, includeProperties);
    return list.ToList();
}

}

The repository layer references the business model and data model (POCOs) classes and has no knowledge of the EF layer. public class PersonRepository : IPersonRepository { private IPersonStore store;

    public ICollection<PersonModel> GetList(Expression<Func<PersonModel, bool>> filter = null, Expression<Func<IQueryable<PersonModel>, IQueryable<PersonModel>>> orderBy = null, ICollection<Expression<Func<IQueryable<PersonModel>, IIncludableQueryable<PersonModel, object>>>> includeProperties = null)
    {
        Expression<Func<Person, bool>> f = filter.MapExpression<Func<PersonModel, bool>, Func<Person, bool>>();
        Expression<Func<IQueryable<Person>, IQueryable<Person>>> mappedOrderBy = orderBy.MapExpression<Func<IQueryable<PersonModel>, IQueryable<PersonModel>>, Func<IQueryable<Person>, IQueryable<Person>>>();
        ICollection<Expression<Func<IQueryable<Person>, IIncludableQueryable<Person, object>>>> includes = mapper.MapIncludesList<Func<IQueryable<PersonModel>, IIncludableQueryable<PersonModel, object>>, Func<IQueryable<Person>, IIncludableQueryable<Person, object>>>(includeProperties);

        ICollection<Person> list = store.Get(f,
                mappedOrderBy == null ? null : mappedOrderBy.Compile(),
                includes == null ? null : includes.Select(i => i.Compile()).ToList());
        return Mapper.Map<IEnumerable<Person>, IEnumerable<PersonModel>>(list).ToList();
    }
}

The EF layer references the data model (POCOs) and has no knowledge of the business model.

public class PersonStore : IPersonStore
{
    public IList<Person> Get(Expression<Func<Person, bool>> filter = null, Func<IQueryable<Person>, IQueryable<Person>> orderBy = null, ICollection<Func<IQueryable<Person>, IIncludableQueryable<Person, object>>> includeProperties = null)
    {
        IList<Person> list = null;
        using (IPersonUnitOfWork unitOfWork = new PersonUnitOfWork())
        {
            list = new PersonDbMapper(unitOfWork).Get(filter, orderBy, includeProperties);
        }

        return list;
    }
}

To get started:

  • Run the .NET Core 1.1 SDK - Installer and Visual Studio 2015 Tools (Preview 2) from here.
  • Build the Solution.

Create the database

  • Search the solution for "Server=.\SQL2016;Database=SchoolDb02" and update connection strings if necessary.
  • Open a command window at {Solution Folder}\src\MigrationTool then execute following command:
    • dotnet ef database update

Seed the database

Execute StoreTestConsole to seed the database.

Run the Web API Solution

Set ContosoUniverity.WebApi as the start up project and press CTRL+F5 to run the Web API project.

To run the Angular 2 project:

Get the latest versions of nodejs and npm as described here. Open a command window at {Solution Folder}\src\ContosoUniverity.Angular2.Web and run:

  • npm install
  • npm install -g angular-cli
  • ng serve

Open the browser as described in the output. NG Live Development Server is running on http://localhost:{port#}.

xpressionmapper.netcore's People

Contributors

blaised avatar

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.