相关疑难解决方法(0)

.NET中每个文件规则一个类?

我遵循这条规则,但我的一些同事不同意它,并认为如果一个类较小,它可以与其他类保留在同一个文件中.

我一直听到的另一个论点是"即使微软不这样做,我们为什么要这样做?"

对此有何普遍共识?是否有应避免的情况?

.net c#

181
推荐指数
13
解决办法
5万
查看次数

在Bootstrapper中配置Automapper违反了开放封闭原则?

我在引导程序配置Automapper和我打电话Bootstrap()Application_Start(),我一直在说,这是错误的,因为我要修改我的Bootstrapper每一次我必须添加一个新的映射类,所以我违反了开闭原则.

你觉得怎么样,我真的违反了这个原则吗?

public static class Bootstrapper
{
    public static void BootStrap()
    {
        ModelBinders.Binders.DefaultBinder = new MyModelBinder();
        InputBuilder.BootStrap();
        ConfigureAutoMapper();
    }

    public static void ConfigureAutoMapper()
    {
        Mapper.CreateMap<User, UserDisplay>()
            .ForMember(o => o.UserRolesDescription,
                       opt => opt.ResolveUsing<RoleValueResolver>());
        Mapper.CreateMap<Organisation, OrganisationDisplay>();
        Mapper.CreateMap<Organisation, OrganisationOpenDisplay>();
        Mapper.CreateMap<OrganisationAddress, OrganisationAddressDisplay>();
    }    
}
Run Code Online (Sandbox Code Playgroud)

.net bootstrapping automapper open-closed-principle solid-principles

35
推荐指数
1
解决办法
9710
查看次数

Global.asax中的多个AutoMapper.Configure()

我正在使用AutoMapper在DTO对象和业务对象之间进行映射.我有两个AutoMapperConfiguration.cs文件 - 一个在我的服务层,另一个在我的web api层.

如以下链接的答案所示, AutoMapper.CreateMaps的位置在哪里?

我在Global.asax类中调用这两个文件的Configure()

AutoMapperWebConfiguration.Configure();
AutoMapperServiceConfiguration.Configure();
Run Code Online (Sandbox Code Playgroud)

但似乎我的服务配置调用(第二次调用)正在覆盖web api层(第一次调用)的映射,我得到一个异常,说明Mapping缺失.

如果我将配置调用反转为这样

AutoMapperServiceConfiguration.Configure();
AutoMapperWebConfiguration.Configure();
Run Code Online (Sandbox Code Playgroud)

我没有得到web api映射的异常,但我得到了Service层的相同映射异常.

我做错了什么,因为这在上面的堆栈溢出链接中明确标记为答案?

这是我的代码:

public static class AutoMapperServiceConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile<CmciFlowTestToGenericFlowTestSimpleMappingProfile>();
            x.AddProfile<FsrsFlowTestToGenericFlowTestSimpleMappingProfile>();
        });
    }
}

public class FsrsFlowTestToGenericFlowTestSimpleMappingProfile : Profile
{
    protected override void Configure()
    {
        Mapper.CreateMap<FsrsFlowTest, GenericFlowTest>()
            .ConvertUsing<FsrsFlowTestToGenericFlowTestSimpleConverter>();
    }
}

public class FsrsFlowTestToGenericFlowTestSimpleConverter : TypeConverter<FsrsFlowTest, GenericFlowTest>
{
    protected override GenericFlowTest ConvertCore(FsrsFlowTest source)
    {
        if (source == null)
        {
            return null;
        }

        return new GenericFlowTest
            {
                FlowTestDate = source.FlowTestDates, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net automapper asp.net-web-api

18
推荐指数
3
解决办法
2万
查看次数

如何使用Simple Injector注册AutoMapper 4.2.0

已更新至AutoMapper 4.2.0,并按照此处提供的迁移指南进行操作:https://github.com/AutoMapper/AutoMapper/wiki/Migrating-from-static-API/f4784dac61b91a0df130e252c91a0efd76ff51de#preserving-static-feel.尝试将StructureMap的页面上的代码转换为Simple Injector.有人能告诉我这个代码在Simple Injector中的样子吗?

StructureMap

public class AutoMapperRegistry : Registry
{
    public AutoMapperRegistry()
    {
        var profiles =
            from t in typeof (AutoMapperRegistry).Assembly.GetTypes()
            where typeof (Profile).IsAssignableFrom(t)
            select (Profile)Activator.CreateInstance(t);

        var config = new MapperConfiguration(cfg =>
        {
            foreach (var profile in profiles)
            {
                cfg.AddProfile(profile);
            }
        });

        For<MapperConfiguration>().Use(config);
        For<IMapper>().Use(ctx => ctx.GetInstance<MapperConfiguration>().CreateMapper(ctx.GetInstance));
    }
}
Run Code Online (Sandbox Code Playgroud)

简单的注射器

?
Run Code Online (Sandbox Code Playgroud)

c# automapper simple-injector

9
推荐指数
2
解决办法
3952
查看次数

在哪里定义AutoMapper映射?

在我的ASP.NET MVC应用程序中,我应该定义AutoMapper映射吗?

Mapper.CreateMap<User, UserViewModel>();
Run Code Online (Sandbox Code Playgroud)

目前我在BaseController的构造函数中定义了这些,我的所有Controlllers都是从这个构造函数派生出来的.这是最好的地方吗?

automapper asp.net-mvc-3

7
推荐指数
1
解决办法
2408
查看次数

在ASP.Net MVC中使用AutoMapper的正确方法

我正在尝试开始使用ViewModels - 但是我遇到了这个POST无法验证的问题 - 模型中的值显示在代码下方的Watch部分中:

ModelStats.IsValid = false

无效的ModelState

我的ItemViewModel是:

  public class ItemViewModel
  {
    public int ItemId { get; set; }
    [Display(Name = "Item")]
    public string ItemName { get; set; }
    [Display(Name = "Description")]
    public string Description { get; set; }
    [Display(Name = "Price")]
    public double UnitPrice { get; set; }
    [Range(0.00, 100, ErrorMessage = "VAT must be a % between 0 and 100")]
    public decimal VAT { get; set; }
    [Required]
    public string UserName { get; set; }
   }
Run Code Online (Sandbox Code Playgroud)

我相信这会很简单 …

c# asp.net-mvc automapper asp.net-mvc-3

5
推荐指数
1
解决办法
4万
查看次数

在C#asp.net性能实践中使用静态自动映射器

预先,对于这个问题是否已经在其他地方得到解答,我深表歉意。但是我在这件事上发现了很多不同的结果。

我正在使用:

  • .net Framework 4.6.1
  • Microsoft.AspNet.Mvc 5.2.6
  • AutoMapper 6.2.2
  • 实体框架6.2.0

我对ASP.net和C#都是陌生的,最近我成为了AutoMapper软件包的粉丝。我主要是使用它来将我的实体转换ApplicationDbContextDTO的(数据传输对象)或ViewModel

现在,我在应用程序中使用此设置进行初始化并在Mapper中使用Controller

Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        Mapper.Initialize(AutoMapperConfiguration.Configure);

        // Other configuration for MVC application...
    }
}
Run Code Online (Sandbox Code Playgroud)

AutoMapperConfiguration.cs

public static class AutoMapperConfiguration
{
    public static void Configure(IMapperConfigurationExpression config)
    {
        config.CreateMap<Post, Post.DetailsViewModel>().ForMember(post => post.CanEdit, cfg => cfg.ResolveUsing((src, dst, arg3, context) => context.Options.Items["UserId"]?.ToString() == src.UserId));
    }
}
Run Code Online (Sandbox Code Playgroud)

PostsController.cs和Post.cs

public class PostsController : Controller
{
    private ApplicationDbContext db …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc entity-framework automapper

1
推荐指数
1
解决办法
2599
查看次数