Reputation: 33
My boss, recently gave me some C# .NET code he wants me to incorporate in an n-tiered MVC application I am building. He wants me to fill in the missing code. Here is a middle-tier class file (shown below)
public class UnitOfWork : IUnitOfWork
{
private readonly JRES_Context _context;
public UnitOfWork(JRES_Context context)
{
_context = context;
table1_UOW = new table1Repository(_context);
table2_UOW = new table2Repository(_context);
}
public Iinterface1 table1_UOW { get; private set; }
public Iinterface2 table2_UOW { get; private set; }
public int Complete()
{
return _context.SaveChanges();
}
public void Dispose()
{
_context.Dispose();
}
}
One things puzzles me: why are 2 previously-defined interfaces declared? Isn't the purpose of the interface to IMPLEMENT it (and thereby enforce behavior inside the object that implements it)? I wondered if there was another good purpose (the interfaces define some 'Get' methods used on the UI layer) - or maybe there is a better way to code this.
Upvotes: 1
Views: 107
Reputation: 397
Assuming you're referring to table1_UOW and table2_UOW, those are not interface declarations, but rather publicly accessible properties of UnitOfWork. Basically, they're just there to allow whatever uses UnitOfWork to access some functionality of table1_UOW and table2_UOW without actually exposing their implementation details (eg what class they actually are, how they actually achieve their functionality, etc), so that all that the consumer of UnitOfWork needs to know about is the interfaces IUnitOfWork, Iinterface1 and Iinterface2, and not the details of the classes that implement them. This kind of information hiding is important for making object oriented code modular and easy to work with.
Upvotes: 0
Reputation: 5506
table1_UOW
for example is a property with type Iinterface1
, you do not need to implement that in order to work with that interface in this class.
Presumably they are implemented elsewhere in the solution, those properties need setting to an instance that implements the interface otherwise there will be an exception at runtime when accessing members on them, these properties could be set manually in the calling code, or using an IoC container.
Upvotes: 4