AutoMapper - 多个 Class 库
AutoMapper - Multiple Class Libraries
我有一个 ASP.NET Web 表单应用程序和一个 Class 库,它们都定义了自己的 AutoMapper.Profiles
classes。一个例子是:
public class MyMappings : AutoMapper.Profile
{
public MyMappings() : base("MyMappings")
{
}
protected override void Configure()
{
Mapper.CreateMap<DAL.User, SessionUser>()
.ForMember(
dest => dest.LocationName,
opt => opt.MapFrom(src => src.LocationForUser.Name));
}
}
为了配置两个映射并使一个映射配置文件不覆盖其他映射配置文件,我应该只有一个 AutoMapperConfig(在 ASP.NET 表单应用程序中定义)是否正确,如下所示:?
namespace PlatformNET.Mappings
{
public class AutoMapperConfig
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<MyMappings>();
x.AddProfile<AssessmentClassLib.Mappings.MyMappings>();
});
}
}
}
并从 Global.asax 调用一次,如下所示?
void Application_Start(object sender, EventArgs e)
{
PlatformNET.Mappings.AutoMapperConfig.Configure();
}
我目前在网络应用程序和 Class 库中都有一个 AutoMapperConfig
class,我正在从 Global.asax 依次调用这两个,但是第二个总是出现覆盖第一个的配置。我需要知道我这样做是否正确。谢谢。
配置文件本身并没有相互覆盖,但每次调用 Mapper.Initialize
都会重置所有以前的 AutoMapper 配置文件。你应该只调用 Mapper.Initialize
一次。
下面的代码演示了这一点:
class Program
{
static void Main(string[] args)
{
CallInitOnce();
Mapper.Reset();
CallInitTwice();
}
static void CallInitOnce()
{
// add profiles one time
Mapper.Initialize(x =>
{
x.AddProfile<ClassAToClassBProfile>();
x.AddProfile<EmptyProfile>();
});
var classA = new ClassA()
{
MyProperty = 2
};
var classB = Mapper.Map<ClassB>(classA);
Debug.Assert(classA.MyProperty == classB.MyProperty); // profiles work together
}
static void CallInitTwice()
{
Mapper.Initialize(x =>
{
x.AddProfile<ClassAToClassBProfile>();
});
// previous mapping profile is lost
Mapper.Initialize(x =>
{
x.AddProfile<EmptyProfile>();
});
var classA = new ClassA()
{
MyProperty = 2
};
var classB = Mapper.Map<ClassB>(classA);
Debug.Assert(classA.MyProperty == classB.MyProperty); // this will fail
}
}
我有一个 ASP.NET Web 表单应用程序和一个 Class 库,它们都定义了自己的 AutoMapper.Profiles
classes。一个例子是:
public class MyMappings : AutoMapper.Profile
{
public MyMappings() : base("MyMappings")
{
}
protected override void Configure()
{
Mapper.CreateMap<DAL.User, SessionUser>()
.ForMember(
dest => dest.LocationName,
opt => opt.MapFrom(src => src.LocationForUser.Name));
}
}
为了配置两个映射并使一个映射配置文件不覆盖其他映射配置文件,我应该只有一个 AutoMapperConfig(在 ASP.NET 表单应用程序中定义)是否正确,如下所示:?
namespace PlatformNET.Mappings
{
public class AutoMapperConfig
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<MyMappings>();
x.AddProfile<AssessmentClassLib.Mappings.MyMappings>();
});
}
}
}
并从 Global.asax 调用一次,如下所示?
void Application_Start(object sender, EventArgs e)
{
PlatformNET.Mappings.AutoMapperConfig.Configure();
}
我目前在网络应用程序和 Class 库中都有一个 AutoMapperConfig
class,我正在从 Global.asax 依次调用这两个,但是第二个总是出现覆盖第一个的配置。我需要知道我这样做是否正确。谢谢。
配置文件本身并没有相互覆盖,但每次调用 Mapper.Initialize
都会重置所有以前的 AutoMapper 配置文件。你应该只调用 Mapper.Initialize
一次。
下面的代码演示了这一点:
class Program
{
static void Main(string[] args)
{
CallInitOnce();
Mapper.Reset();
CallInitTwice();
}
static void CallInitOnce()
{
// add profiles one time
Mapper.Initialize(x =>
{
x.AddProfile<ClassAToClassBProfile>();
x.AddProfile<EmptyProfile>();
});
var classA = new ClassA()
{
MyProperty = 2
};
var classB = Mapper.Map<ClassB>(classA);
Debug.Assert(classA.MyProperty == classB.MyProperty); // profiles work together
}
static void CallInitTwice()
{
Mapper.Initialize(x =>
{
x.AddProfile<ClassAToClassBProfile>();
});
// previous mapping profile is lost
Mapper.Initialize(x =>
{
x.AddProfile<EmptyProfile>();
});
var classA = new ClassA()
{
MyProperty = 2
};
var classB = Mapper.Map<ClassB>(classA);
Debug.Assert(classA.MyProperty == classB.MyProperty); // this will fail
}
}