怀疑(接口c#) [英] Doubts (Interface c#)

查看:65
本文介绍了怀疑(接口c#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我有这样的问题:



我有这个界面

公共接口IProductRepository:IDisposable 
{
List< Product>得到();
Product Get(int id);
void Create(产品);
void Update(产品);
void删除(int id);
}

实现接口的类

公共类ProductRepository:IProductRepository 
{
private AppDataContext _db;

public ProductRepository()
{
_db = new AppDataContext();
}
public void创建(商品)
{
_db.Products.Add(product);
_db.SaveChanges();
}

public void删除(int id)
{
_db.Products.Remove(Get(id));
_db.SaveChanges();
}

public List< Product> Get()
{
return _db.Products.ToList();
}

public Product Get(int id)
{
return _db.Products.Find(id);
}

public List< Product>获取(字符串名称)
{
返回_db.Products.Where(x => x.Name.StartsWith(name))。ToList();
}

public void更新(产品)
{
_db.Entry< Product>(product).State = System.Data.Entity.EntityState.Modified ;
_db.SaveChanges();
}

public void Dispose()
{
_db.Dispose();
}
}

注意Get(字符串名称)方法,它不在界面中,当我想使用它时,我不能。我将告诉你这次尝试。

 private IProductRepository _repository = new ProductRepository(); 

public HttpResponseMessage GetProducts()
{
var response = new HttpResponseMessage();

try
{
//错误:'name'在当前上下文中不存在
var result = _repository.Get(name);

response = Request.CreateResponse(HttpStatusCode.OK,result);
}
catch
{
response = Request.CreateResponse(HttpStatusCode.BadRequest," Ops,nãofoipossivel listar os produtos");
}

返回回复;
}




想法不是将此方法插入接口,因为并非每个使用此存储库的人都会使用此方法方法。你能帮助我吗?



解决方案

Hello Renato,


如果您想要访问对象上存在但不在当前强制转换中的方法,那么您需要重新制作它。 例如((ProductRepository)_repository).Get(name)。


如果存储库的用户知道它将处理ProductRepository,那么它可能不应该将ProductRepository转换为IProductRepository &NBSP;&NBSP;


Guys, I have a question like this:

I have this interface

public interface IProductRepository : IDisposable
{
	List<Product> Get();
	Product Get(int id);
	void Create(Product product);
	void Update(Product product);
	void Delete(int id);
}

The class that implements the interface

public class ProductRepository : IProductRepository
    {
        private AppDataContext _db;

        public ProductRepository()
        {
            _db = new AppDataContext();
        }
        public void Create(Product product)
        {
            _db.Products.Add(product);
            _db.SaveChanges();
        }

        public void Delete(int id)
        {
            _db.Products.Remove(Get(id));
            _db.SaveChanges();
        }

        public List<Product> Get()
        {
            return _db.Products.ToList();
        }

        public Product Get(int id)
        {
            return _db.Products.Find(id);
        }

        public List<Product> Get(string name)
        {
            return _db.Products.Where(x => x.Name.StartsWith(name)).ToList();
        }

        public void Update(Product product)
        {
            _db.Entry<Product>(product).State = System.Data.Entity.EntityState.Modified;
            _db.SaveChanges();
        }

        public void Dispose()
        {
            _db.Dispose();
        }
    }

pay attention to the Get (string name) method, it is not in the interface, when I want to use it, I can not. I'll show you the attempt.

private IProductRepository _repository = new ProductRepository();

public HttpResponseMessage GetProducts()
        {
            var response = new HttpResponseMessage();

            try
            {
                // ERROR: THE 'name' does not exists in the current context 
                var result = _repository.Get(name);

                response = Request.CreateResponse(HttpStatusCode.OK, result);
            }
            catch
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest, "Ops, não foi possivel listar os produtos");
            }

            return response;
        }


The idea is not to insert this method into the interface, because not everyone who consumes this repository will use this method. Can you help me?

解决方案

Hello Renato,

If you want to access a method that exists on an object but not in the current cast then you need to recast it.  For example ((ProductRepository)_repository).Get(name).

If a user of the repository knows it will be dealing with a ProductRepository then it probably should not cast the ProductRepository to a IProductRepository.  


这篇关于怀疑(接口c#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆