DotNet Core AA.FrameWork introduction
Open source application infrastructure framework, AA.FrameWork, can be regarded as giving back to the community and making a little contribution. I hope to help me to enter the market similar to that year. .
AA.FrameWork is a basic application framework created based on the popular open source library of .NET core.
The framework code diagram is as follows:
Source code github address: https://github.com/ChengLab/AAFrameWork
In nuget management, the search prefix name AA can be as shown in the figure.
Nuget Packages
PackageNugetDescription
AA.Dapper
Nuget
Based on dapper, Dapper-FluentMap development, support for work units, warehousing modes and native dapper operations
AA.Log4Net
Nuget
Based on Log4net development, provide common log operation classes
AA.FrameWork
Nuget
Basic framework
AA.Redis
Nuget
Based on StackExchange.Redis development, providing a variety of redis operations and redis-based distributed locks
AA.AutoMapper
Nuget
Object mapping based on AutoMapper development
AA.ServiceBus
Nuget
Service bus based on MassTransit; convenient for application in event bus and publish/subscribe scenarios
AA.AspNetCore
Nuget
Aspnetcore common class collection
AA.Dapper usage
Entity mapping configuration
Use the DommelEntityMap<TEntity> class to map attribute names. Create a derived class, set the Map method in the constructor, you can specify a property mapping to the database column name
Public class UserInfoMap : DommelEntityMap<UserInfo> { Public UserInfoMap() { Totable ( " Sys_UserInfo " ); // mapping table specifically the Map (P => p.SysNo) .IsKey (); // primary key is specified, IsIdentity incremented if the Map (P => p.GmtCreate) .ToColumn ( " GmtCreateDate " ); / / attribute name and database column name can be different Map (p => p.LastLoginDate). Ignore (); / / some calculation properties, you can ignore the need to map with the database column } }
Use the MapConfiguration.Init method to initialize the mapping class, which can be used later.
Public static void InitMapCfgs() { Var fluentMapconfig = new List<Action<FluentMapConfiguration>> (); fluentMapconfig.Add(cfg => { cfg.AddMap( new UserInfoMap()); }); MapConfiguration.Init(fluentMapconfig); }
Start using AA.Dapper
Use DapperContext to set the database connection and database type is sqlserver or mysql
Public class AADapperContext : DapperContext { Public AADapperContext() : base ( new NameValueCollection() { [ " aa.dataSource.AaCenter.connectionString " ] = " Data Source =.; Initial Catalog = AaCenter; User ID = sa; Password = 123; " , [ " aa.dataSource.AaCenter.provider " ] = " SqlServer " }) { } }
Warehousing contains most of the operations, while supporting Async operations
IDapperRepository<UserInfo> userInfoRepository = new DapperRepository<UserInfo>();
Insert entity
Var user = new UserInfo() { UserName = " chengTian " , RealName = " 成天" , GmtCreate = DateTime.Now, GmtModified = DateTime.Now }; Var result = userInfoRepository.Insert(user);
Modify entity
Var user = userInfoRepository.Get( 1 ); user.GmtModified = DateTime.Now; var result = userInfoRepository.Update(user);
Get entity
Var user = userInfoRepository.Get( 1 ); // By primary key var users = userInfoRepository.GetAll(); // all var users = userInfoRepository.Select(p => p.UserName == " chengTian " ); // predicate
Delete entity
Var user = userInfoRepository.Get( 1 ); var result = userInfoRepository.Delete(user);
Support Dapper native operation
The basic package operation is a single-table operation, which can satisfy some business development. Some business scenarios are more suitable for writing sql, such as reports and some complicated queries. It is recommended to use the native dapper operation.
Public class UserInfoRepository : DapperRepository<UserInfo> , IUserInfoRepository { Private readonly IDapperContext _dapperContext; public UserInfoRepository(IDapperContext context) { _dapperContext = context; } Public IEnumerable<UserInfo> QueryAll() { Var result = _dapperContext.DataBase.Query<UserInfo>( " SELECT * from [Sys_UserInfo] " ); // instance return result; } }
AA.Log4Net usage
- Log4net.config configuration, and copy the log4net.config properties -> to the output directory -> set to -> always copy
- Log4NetLogger.Use(“configuration file name or path + configuration file name”); For example: log4net.config file in the root directory, Log4NetLogger.Use(“log4net.config”); if in a custom file; for example config/log4net .config then Log4NetLogger.Use(“config/log4net.config”)
- ILog log= Logger.Get(typeof(class)); log.Debug(“example”);
AA.ServiceBus usage
Producer (two types of events and commands)
Instantiate bus
// Events IBusControl busControl = ServiceBusManager.Instance.UseRabbitMq(rabbitMqUri, rabbitMqUserName, rabbitMqPassword) .BuildEventProducer(); // Command ISendEndpoint busControl = ServiceBusManager.Instance.UseRabbitMq(rabbitMqUri, rabbitMqUserName, rabbitMqPassword) .BuildCommandProducer(queueName);
Send and publish two methods after instantiating bus
// Command TaskUtil.Await(busControl.Send<SubmitOrder>( new { Id = 10 })); // Event TaskUtil.Await(busControl.Publish<OrderSubmitted>( new { Id = 1 }));
consumer
[Fact] public void TestConsumer() { Log4NetLogger.Use( " Log4Net/log4net.config " ); string rabbitMqUri = " rabbitmq://localhost:5672 " ; string rabbitMqUserName = "" ; string rabbitMqPassword = "" ; string queueName = " order.queue " ; Var busControl = ServiceBusManager.Instance.UseRabbitMq(rabbitMqUri, rabbitMqUserName, rabbitMqPassword) .RegisterConsumer <SubmitOrderCommandConsumer>(queueName) // Register the command consumer.RegisterConsumer <OrderSubmittedEventConsumer>( null ) // Register the event consumer.Build (); busControl.Start(); }
Defining the consumer needs to implement the interface IConsumer
Public class OrderSubmittedEventConsumer : IConsumer<OrderSubmitted> { Public async Task Consume(ConsumeContext<OrderSubmitted> context) { Var @event = context.Message; Var result = $ " OrderSubmittedEvent {@event.Id.ToString()} " ; ILog log = Logger.Get( typeof (OrderSubmittedEventConsumer)); log.Debug(result); // do somethings... } } Public class SubmitOrderCommandConsumer : IConsumer<SubmitOrder> { Public async Task Consume(ConsumeContext<SubmitOrder> context) { Var command = context.Message; Var result = $ " CreateFooCommand {command.Id.ToString()} " ; ILog log = Logger.Get( typeof (SubmitOrderCommandConsumer)); log.Debug(result); // do somethings... } }
AA.AutoMapper usage
Implement the IMapperConfiguration interface and create a mapping rule configuration
Public class WebMapperConfigurations : IMapperConfiguration { Public int Order { get { return 0 ; } } Public Action<IMapperConfigurationExpression> GetConfiguration () { Action <IMapperConfigurationExpression> action = cfg => { cfg.CreateMap <UserVm, UserInput> (); }; Return action; } }
In the program startup call configuration
Var mapperConfig = new WebMapperConfigurations(); AutoMapperConfiguration.Init( new List<Action<IMapperConfigurationExpression>> { mapperConfig.GetConfiguration() }); ObjectMapping.ObjectMapManager.ObjectMapper = new AutoMapperObjectMapper();
Perform mapping using the extension method MapTo
[Fact] public void TestMap() { // init Init(); UserVm UserVm = new new UserVm = {Id . 1 , the Name = " all day " , = Remark, the " micro-channel public number: dotNet Salon " }; var UserDTO = userVm.MapTo <UserInput> (); // var userDto2 = userVm.MapTo <UserVm , UserInput>(); }
Application examples based on the AA framework will also be provided later. If you have any questions, you can contact me and communicate and grow together.
Orignal link:https://www.cnblogs.com/chengtian/p/11684860.html