Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类问题的接口实现_C#_Asp.net_Interface_Data Access Layer_Business Logic Layer - Fatal编程技术网

C# 类问题的接口实现

C# 类问题的接口实现,c#,asp.net,interface,data-access-layer,business-logic-layer,C#,Asp.net,Interface,Data Access Layer,Business Logic Layer,这是我的简单的塞纳里奥。 我有一个类(BLL类),它实现了一个接口。我想做的是,在预植入层中,我希望用户只访问接口并与该接口交互类,而不是直接与类函数交互。有什么办法吗 我的BLL类实现的接口: public interface IOfis { bool Add(Ofis ofs); bool Update(Ofis ofs); } public class BLL_Ofis:IOfis { publ

这是我的简单的塞纳里奥。 我有一个类(BLL类),它实现了一个接口。我想做的是,在预植入层中,我希望用户只访问接口并与该接口交互类,而不是直接与类函数交互。有什么办法吗

我的BLL类实现的接口:

    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; }
        }
}
在presantatoin层中,我使用它如下:

IOfis bll_ofis = new BLL_Ofis();
bll_ofis.Update();
 BLL_Ofis bll_ofis = new BLL_Ofis();
 bll_ofis.Update();
但在这种情况下,我也可以像这样直接到达课堂:

IOfis bll_ofis = new BLL_Ofis();
bll_ofis.Update();
 BLL_Ofis bll_ofis = new BLL_Ofis();
 bll_ofis.Update();
我不想那样。
我只想找到在接口中声明的方法。

您还必须在类中实现方法
Add
。至于可见性,请从类声明中删除
public
,这将使它仅对驻留在同一程序集中的代码可见。

我不明白为什么需要它,但“解决方案”可能是将
public
方法更改为显式接口实现。为此,请删除访问修饰符(此处为
public
),并在方法名称前添加
IOfis.
。像这样:

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

    ...
}

只有当变量的编译时类型为接口类型时,才能对其进行invkoked。

但它们位于不同的dll中,这意味着不同的程序集。我指的是不同的类库预植入层和BLL。在这种情况下,只能通过公共接口访问这些方法,我认为,是所需的功能