AA.Framework is built on the popular open source class library of NET Core
- log:Log4net
- object mapper:automapper
- orm:dapper(Dapper-FluentMap,Dommel)
- MQ:RabbitMQ.Client
- Redis:StackExchange.Redis
- message bus:MassTransit
- more.....
Package | nuget | description |
---|---|---|
AA.Dapper | nuget | 基于dapper,Dapper-FluentMap 开发 , 支持工作单元、仓储模式和原生dapper的操作 |
AA.Log4Net | nuget | 基于Log4net开发,提供常用的日志操作类 |
AA.FrameWork | nuget | 基础框架 |
AA.Redis | nuget | 基于StackExchange.Redis开发,提供各种redis操作和基于redis的分布式锁 |
AA.AutoMapper | nuget | 基于AutoMapper开发,对象映射 |
AA.ServiceBus | nuget | 基于MassTransit开发的服务总线 ;很方便的应用在event bus 和 publish/subscribe 场景 |
AA.AspNetCore | nuget | aspnetcore常用类集合 |
使用DommelEntityMap类映射属性名称。创建一个派生类,在构造函数中设置Map方法,可指定某个属性映射到数据库列名
public class UserInfoMap : DommelEntityMap<UserInfo>
{
public UserInfoMap()
{
ToTable("Sys_UserInfo");//映射具体的表名
Map(p => p.SysNo).IsKey();//指定主键 ,IsIdentity是否自增
Map(p=>p.GmtCreate).ToColumn("GmtCreateDate"); //属性名和数据库列名 可以不同
Map(p=>p.LastLoginDate).Ignore();//一些计算属性,可以忽略不需要跟数据库列进行映射
}
}
使用MapConfiguration.Init方法,把映射类初始化,后续就可以使用了
public static void InitMapCfgs()
{
var fluentMapconfig = new List<Action<FluentMapConfiguration>>();
fluentMapconfig.Add(cfg =>
{
cfg.AddMap(new UserInfoMap());
});
MapConfiguration.Init(fluentMapconfig);
}
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"
})
{ }
}
IDapperRepository<UserInfo> userInfoRepository = new DapperRepository<UserInfo>();
var user = new UserInfo()
{
UserName = "chengTian",
RealName = "成天",
GmtCreate = DateTime.Now,
GmtModified = DateTime.Now
};
var result = userInfoRepository.Insert(user);
var user = userInfoRepository.Get(1);
user.GmtModified = DateTime.Now;
var result = userInfoRepository.Update(user);
var user = userInfoRepository.Get(1);//By 主键
var users = userInfoRepository.GetAll();//所有
var users = userInfoRepository.Select(p => p.UserName == "chengTian");//谓词
var user = userInfoRepository.Get(1);
var result = userInfoRepository.Delete(user);
var result = _userRepository.From(sql =>
{
sql.Select()
.Where(x=>x.UserName==input.UserName)
.Page(input.PageIndex, input.PageSize);
});
using (var dbTransaction = dapperContext.BeginTransaction())
{
try
{
var user = new UserInfo()
{
UserName = "chengTian",
RealName = "成天",
GmtCreate = DateTime.Now,
GmtModified = DateTime.Now,
LastLoginDate = DateTime.Now
};
userInfoRepository.Insert(user);
userAlbumRepository.Insert(new UserAlbum
{
Pic = "image/one.jgp"
});
dapperContext.Commit();
}
catch (Exception ex)
{
dbTransaction.Rollback();
}
}
var where = DynamicWhereExpression.Init<User>();
if (input.RealName != "")
{
where = where.And(x => x.RealName.Contains(input.RealName));
}
if (input.SysNo!=Guid.Empty)
{
where = where.And(x=>x.SysNo==input.SysNo);
}
if (input.SysNos.Any())
{
where = where.And(x=>input.SysNos.Contains(x.SysNo));
}
var result = _userRepository.From(sql =>
{
sql.Select()
.Where(where)
.Page(input.PageIndex, input.PageSize);
});
操作基本的封装都是单表的操作,可以满足一部分业务开发,有些业务场景编写sql还是比较合适的比如报表和一些复杂的查询,推荐使用原生dapper的操作
public class UserInfoRepository : DapperRepository<UserInfo>, IUserInfoRepository
{
public IEnumerable<UserInfo> QueryAll()
{
var result = DapperContext.Current.DataBase.Query<UserInfo>("SELECT * from [Sys_UserInfo]");//实例
return result;
}
}
- log4net.config 配置,并将log4net.config的属性->复制到输出目录->设置为->始终复制
- Log4NetLogger.Use("配置文件名或者路径+配置文件名"); 例如:log4net.config文件在根目录下,Log4NetLogger.Use("log4net.config");如果在自定义文件中;例如config/log4net.config 则Log4NetLogger.Use("config/log4net.config")
- ILog log= Logger.Get(typeof(类)); log.Debug("example");
实例化bus
//事件
IBusControl busControl = ServiceBusManager.Instance.UseRabbitMq(rabbitMqUri, rabbitMqUserName, rabbitMqPassword)
.BuildEventProducer();
//命令
ISendEndpoint busControl = ServiceBusManager.Instance.UseRabbitMq(rabbitMqUri, rabbitMqUserName, rabbitMqPassword)
.BuildCommandProducer(queueName);
实例化bus之后可以使用发送和发布两个方法
//命令
TaskUtil.Await(busControl.Send<SubmitOrder>(new
{
Id = 10
}));
//事件
TaskUtil.Await(busControl.Publish<OrderSubmitted>(new
{
Id = 1
}));
[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)//注册命令消费者
.RegisterConsumer<OrderSubmittedEventConsumer>(null)//注册事件消费者
.Build();
busControl.Start();
}
定义消费者需要实现接口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...
}
}
public class WebMapperConfigurations : IMapperConfiguration
{
public int Order { get { return 0; } }
public Action<IMapperConfigurationExpression> GetConfiguration()
{
Action<IMapperConfigurationExpression> action = cfg =>
{
cfg.CreateMap<UserVm, UserInput>();
};
return action;
}
}
var mapperConfig = new WebMapperConfigurations();
AutoMapperConfiguration.Init(new List<Action<IMapperConfigurationExpression>> { mapperConfig.GetConfiguration() });
ObjectMapping.ObjectMapManager.ObjectMapper = new AutoMapperObjectMapper();
[Fact]
public void TestMap()
{
//init
Init();
UserVm userVm = new UserVm { Id = 1, Name = "成天" ,Remark="微信公众号:dotNet知音"};
var userDto = userVm.MapTo<UserInput>();
//var userDto2 = userVm.MapTo<UserVm,UserInput>();
}