首页 新闻 赞助 找找看

(C#)求抽象工厂模式下,如何把接口利用起来

0
悬赏园豆:20 [已解决问题] 解决于 2015-08-25 16:09

如果单纯是UI-->BLL-->DAL就不要告诉我,我要的是在BLL中用接口,去调用数据层DAL中的方法,中间是怎么实现的,如果只是在BLL层中引用数据层实例化数据层对象(类似

DAL dal = new DAL();

dal.get();   //get()数据层里的方法)

这个就不用告诉我

我要的是

用一个接口的对象,去访问数据层(数据层已实现继承接口)

如  IAmain(表示接口类) 其对象 iAmain

在BLL层 用 iAmain去调用数据层方法

也就是如何在抽象工厂下实现接口的问题

夜空下的男子的主页 夜空下的男子 | 初学一级 | 园豆:31
提问于:2015-08-25 15:06
< >
分享
最佳答案
0

(不知道是不是这样,自己写了个例子)

1:定义接口:

 public interface IService<TEntity>
    {
        int Save(TEntity entity);

        void SaveOrUpdate(TEntity entity);

        bool Exists(Expression<Func<TEntity, bool>> predicate);
  }

2:实现:

public class Service<TEntity> : IService<TEntity> where TEntity : EntityBase
    {
        protected IRepository<TEntity> Repository { get; set; }
        protected IUnitOfWork UnitOfWork { get; private set; }

        public Service(IRepository<TEntity> repository, IUnitOfWork unitOfWork)
        {
            this.Repository = repository;
            this.UnitOfWork = unitOfWork;
        }

        #region IService<TEntity> 成员

        public int Save(TEntity entity)
        {
            return (int) (Repository.Save(entity) ?? 0);
        }

        public void SaveOrUpdate(TEntity entity)
        {
            Repository.SaveOrUpdate(entity);
        }
       public bool Exists(Expression<Func<TEntity, bool>>predicate)
        {
            return Repository
                .Query(predicate)
                .Any();
        }
}

3:利用IOC注入(不写例子了),实例化:

 public class CatalogOrderController : Controller
    {
        private IService<Model.Order> orderService;
 

        public CatalogOrderController(IService<Model.Order> orderService)
        {
            this.orderService = orderService;
        }
}

4:直接调用(order为一个对象):

orderService.SaveOrUpdate(order);

收获园豆:20
稳稳的河 | 老鸟四级 |园豆:4216 | 2015-08-25 15:41

这是哪一种程序的代码?

夜空下的男子 | 园豆:31 (初学一级) | 2015-08-25 15:56

@夜空下的男子: c#,不是你要的C#的代码?

稳稳的河 | 园豆:4216 (老鸟四级) | 2015-08-25 15:58

@稳稳的河: 哦,这是你的编码方式

夜空下的男子 | 园豆:31 (初学一级) | 2015-08-25 15:59

@夜空下的男子: 这编码方式有什么问题?

稳稳的河 | 园豆:4216 (老鸟四级) | 2015-08-25 16:00

@稳稳的河: 没,我是新手有点不习惯,谢谢

夜空下的男子 | 园豆:31 (初学一级) | 2015-08-25 16:02

@夜空下的男子: 哦,TEntity我应该写成T,我写成TEntity习惯了,用TEntity代表泛型了

稳稳的河 | 园豆:4216 (老鸟四级) | 2015-08-25 16:08

@夜空下的男子: 楼下的你看明白快点,用了单例,也把基本写法写出来了,MyEntity对象也具体化了,不过推荐你试试去用泛型的编程思想写下

稳稳的河 | 园豆:4216 (老鸟四级) | 2015-08-25 16:13

@稳稳的河: @clarlespeng: 嗯,谢谢你们

夜空下的男子 | 园豆:31 (初学一级) | 2015-08-25 16:14
其他回答(1)
0

我写个比较简单点的

 public class MyEntity
    {
        public string id
        {
            get;
            set;
        }
    }
public interface IMyDAL
    {
        void Add(MyEntity ent);
        bool Update(MyEntity ent);
    }
public class MyDAL: IMyDAL
    {
       public void Add(MyEntity ent)
       {

       }
       public bool Update(MyEntity ent)
       {
           return true;
       }
    }
public class ProviderInstance
    {
        private static IMyDAL _instance;
        private static readonly object syncObject = new object();

        private ProviderInstance()
        {

        }
       
        public static IMyDAL GetInstance()
        {
            if (_instance == null)
            {
                lock (syncObject)
                {
                    if (_instance == null)
                    {
                        _instance = (IMyDAL)Activator.CreateInstance(Type.GetType(string.Format("{0}.MyDAL, {0}", "DAL")));
                    }
                }
            }
            return _instance;
        }         
    }
public class MyBLL
    {
        public void Add(MyEntity ent)
        {
            ProviderInstance.GetInstance().Add(ent);
        }
        public bool Update(MyEntity ent)
        {
            return ProviderInstance.GetInstance().Update(ent);
        }
    }
clarlespeng | 园豆:469 (菜鸟二级) | 2015-08-25 16:08

汗,我还以为是稳稳的河重新回复我,所以全结帖了,sorry

支持(0) 反对(0) 夜空下的男子 | 园豆:31 (初学一级) | 2015-08-25 16:11

@夜空下的男子: 没事

他的比较高级 我的比较初级

能帮助你就行 

支持(0) 反对(0) clarlespeng | 园豆:469 (菜鸟二级) | 2015-08-25 16:13

@clarlespeng: 嗯,谢谢你们

支持(0) 反对(0) 夜空下的男子 | 园豆:31 (初学一级) | 2015-08-25 16:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册