抽象自动映射器预测
Abstract Automapper Projections
我使用 Entity Framework 开发网络 api 应用程序 6.
我想集成并开始使用 Automapper 与我的 EF 实体模型进行映射。
我已经阅读了有关投影的内容,并意识到如果我决定使用 Automapper,则有必要使用 Project().To<> 以获得更好的性能。但是,我不想将我的 DAL 暴露给 Automapper 库。
有没有办法抽象出 Automapper Project()?
提前致谢
- 用"projection"方法创建接口,可以复制
原始 AutoMapper 方法。
- 然后创建一个具体的实现。
- 之后将此接口添加到存储库的构造函数中。
- 使用它
- 将接口注册到您的依赖项注入容器。
- 按 F5
这是一个完整的例子
(当然,每个 class/interface 都在正确的图层中)。
using AutoMapper;
using AutoMapper.QueryableExtensions;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
namespace ConsoleApplicationMapper
{
class Program
{
static void Main(string[] args)
{
Mapper.Initialize(c =>
{
c.CreateMap<Customer, CustomerModel>();
});
//If you use a dependency injection container you don't have to use the constructors
var repository = new CustomerRepository(new EntityProjector());
foreach (var item in repository.GetCustomers())
{
Console.WriteLine(item.Name);
}
Console.ReadLine();
}
}
public class CustomerModel
{
public int CustomerId { get; set; }
public string Name { get; set; }
}
public interface IEntityProjector
{
IQueryable<TDestination> ProjectTo<TDestination>(IQueryable source, params Expression<Func<TDestination, object>>[] membersToExpand);
}
public class EntityProjector : IEntityProjector
{
public IQueryable<TDestination> ProjectTo<TDestination>(IQueryable source, params Expression<Func<TDestination, object>>[] membersToExpand)
{
return source.ProjectTo(membersToExpand);
}
}
public interface ICustomerRepository
{
IEnumerable<CustomerModel> GetCustomers();
}
public class CustomerRepository : ICustomerRepository
{
private readonly IEntityProjector projector;
public CustomerRepository(IEntityProjector theProjector)
{
projector = theProjector;
}
public IEnumerable<CustomerModel> GetCustomers()
{
MyContext context = new MyContext();
//Uncomment this if you want to confirm that only CustomerId,Name are selected and not LastName
//context.Database.Log = s => Console.WriteLine(s);
return context.Customers.SelectTo<CustomerModel>();
}
}
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
public static class MyExtentions
{
static IEntityProjector projector;
static MyExtentions()
{
//You can get it from your dependecy injection container if you use one
projector = new EntityProjector();
}
//I renamed this SelectTo instead of ProjectTo so you don't have any conflict if you use AutoMapper
//Change it to to ProjectTo if you want
public static IQueryable<TDestination> SelectTo<TDestination>(this IQueryable source, params Expression<Func<TDestination, object>>[] membersToExpand)
{
return projector.ProjectTo<TDestination>(source);
}
}
}
我使用 Entity Framework 开发网络 api 应用程序 6. 我想集成并开始使用 Automapper 与我的 EF 实体模型进行映射。
我已经阅读了有关投影的内容,并意识到如果我决定使用 Automapper,则有必要使用 Project().To<> 以获得更好的性能。但是,我不想将我的 DAL 暴露给 Automapper 库。
有没有办法抽象出 Automapper Project()?
提前致谢
- 用"projection"方法创建接口,可以复制 原始 AutoMapper 方法。
- 然后创建一个具体的实现。
- 之后将此接口添加到存储库的构造函数中。
- 使用它
- 将接口注册到您的依赖项注入容器。
- 按 F5
这是一个完整的例子
(当然,每个 class/interface 都在正确的图层中)。
using AutoMapper;
using AutoMapper.QueryableExtensions;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
namespace ConsoleApplicationMapper
{
class Program
{
static void Main(string[] args)
{
Mapper.Initialize(c =>
{
c.CreateMap<Customer, CustomerModel>();
});
//If you use a dependency injection container you don't have to use the constructors
var repository = new CustomerRepository(new EntityProjector());
foreach (var item in repository.GetCustomers())
{
Console.WriteLine(item.Name);
}
Console.ReadLine();
}
}
public class CustomerModel
{
public int CustomerId { get; set; }
public string Name { get; set; }
}
public interface IEntityProjector
{
IQueryable<TDestination> ProjectTo<TDestination>(IQueryable source, params Expression<Func<TDestination, object>>[] membersToExpand);
}
public class EntityProjector : IEntityProjector
{
public IQueryable<TDestination> ProjectTo<TDestination>(IQueryable source, params Expression<Func<TDestination, object>>[] membersToExpand)
{
return source.ProjectTo(membersToExpand);
}
}
public interface ICustomerRepository
{
IEnumerable<CustomerModel> GetCustomers();
}
public class CustomerRepository : ICustomerRepository
{
private readonly IEntityProjector projector;
public CustomerRepository(IEntityProjector theProjector)
{
projector = theProjector;
}
public IEnumerable<CustomerModel> GetCustomers()
{
MyContext context = new MyContext();
//Uncomment this if you want to confirm that only CustomerId,Name are selected and not LastName
//context.Database.Log = s => Console.WriteLine(s);
return context.Customers.SelectTo<CustomerModel>();
}
}
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
public static class MyExtentions
{
static IEntityProjector projector;
static MyExtentions()
{
//You can get it from your dependecy injection container if you use one
projector = new EntityProjector();
}
//I renamed this SelectTo instead of ProjectTo so you don't have any conflict if you use AutoMapper
//Change it to to ProjectTo if you want
public static IQueryable<TDestination> SelectTo<TDestination>(this IQueryable source, params Expression<Func<TDestination, object>>[] membersToExpand)
{
return projector.ProjectTo<TDestination>(source);
}
}
}