user137348
user137348

Reputation: 10332

Handling the context in Entity-framework

I have a class that provide the CRUD operation for an entity.Im using the context as private member accessible for all methods in the class.

  public class CustomerService
  { 
    private CeoPolandEntities context;

    public CustomerService()
    {
        context = new CeoPolandEntities();
    }


    public bool IsCustomerValid(string userName,string password)
    {
        Customer customer;

        customer = context.CustomerSet.FirstOrDefault(c => c.UserName == userName
                                                    && c.Password == password);

        return customer == null ? false : true;
    }

    public bool IsUserNameValid(string userName)
    {
        Customer customer;

        customer = context.CustomerSet.FirstOrDefault(c => c.UserName == userName);

        return customer == null ? true : false;
    }
}

Is this a proper using of context ?? Is it thread safe and concurency safe??

Its a ASP.NET app.

Upvotes: 1

Views: 409

Answers (2)

Craig Stuntz
Craig Stuntz

Reputation: 126547

Contexts aren't thread-safe.

Your current code is fine if:

  1. You change CustomerService to Dispose the Context.
  2. You use one CustomerService per request.

Upvotes: 1

eglasius
eglasius

Reputation: 36027

As long as u have different instances of CustomerService to handle the different requests, u don't need to concern about that. If you happen to have any threads u create yourself, avoid calling multiple methods in the same instance.

Upvotes: 1

Related Questions