응용 프로그램 사용량이 늘어남에 따라 시간이 지남에 따라 서서히 밝혀진 문제로 인해 서서히 찢어졌습니다. DI를 사용하여 저장소의 멋진 아키텍처를 사용하고 있습니다.DI 컨테이너의 인스턴스화 유형을 변경하거나 100+ 메소드를 변경하십시오.
응용 프로그램을 개발할 때 세션 당 하나만 실행되고 필요할 때 인스턴스화되기 때문에 데이터 컨트롤러를 제공하기 위해 단일 패턴이 선택되었습니다.
그러나 사용자 사용량이 늘어 났으며 스레딩 문제가 발생했습니다.
나는 75 개 정도의 데이터 컨트롤러를 가지고 있으며 모두 다음 패턴으로 개발되었습니다.
public class MyService : IMyServiceBase
{
internal MyServiceDataDataContext _dataContext = null;
protected IConfigurationService _systemService;
public MyService(IConfigurationService systemService)
{
_systemService = systemService;
_dataContext = new MyServiceDataDataContext(_systemService.GetCurrentSystem().WriteOnlyDatabase.ConnectionString);
_dataContext.ObjectTrackingEnabled = false;
}
public Domain.Data.MyObject GetSomething(int id)
{
return (
from s in _dataContext.MyObjects
select Mapper.Map<MyObject, Domain.Data.MyObject>(s)
).SingleOrDefault();
}
}
이제 일부 조사를 한 후 가장 많이 사용되는 컨트롤러와 관련하여 약간의 동시성 문제가 있음을 발견했습니다. 나는 가장 많이 사용되는 아래에있는 수정 프로그램을 적용 해왔고 프로덕션 서버에서 내 문제를 해결하고 있음을 발견했습니다. 요약하면, 각 함수 호출에서 요청 당 datacontext를 의도 한 한 번의 싱글 톤 방식으로 생성합니다.
public class MyService : IMyServiceService
{
internal MyServiceDataDataContext _dataContext = null;
protected IConfigurationService _systemService;
public MyService(IConfigurationService systemService)
{
_systemService = systemService;
_dataContext = new DistrictDataDataContext(_systemService.GetCurrentSystem().WriteOnlyDatabase.ConnectionString);
_dataContext.ObjectTrackingEnabled = false;
}
public Domain.Data.MyObject GetSomething(int id)
{
using (_dataContext = new MyServiceDataDataContext(_systemService.GetCurrentSystem().WriteOnlyDatabase.ConnectionString))
{
Mapper.CreateMap<MyObject, Domain.Data.MyObject>();
return (from s in _dataContext.MyObjects
select Mapper.Map<MyObject, Domain.Data.MyObject>(s)).SingleOrDefault();
}
}
}
이제 질문에 대한 ... 모든 75 데이터 컨트롤러로 이동, 일부 컨트롤러는 위의 예에 따라, 스무 방법을 가지고, 아니면 단순히에 싱글 사용을 중지 할 수있는 모든 방법을 리팩토링하는 것이 좋습니다 것 DI 생성자.
다음 스 니펫을 호출 할 때마다 변경하는 방법은 각 메서드에서 생성자를 호출하는 것과 동일합니까?
AppServiceFactory.Instance.RegisterTypeSingleton<IMyService>(typeof(My.Resources.Data.MyService), "MyService");
감사