-2

I have two class library named HTS.lib.Common and HTS.lib.Common.Contract

In HTS.lib.Common.Contract i have ModelView class and Interfaces

In HTS.lib.Common i have public classes which inherit some interfaces from HTS.lib.Common.Contract and have definition for it and all are of public type.

I have a MVC application which have reference of above two projects i can able to access only HTS.lib.Common.Contract members and not HTS.lib.Common

I am wondering how can it can be happen, in my controller class i can able to create instance through DependencyResolver of HTS.lib.Common.Contract. But when i try to access HTS.lib.Common members not at all :(

Can any one know how we can configure so that derived class can`t be accessible but base interface and class able to access.

Cœur
  • 37,241
  • 25
  • 195
  • 267
whoami
  • 83
  • 1
  • 2
  • 7
  • No error, just i am trying to understand the logic behind it, why i can't able to access even though i have reference of 'HTS.lib.Common' – whoami Sep 25 '15 at 01:14
  • If you have an actual question, then ask it. Otherwise you will be down voted and closed. – JK. Sep 25 '15 at 01:17
  • @whoami - no error? How do you know that you are "able to access ... not 'HTS.lib.Common'"? – Igor Sep 25 '15 at 01:21
  • @JK, I hope through this form we can able to gain, or develop knowledge. With out understanding my nature of question i am wondering your way of response. – whoami Sep 25 '15 at 01:42
  • @lgor21: As i said in my initial post i can able to access my 'HTS.lib.Common' through 'HTS.lib.Common.Contract'. I am creating instance for 'HTS.lib.Common.Contract' with help of 'DependencyResolver', so that i can able to access ''HTS.lib.Common' class and member. But i can't able to access my classes in 'HTS.lib.Common' directly. – whoami Sep 25 '15 at 01:48
  • @whoami - How are you accessing classes "in 'HTS.lib.Common' directly" and how do you know that you cannot? – Igor Sep 25 '15 at 01:57
  • I tried creating new instance for the class 'MyAccount' using 'HTS.lib.Common' but i can't able to access. The When i put dot after 'HTS.lib.Common' only i can able to see 'Contract' namespace in the popup and no other class shown which under 'HTS.lib.Common'. – whoami Sep 25 '15 at 02:17
  • can u show some code to put more light on the question – Mohit S Sep 25 '15 at 02:21

1 Answers1

1

You've probably implemented the interface in your common library explicitly, that'll mean all your methods are private and can only be accessed via the interface. As an example, given this interface:

public interface IUser
{
    String Username { get; }
}

You can implement that interface in two ways, firstly explicitly:

public class User : IUser
{
    String IUser.Username
    {
        get { return "Bob"; }
    }
}

In the above example you can only access the Username property via the interface, like this:

IUser user = new User();
var name = user.Username;

If you create the class directly you won't be able to access that property:

User user = new User();
var name = user.Username; //won't compile

If you want to be able to access the property in both ways you simply implement the interface "implicitly":

public class User : IUser
{
    public String Username
    {
        get { return "Bob"; }
    }
}

Now both of the forms of access shown above will work.

As to whether you should implement interfaces explicitly or implicitly, it depends, there are other questions on here that discuss that if you're interested.

Matt
  • 12,569
  • 4
  • 44
  • 42
  • Check out this question for a discussion of explicit vs implicit: http://stackoverflow.com/questions/143405/c-sharp-interfaces-implicit-implementation-versus-explicit-implementation – Matt Sep 26 '15 at 19:24