Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.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语言中使用GetById函数#_C# - Fatal编程技术网

C# 如何在c语言中使用GetById函数#

C# 如何在c语言中使用GetById函数#,c#,C#,我在学c。我试图在模拟存储库中创建一个基本列表(而不是直接从SQL中提取数据)。我有一个包含汽车的列表,其中包含Id、型号、品牌、描述等字段 然后,我尝试在我的Car.CS类中创建一个方法,该方法通过Id获取汽车。例如,如果我传入汽车的Id,它将返回汽车的其他详细信息 不幸的是,当我运行控制台应用程序时,它只是在控制台中返回空白?有人能告诉我哪里出了问题吗 首先,我有一个假设 namespace GeneralConsoleApp { interface ICarRepository

我在学c。我试图在模拟存储库中创建一个基本列表(而不是直接从SQL中提取数据)。我有一个包含汽车的列表,其中包含Id、型号、品牌、描述等字段

然后,我尝试在我的Car.CS类中创建一个方法,该方法通过Id获取汽车。例如,如果我传入汽车的Id,它将返回汽车的其他详细信息

不幸的是,当我运行控制台应用程序时,它只是在控制台中返回空白?有人能告诉我哪里出了问题吗

首先,我有一个假设

namespace GeneralConsoleApp
{
    interface ICarRepository
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
    }
}
然后我有我的MockCarRepository,其中包含了列表

namespace GeneralConsoleApp
{
    class MockCarRepository : ICarRepository
    {
        public IEnumerable<Car> Cars =>
            new List<Car>
            {
                new Car {Id = 1, Name="BMW", Price=23000.00M, Description="BMW car" }
            };

        public int Id { get; set; }
        public string Name {get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
    }
}

您的问题是您的Car类有一个Car类型列表,您将从中向控制台返回一个值

您应该从存储库返回一个值

nammespace GeneralConsoleApp
{
    class MockCarRepository : ICarRepository
    {
           public IEnumerable<Car> Cars => new List<Car>
           {
                   new Car {Id = 1, Name="BMW", Price=23000.00M, Description="BMW car" }
           };

           public Car GetCarById(int Id)
           {
             return Cars.FirstOrDefault(z => z.Id == Id);
           }
    }
    class Program
    {
        static void Main()
        {
            var Repository = new MockRepository();


            Console.WriteLine(Repository.GetCarById(1));

        }
    }
}
nammespace通用解决方案
{
类别MockCarRepository:ICarRepository
{
公共IEnumerable汽车=>新列表
{
新车{Id=1,Name=“宝马”,Price=23000.00M,Description=“宝马汽车”}
};
公共汽车GetCarById(内部Id)
{
返回Cars.FirstOrDefault(z=>z.Id==Id);
}
}
班级计划
{
静态void Main()
{
var Repository=new MockRepository();
Console.WriteLine(Repository.GetCarById(1));
}
}
}

您的
汽车
类不使用
MockCarRepository
,而是有自己的空列表(
CarList


您应该在
Car
中填充列表,或者在那里使用模拟存储库

CarList是一个空列表,这就是没有输出的原因

将主呼叫更改为

MockCarRepository car = new MockCarRepository();
Console.WriteLine(car.GetCarById(1).Name);

你会看到区别

你应该这样做

ICarRepository repository= new MockCarRepository();
CarService carService= new CarService(repository);
Console.WriteLine(carService.GetCarById(1));
您需要添加CarService类,该类可以注入模拟存储库或实际数据库存储库,并将Car类修改为模型

    class CarService
    {
        private readonly ICarRepository _repository;
        public CarService(ICarRepository repository)
        {
           _repository=repository;
        }

        public Car GetCarById(int Id)
        {
            return _repositoy.GetCarById(id);    
        }
    }

    class Car
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public string Description { get; set; }       
    }
    interface ICarRepository
    {
        Car GetCarById(int Id);
    }
    class MockCarRepository : ICarRepository
    {
        public IEnumerable<Car> Cars =>
            new List<Car>
            {
                new Car {Id = 1, Name="BMW", Price=23000.00M, Description="BMW car" }
            };
        public Car GetCarById(int Id)
        {
            return Cars.First(z => z.Id == Id);    
        }
    }
class服务
{
私有只读iCarrecository存储库;
公共汽车服务(ICarRepository存储库)
{
_存储库=存储库;
}
公共汽车GetCarById(内部Id)
{
返回_repositoy.GetCarById(id);
}
}
班车
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共十进制价格{get;set;}
公共字符串说明{get;set;}
}
界面电沉积
{
Car GetCarById(内部Id);
}
类别MockCarRepository:ICarRepository
{
公共IEnumerable汽车=>
新名单
{
新车{Id=1,Name=“宝马”,Price=23000.00M,Description=“宝马汽车”}
};
公共汽车GetCarById(内部Id)
{
返回车辆。首先(z=>z.Id==Id);
}
}

这里有几个关于关注点分离的问题。您的汽车模型只需要对与汽车关联的属性进行建模

同样,CarRepository应该只包含对数据存储执行查询所需的方法,如

GetCarById(int id)

IEnumerable FindByManufacturer(字符串制造商);
在模拟数据存储方面,您将创建一个专门用于模拟的类,如MockCarRepository,然后在运行时可以实例化它。这是非常有用的原因,因为您最终是针对接口ICarRepository编程的,当您准备好使用真实数据时,您所需要做的就是创建ICarRepository的新实现,使用您想要的任何存储、本机SQL连接、EntityFramework、,CosmosDB等,只需更改实例化类型的位置,即可满足ICarRepository

我建议你研究一下坚实的设计原则——下面是很棒的资源,它比我解释的要好得多

通过一些小的调整,你可以使用下面这样的东西

namespace GeneralConsoleApp
{
interface ICarRepository
{
    Car GetCarById(int id);
}

class MockCarRepository : ICarRepository
{
    private List<Car> _mockCars;

    public MockCarRepository()
    {
        _mockCars = new List<Car>
        {
            new Car {Id = 1, Name="BMW", Price=23000.00M, Description="BMW car" }
        };
    }

    public Car GetCarById(int id)
    {
        return _mockCars.FirstOrDefault(v => v.Id == id);
    }
}

class SqlCarRepository : ICarRepository
{
    private readonly string _connectionString;

    public SqlCarRepository(string connectionString)
    {
        _connectionString = connectionString;
    }

    public Car GetCarById(int id)
    {
        // use the _connectionString to create your database connection.
        var car = FROM DATABASE;
        return car;
    }
}

class Car
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        // All repositories should implement ICarRepository
        // meaning switching repo implementation is a one line change below, a DI container adjustment, or some sort of strategy pattern.
        ICarRepository mockRepo = new MockCarRepository();;

        var carFound = mockRepo.GetCarById(1);

        Console.WriteLine("Found car: " + carFound.Description);

        Console.ReadLine();

    }
}
名称空间通用解决方案
{
界面电沉积
{
Car GetCarById(内部id);
}
类别MockCarRepository:ICarRepository
{
私家车名单;
公共图书馆
{
_模拟车=新列表
{
新车{Id=1,Name=“宝马”,Price=23000.00M,Description=“宝马汽车”}
};
}
公共汽车GetCarById(内部id)
{
return _mockCars.FirstOrDefault(v=>v.Id==Id);
}
}
类SqlCarRepository:ICarRepository
{
私有只读字符串_connectionString;
公共SqlCarRepository(字符串连接字符串)
{
_connectionString=connectionString;
}
公共汽车GetCarById(内部id)
{
//使用_connectionString创建数据库连接。
var car=来自数据库;
返回车;
}
}
班车
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共十进制价格{get;set;}
公共字符串说明{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
//所有存储库都应实现ICarRepository
//这意味着转换回购协议的实施是以下的一行变更、DI容器调整或某种策略模式。
ICarRepository mockRepo=新MockCarRepository();;
var carFound=mockRepo.GetCarById(1);
控制台.WriteLine(“找到的车:+carFound.Description”);
Console.ReadLine();
}
}

}

为避免混淆,您可能应该将
CarList
汽车中删除。谢谢菲尔。如果它不是模拟数据,并且实际上是从SQL数据库中提取数据,那么Car.cs是否会包含检索数据的方法,而不是MockRepo中的方法?@Matty21202您还需要一个以IRepository作为输入的层。见下面我的答案。菲尔,希望你也同意。@DeepakMishra谢谢
GetCarById(int id)
IEnumerable<Car> FindByManufacturer(string manufacturer);
namespace GeneralConsoleApp
{
interface ICarRepository
{
    Car GetCarById(int id);
}

class MockCarRepository : ICarRepository
{
    private List<Car> _mockCars;

    public MockCarRepository()
    {
        _mockCars = new List<Car>
        {
            new Car {Id = 1, Name="BMW", Price=23000.00M, Description="BMW car" }
        };
    }

    public Car GetCarById(int id)
    {
        return _mockCars.FirstOrDefault(v => v.Id == id);
    }
}

class SqlCarRepository : ICarRepository
{
    private readonly string _connectionString;

    public SqlCarRepository(string connectionString)
    {
        _connectionString = connectionString;
    }

    public Car GetCarById(int id)
    {
        // use the _connectionString to create your database connection.
        var car = FROM DATABASE;
        return car;
    }
}

class Car
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        // All repositories should implement ICarRepository
        // meaning switching repo implementation is a one line change below, a DI container adjustment, or some sort of strategy pattern.
        ICarRepository mockRepo = new MockCarRepository();;

        var carFound = mockRepo.GetCarById(1);

        Console.WriteLine("Found car: " + carFound.Description);

        Console.ReadLine();

    }
}