ASP.NET MVC5 achieve Mango Distribution background management system (a): system architecture design, integration AutoMapper, Log4net

After completion of the idea of ​​systems thinking brain map, small ink returned home, he began gearing up development work. To the speedy completion of system development, system design and pre-preparation is particularly important because small ink done too many large and small business systems, preparation work is also very smooth.

system structure

file

As the structure of the entire project, based on the traditional three-tier architecture model (go and do not understand the study intensively)

  • Web: Based on MVC project template to build the system model, view, controller
  • Service: Business
  • Repository: Persistence layer, integrated EntityFramework
  • Tracking: Log Component Integration Log4net
  • CommonUtils: public tool that provides encryption and decryption / serialization / Excel process, provided Castle Ioc management

Man of few words said, directly on the dry

Integrated Log4net

Although Li's mango business system is so simple, it has always had various architectural features small ink system planning OCD still rhythmic, small things are perfect ink consistent style. Logs can save you, of course not! The following integration Log4net.

Download Nuget

Right here direct search for solutions Nuget Log4net, install the latest version 5.2.7

file

Log profile

            <configuration>
                <configSections>
                    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
                </configSections>

                <log4net debug="true">
                    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
                        <file value="Logs/Log" />
                        <appendToFile value="true" />
                        <rollingStyle value="Composite" />
                        <staticLogFileName value="false" />
                        <datePattern value="_yyyy.MM.dd.'log'" />
                        <maxSizeRollBackups value="20" />
                        <maximumFileSize value="5MB" />
                        <layout type="log4net.Layout.PatternLayout">
                            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
                        </layout>
                    </appender>
                    <root>
                        <level value="DUBEG" />
                        <appender-ref ref="RollingLogFileAppender" />
                    </root>
                </log4net>
            </configuration>

Some need to explain:

        <file value="Logs/Log" /> 配置logs打印路径
        <staticLogFileName value="false" />  配置Log文件名非静态,因为小墨想每天生成一个Log文件
        <datePattern value="_yyyy.MM.dd.'log'" /> 日志文件格式 Log_2020.03.20.log

Log in to start the configuration Global.asax.cs

        public class LoggerFactory
            {
                    public static void Config()
                    {
                            var baseDir = AppDomain.CurrentDomain.BaseDirectory;
                            var filePath = Path.Combine(baseDir, "Config\\Log4net.config");
                            var fileInfo = new FileInfo(filePath);
                            log4net.Config.XmlConfigurator.Configure(fileInfo);
                    }

            }

Then you can use the elegant log it!

//statement

    ILog logger = log4net.LogManager.GetLogger(typeof(MvcApplication));

//use

    logger.Info("大青芒管理系统已启动.");

ILog here again next package, supports parametric print log can be optimized under.

Integrated AutoMapper

First Model at the whole system, Dto, Entity, according to business needs to be defined is relatively simple, divided into user (proxy table), the Orders table, so there
UserModel / UserInfo / userEntity
OrderModel / OrdeDto / OrderEntity
file

file

Here inevitable transformation used various objects to each other, of course, we will not field one by one assignment, weak, here we use with AutoMapper, because He is widely used in business after us in.

Nuget introduced AutoMapper

file

AutoMapper Configuration

file

Because here, with my Dto Entity are simple types, and type are the same, so you can directly Map, but generally slightly more complex business, often nesting in Dto Dto other, or does not match the field type is we mainly use the following two methods AutoMapper to do the conversion, the example here, I do not do.

ForMember
ResolveUsing
    

When Global.asax.cs system startup, startup Map configuration. Here the introduction of a AutoMapperInit class Service layer, about the transition calls the Initialize method Repository, and do not want to layer into AutoMapper Service packages, all with Dto Entity conversion process in the Repository

    AutoMapperInit.Init();
    

Use AutoMapper
file

Thanks for reading!

file

This article from the blog a mass text multiple tools such as operational platform OpenWrite release

Guess you like

Origin www.cnblogs.com/dalianmaodada/p/12536429.html