0

I created two edmx files and have to contexts.

Is there a problem with doing something like:

public DataManager
{
  protected ObjectContext _context;

  public DataManager(ObjectContext context)
  {
     _context = context;
  }
}

or is it better to have an overloaded construtor:

public DataManager
{
  protected db1entities _context;
  protected db2entities _context2;

  public DataManager(db2entities context)
  {
     _context = context;
  }

  public DataManager(db2entities context)
  {
     _context2 = context;
  }
}

I noticed if I do it the first way, then the context does not know about my entities, where as it does if I explicitly specify the context

1 Answer 1

2

I'm not entirely sure what you are trying to accomplish, but you could also do this using generics (http://msdn.microsoft.com/en-us/library/512aeb7t(v=vs.110).aspx). Something like...

public class DataManager<T> where T:ObjectContext
{
  protected T _context;

  public DataManager(T context)
  {
     _context = context;
  }
}

Then ...

DataManager<db1Entities> DataManager1;
DataManager<db2Entities> DataManager2;
Sign up to request clarification or add additional context in comments.

5 Comments

I believe this is it. I will test it out, but basically I am just trying to pass a different context depending on what data I want to work with. Would you do me a favor and explain what T means?
This is a generic class. T stands for the type being used. In this case it specifies that T needs to be of type ObjectContext or classes that inherit from it.
Like bcr said, T is sort of a placeholder for the type you specify when creating the class. The expression T:ObjectContext just limits the types you can use with this class to those of type ObjectContext of a child of that class. Basically, it just stops you from using a completely inappropriate datatype with this class.
ok, so if I removed the Where, T Could be anything, so it is like a constraint.
Yes. With out it, you could say DataManager<TextBox> DataManager1;. That obviously makes no sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.