1

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.

4
  • 1
    Read up on preferring composition to inheritance. Commented Jan 13, 2017 at 21:17
  • 2
    Why would this class need to implement the interfaces? table1Repository and table2Repository already implement them (otherwise you couldn't assign instances of those classes into your properties). Commented Jan 13, 2017 at 21:19
  • 1
    What would be the point in implementing interfaces if you never actually used the interface for anything? The point of implementing an interface is so that other code can use those interfaces for things. Commented Jan 13, 2017 at 21:26
  • 1
    I'm not sure what you are referring to. If you are talking about table1_UOW and table2_UOW, those aren't interface declarations, they are properties of type Iinterface1 and Iinterface2, respectively. Commented Jan 13, 2017 at 21:28

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It should be mentioned that those are horrible names for interfaces.
I'll take that as the answer. (There were several good responses.) An actual declaration of the table1Repository/table2Repository tables is apparently missing at the UI layer. [there are numerous loose ends to the code, I just wanted to clarify the table one.] Thanks, all!
Yes, that needs mentioning =) I presume they are a work in progress
Those aren't the real names. There's some mixing and usage of UnitOfWork and Repository patterns that I'm skeptical about, but I'll make do.
Are you skeptical because it seems like an over abstraction? It seems like an "enterprisey" thing to do these days and I tend not to abstract over ORMs these days, but there is still a place for it. See here for a look at this pattern with .NET and EF.
0

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.

Comments

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.