C# 当属性是不同的对象类型时,将代码合并到基类中
我有几个类都有非常相似的代码,我想知道合并通用代码是否有意义,以及如何进行。每个类都有一个特定的模型属性和一个特定的服务属性,但除此之外,它们基本相同 下面是我正在使用的代码示例:C# 当属性是不同的对象类型时,将代码合并到基类中,c#,inheritance,C#,Inheritance,我有几个类都有非常相似的代码,我想知道合并通用代码是否有意义,以及如何进行。每个类都有一个特定的模型属性和一个特定的服务属性,但除此之外,它们基本相同 下面是我正在使用的代码示例: public class Example1 { public Object1 Model { get; set; } private Service1 Service {get;set;} protected bool Create() { try
public class Example1 {
public Object1 Model { get; set; }
private Service1 Service {get;set;}
protected bool Create()
{
try
{
Model = Service.Create(Model);
return true;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
假设我有4个类,它们都具有相同的Create()
方法语法,但属性对象类型都不同。是否有一种方法可以将其转换为4个类,每个类都定义了它们的属性,然后在一个基类中使用Create()
方法
我希望它的外观如下所示:
public class Example1 : Base {
public Object1 Model { get; set; }
private Service1 Service { get; set; }
}
public class Example2 : Base {
public Object2 Model { get; set; }
private Service2 Service { get; set; }
}
public class Example3 : Base {
public Object3 Model { get; set; }
private Service3 Service { get; set; }
}
public class Example4 : Base {
public Object4 Model { get; set; }
private Service4 Service { get; set; }
}
public class Base {
// Wondering if this would do the trick; could be confusing though
// public object Model { get; set; }
// private object Service { get; set; }
//
protected bool Create()
{
try
{
Model = Service.Create(Model);
return true;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
但是我被困在如何在基类中提供属性的问题上
谢谢你的帮助。如果这是不可能的,那很好,我只是想让我的代码库少一点膨胀
编辑:
我认为可能可行的一件事是将模型和服务定义在基类中作为对象,但随后我担心,根据调用属性的位置,可能会出现混淆。您可以将泛型与抽象基类一起使用,以获得所需的位置:
public abstract class Base<TModel, TService> where TService: IService
{
public TModel Model { get; set; }
private TService Service { get; set; }
protected bool Create()
{
try
{
Model = Service.Create(Model);
return true;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
实现此抽象基类后,您的实现如下所示:
public interface IService
{
T Create(T input);
}
public class Example1 : Base<Object1, Service1>
{
//Code specific to this implementation
}
public class Example2 : Base<Object2, Service2>
{
//Code specific to this implementation
}
公共类示例1:Base
{
//特定于此实现的代码
}
公共类示例2:基本类
{
//特定于此实现的代码
}
有一件事你从来没有提到过,那就是你的基地将如何获得它的内容。您可能需要通过构造函数传递这些信息。我在这里没有提供,因为您也没有提供,但请记住,您至少需要将服务传递到基类中,以便它可以从传递的服务调用
Create()
。您可以将泛型与抽象基类一起使用,以达到您需要的目的:
public abstract class Base<TModel, TService> where TService: IService
{
public TModel Model { get; set; }
private TService Service { get; set; }
protected bool Create()
{
try
{
Model = Service.Create(Model);
return true;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
实现此抽象基类后,您的实现如下所示:
public interface IService
{
T Create(T input);
}
public class Example1 : Base<Object1, Service1>
{
//Code specific to this implementation
}
public class Example2 : Base<Object2, Service2>
{
//Code specific to this implementation
}
公共类示例1:Base
{
//特定于此实现的代码
}
公共类示例2:基本类
{
//特定于此实现的代码
}
有一件事你从来没有提到过,那就是你的基地将如何获得它的内容。您可能需要通过构造函数传递这些信息。我在这里没有提供,因为您也没有,但请记住,您至少需要将服务传递到基础中,以便它可以从传递的服务调用
Create()
。如果模型和对象是同一个类,您的示例将起作用。如果Object1和Object2是不同的类,那么可能需要进行一些调整,或者您试图实现的目标可能不可能实现
如果它们是同一个类,则可以将对象变量设置为受保护的,并在每个派生类的构造函数中以不同的方式声明它
如果它们不是同一个类,而是相似的,则Object1、Object2等类都可以从一个对象类继承,并且上述操作可以相同完成,但是如果Object1和Object2完全不同,这将没有用处
如果我明白你想做什么,也许这会有帮助:
public class Base
{
public Object model;
protected Service service;
protected bool Create()
{
try
{
Model = Service.Create(Model);
return true;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
public class Example1 : Base
{
public Example1() //Assign specifics in constructor
{
model = model1;
service = service1;
}
}
public class Example2 : Base
{
public Example2() //Assign specifics in constructor
{
model = model2;
service = service2;
}
}
如果这没有帮助,那么可能您正在寻找的是抽象类/方法,但我不能确定。如果模型和对象是同一个类,那么您的示例将起作用。如果Object1和Object2是不同的类,那么可能需要进行一些调整,或者您试图实现的目标可能不可能实现
interface IModel { }
interface IService<out TModel>
{
TModel Create(IModel model);
}
interface IModelService<TModel, TService> where TModel : IModel where TService : IService<TModel>
{
TModel Model { get; set; }
TService Service { get; set; }
}
class ExampleModel : IModel { }
class ExampleService : IService<ExampleModel>
{
public ExampleModel Create(TModel model)
{
return new ExampleModel();
}
}
class Example : ExampleBase<ExampleModel, ExampleService>
{
}
abstract class ExampleBase<TModel, TService> : IModelService<TModel, TService> where TModel : IModel where TService : IService<TModel>
{
public TModel Model { get; set; }
public TService Service { get; set; }
protected bool Create()
{
//do what you must
}
}
如果它们是同一个类,则可以将对象变量设置为受保护的,并在每个派生类的构造函数中以不同的方式声明它
如果它们不是同一个类,而是相似的,则Object1、Object2等类都可以从一个对象类继承,并且上述操作可以相同完成,但是如果Object1和Object2完全不同,这将没有用处
如果我明白你想做什么,也许这会有帮助:
public class Base
{
public Object model;
protected Service service;
protected bool Create()
{
try
{
Model = Service.Create(Model);
return true;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
public class Example1 : Base
{
public Example1() //Assign specifics in constructor
{
model = model1;
service = service1;
}
}
public class Example2 : Base
{
public Example2() //Assign specifics in constructor
{
model = model2;
service = service2;
}
}
如果这没有帮助,那么您可能正在寻找抽象类/方法,但我不能确定。interface IModel{}
interface IModel { }
interface IService<out TModel>
{
TModel Create(IModel model);
}
interface IModelService<TModel, TService> where TModel : IModel where TService : IService<TModel>
{
TModel Model { get; set; }
TService Service { get; set; }
}
class ExampleModel : IModel { }
class ExampleService : IService<ExampleModel>
{
public ExampleModel Create(TModel model)
{
return new ExampleModel();
}
}
class Example : ExampleBase<ExampleModel, ExampleService>
{
}
abstract class ExampleBase<TModel, TService> : IModelService<TModel, TService> where TModel : IModel where TService : IService<TModel>
{
public TModel Model { get; set; }
public TService Service { get; set; }
protected bool Create()
{
//do what you must
}
}
接口设备
{
t模型创建(IModel模型);
}
接口IModelService,其中TModel:IModel,其中TService:iSeries
{
t模型{get;set;}
t服务服务{get;set;}
}
类ExampleModel:IModel{}
类示例服务:iSeries设备
{
公共示例模型创建(TModel模型)
{
返回新的ExampleModel();
}
}
类示例:ExampleBase
{
}
抽象类ExampleBase:IModelService where TModel:IModel where TService:iSeries
{
公共TModel模型{get;set;}
公共TService服务{get;set;}
受保护的bool创建()
{
//做你必须做的事
}
}
抽象类,具有泛型类型参数和少量用于定义和约束接口IModel{}
接口设备
{
t模型创建(IModel模型);
}
接口IModelService,其中TModel:IModel,其中TService:iSeries
{
t模型{get;set;}
t服务服务{get;set;}
}
类ExampleModel:IModel{}
类示例服务:iSeries设备
{
公共示例模型创建(TModel模型)
{
返回新的ExampleModel();
}
}
类示例:ExampleBase
{
}
抽象类ExampleBase:IModelService where TModel:IModel where TService:iSeries
{
公共TModel模型{get;set;}
公共TService服务{get;set;}
受保护的bool创建()
{
//做你必须做的事
}
}
抽象类,具有泛型类型参数和少量接口,用于定义和约束另一种解决问题的方法,因为每个子类都有自己的服务对象
将IService
接口设置为Service
类合同。
有一个合同TModel-Create(TModel-model)代码>在IService
中
public interface IService {
TModel Create<TModel>(TModel model);
}
你的子类可以