我尝试仅使用其接口映射两个对象,但返回的值始终是具有空/ null属性的对象:
域名界面:
public interface ICustomer
{
int Age { get; }
string Name { get; }
}
DAL界面:
public interface ICustomerEntity
{
int Age { get; }
string Name { get; }
}
我尝试使用以下代码映射它们:
class Program
{
static void Main(string[] args)
{
MapperConfig.RegisterMappings();
ICustomer customer1 = new Customer("John", 30);
ICustomer customer2 = new Customer("Mary", 30);
var customerEntity = Mapper.Map<ICustomer, ICustomerEntity>(customer1);
var customerReturned = Mapper.Map<ICustomer>(customerEntity);
}
}
这是我的配置文件:
public class MapperConfig
{
public static void RegisterMappings()
{
Mapper.Initialize(c =>
{
c.AddProfile<DomainToEntitiesMappingProfile>();
c.AddProfile<EntitiesToDomainMappingProfile>();
});
}
}
这些是我的个人资料:
class DomainToEntitiesMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<ICustomer, ICustomerEntity>()
.Include<Customer, CustomerEntity>();
}
}
class EntitiesToDomainMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<ICustomerEntity, ICustomer>()
.Include<CustomerEntity, Customer>();
}
}
当我尝试映射具体类(删除接口)时,它工作正常,但我不想将我的映射耦合到具体的类。
如何实现?
我已尝试使用与AutoMapper 6相同的方法,但它也没有用。
答案 0 :(得分:3)
问题在于您的界面缺少任何公共制定者:
public interface ICustomer
{
int Age { get; }
string Name { get; }
}
您需要添加它们:
public interface ICustomer
{
int Age { get; set; }
string Name { get; set; }
}