-1

Maybe its a general question , but I really don't know how to find the best solution.In my website, I need to have a select of all companies and subsets in a tree-mode component(this returns a large number of records), almost 80% of my pages needs to be filtered by company-Id (which can be selected by user , or a default value).

Question: I use Angular2,MVC and EF in my project , Is there any way to reduce these request numbers ?since data security is very important to me, is using sessions or similar option proper for me?

Any help or address to a solution would be appreciated.

Sara N
  • 1,079
  • 5
  • 17
  • 45
  • Can you elaborate what is `transaction` ? Is this about `requests` to fetch the options in the select ? – bhantol Dec 19 '16 at 23:15
  • @bhantol sorry I fixed that. I mean request (fetching data) – Sara N Dec 19 '16 at 23:23
  • Need more details... Is it your SQL query statement slow? How does your sql table schema looks like? Are you using index? Or is this not related to SQL, what are you doing with MVC and angular 2 that is continuously requesting data? Perhaps considering adding some code on what you are trying to achieve to the question, and be bit more specific on where the issue is. – penleychan Dec 20 '16 at 00:09
  • @12seconds my question is just about the best way to get data from SQL server when we're using them in various pages.(for example list of companies and subsets which can be like a filter in all pages) the reason I asked this is because this is my first project with Angular and MVC. and I though maybe it would be different from Asp.net webforms. I don't know why some one may give minus rate , without even any hint. I just asked for best approach to that. – Sara N Dec 20 '16 at 00:13

1 Answers1

0

Since there's no code to relate, I can only give you a generic answer. Based on your comment.

You would create a service class for example CompanyService

@Injectable()
export class CompanyService {

    constructor(private http: Http) { }

    private url: string = 'http://yourapiurl/';

    getCompanies(): Observable<ICompany[]> {
        return (this.http.get(this.url + 'api/company')
            .map((response: Response) => {
                return <ICompany[]>response.json();
            })
            .publishReplay(1)
            .refCount()
            .catch(CompanyService.handleError)) as any;
    }

    private static handleError(error: Response) {
        console.error(error);
        return Observable.throw(error.json().error || 'Server error');
    }
}

From there you can inject the service into your components depending where you would need them. For example you need to get the data on your HomeComponent, you would inject the service.

@Component({
    selector: 'app-home',
    templateUrl: 'home.component.html',
    styleUrls: ['home.component.scss']
})
export class HomeComponent implements OnInit {
    constructor(private companyService: CompanyService) {
    }

    companies: ICompany[];

    ngOnInit(): void {
        this.companyService.getCompanies()
            .subscribe(
                (companies: ICompany[]) => {
                    this.companies = companies;
            });
    }
}

Hope that helps...

UPD

Caching using .publishReplay(1).refCount();

penleychan
  • 5,370
  • 1
  • 17
  • 28
  • thank you for the answer. so I don't have to use any session or similar thing to cache the companies? even if it takes long to retrieve them? – Sara N Dec 20 '16 at 00:43
  • @SarahN you can cache your results using .publishReplay(1).refCount() on services class. I noticed you updated your question that you are using EF, how much data returned are we talking about here? Are you lazy loading (this can affect the performance)? What about adding AsNoTracking() since you are not modifying your data I assume (which should increase your performance). – penleychan Dec 20 '16 at 00:51
  • I use Lazy loading , and there are about 3000 companies , but I need to retrieve them in hierarchical. this is just a sample of the similar situations. do you mean I need to get it with eager loading? I didnt use AsNoTracking before , but I will if I know its useful – Sara N Dec 20 '16 at 01:37
  • @SarahN I always use eager loading in my application. However, 3000 records shouldn't be a performance hit. I am assuming this is because of deep hierarchy in your entities due to lazy loading. You need to ask yourself do you really need all those data to be retrieved? This question might help: http://stackoverflow.com/questions/19311491/how-entity-framework-works-for-large-number-of-records – penleychan Dec 20 '16 at 02:04
  • Thank you , I think it was helpful . – Sara N Dec 20 '16 at 02:18
  • @SarahN, here's something related to Entity framework article that will be helpful in terms of increasing your performance using EF. http://www.c-sharpcorner.com/uploadfile/ff2f08/tips-to-improve-entity-framework-performance/ – penleychan Dec 20 '16 at 02:24