Question

Here is the my simple senario. I have a class(BLL class) which implemented an interface. What I wanna do is, inside the presantation layer, i want users reach only the interface and interacting the class with this interface, not the class function directly. Is there anyway to do that?

My BLL class implemeted interface :

    public interface IOfis
    {

        bool Add(Ofis ofs);
        bool Update(Ofis ofs);


    }

    public class BLL_Ofis:IOfis
    {
        public bool Update(Ofis ofs)
        {
            DAL_Ofis ofs_dal = new DAL_Ofis();
            try
            {
                return ofs_dal.Update(ofs);
            }
            catch (Exception)
            {

                throw;
            }
            finally { ofs_dal = null; }
        }

      public bool Add(Ofis ofs_obj)
        {
            DAL_Ofis ofs_dal = new DAL_Ofis();
            try
            {
                return ofs_dal.Add(ofs_obj);
            }
            catch (Exception)
            {

                throw;
            }
            finally { ofs_dal = null; }
        }
}

Inside the presantatoin layer, i use it like this :

IOfis bll_ofis = new BLL_Ofis();
bll_ofis.Update();

But in this situation I can also reach the class directly like this :

 BLL_Ofis bll_ofis = new BLL_Ofis();
 bll_ofis.Update();

which I dont want that. I wanna reach only method which declared inside the Interface.

Was it helpful?

Solution

I don't see why you would want it, but a "solution" could be to change the public methods into explicit interface implementations. To do that, remove access modifiers (here public) and prepend IOfis. to the name of the method. Like this:

public class BLL_Ofis : IOfis
{
    bool IOfis.Update(Ofis ofs)
    {
        ...
    }

    ...
}

An explicit interface implementation can only be invkoked when the compile-time type of the variable is the interface type.

OTHER TIPS

You must implement the method Add in your class as well. As for visibility, remove public from the class declaration, that will make it visible only for code that resides in the same assembly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top