C#-working-notes

Basic grammer#

How to call async method in sync method#

suppose function A() is marked as async, if we want to call A in function B, usually we use await and marked b as async. However, if we dont want to modify the return type of B. To solve that, I decide to use anonymous function to encapsulate the calling of function A, then receive the return_val of A sync.

1
2
3
4
5
6
7
public int B(){
var asyncTask = Task.Run(async () => {
IEnumerable<returnObj>res = await A();
return res; });
asyncTask.Wait();
return asyncTask.Result().Count();
}

Utility#

ELMAH#

log4net#

#

Introduction#

AutoMapper is an object-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type. What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B. As long as type B follows AutoMapper’s established convention, almost zero configuration is needed to map two types.

Usage#

Once you have your types you can create a map for the two types using a MapperConfiguration and CreateMap. You only need one MapperConfiguration instance typically per AppDomain and should be instantiated during startup. More examples of initial setup see in Setup.

1
var config = new MapperConfiguration(cfg => cfg.CreateMap<Order, OrderDto>());

The type on the left is the source type, and the type on the right is the destination type. To perform a mapping, call one of the Map overloads:

1
2
3
4
var mapper = config.CreateMapper();
// or
var mapper = new Mapper(config);
OrderDto dto = mapper.Map<OrderDto>(order);
1
2
3
4
var obj2 = Mapper.Map<Obj1, Obj2>(obj1);
//obj1 is type of Obj1
//obj2 is type of Obj2
// it will map the same Attribute with Obj1 and Obj2 and return a new obj which type of Obj2