gpt4 book ai didi

c# - 抽象自动映射器预测

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:14 27 4
gpt4 key购买 nike

我使用 Entity Framework 6 开发 Web API 应用程序。我想集成并开始使用 Automapper 与我的 EF 实体模型进行映射。

我已经阅读了有关投影的内容,并意识到如果我决定使用 Automapper,则有必要使用 Project().To<> 以获得更好的性能。但是,我不想将我的 DAL 暴露给 Automapper 库。

有没有办法抽象掉 Automapper Project()?

提前致谢

最佳答案

  1. 用“投影”方法创建一个界面,你可以复制原始 AutoMapper 方法。
  2. 然后创建一个具体的实现。
  3. 之后将此接口(interface)添加到您的存储库的构造函数中。
  4. 使用它
  5. 将接口(interface)注册到您的依赖项注入(inject)容器。
  6. 按 F5

这是一个完整的例子
(当然,您将在正确的层中拥有每个类/接口(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);
}
}
}

关于c# - 抽象自动映射器预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35818056/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com