C# 用C语言使用带有依赖注入的泛型类#

C# 用C语言使用带有依赖注入的泛型类#,c#,generics,dependency-injection,asp.net-core,C#,Generics,Dependency Injection,Asp.net Core,我需要为我的服务创建一个commons方法,并根据作为参数传递的对象类型使用存储库或其他方法 例如,我有以下接口: public interface IServiceText { Task<string> UpdateTextPosition(TextDto object); Task<string> UpdateTextSize(TextDto object); Task<string> UpdateOtherThingOnText(Tex

我需要为我的服务创建一个commons方法,并根据作为参数传递的对象类型使用存储库或其他方法

例如,我有以下接口:

public interface IServiceText
{
   Task<string> UpdateTextPosition(TextDto object);
   Task<string> UpdateTextSize(TextDto object);
   Task<string> UpdateOtherThingOnText(TextDto object);

}

public interface IServiceImage
{
   Task<string> UpdateImagePosition(ImageDto object);
   Task<string> UpdateImageSize(ImageDto object);
   Task<string> UpdateOtherThingOnImage(ImageDto object);

}
公共接口IServiceText
{
任务UpdateTextPosition(TextDto对象);
任务UpdateTextSize(TextDto对象);
任务更新otherthingontext(TextDto对象);
}
公共接口IServiceImage
{
任务更新图像位置(ImageDto对象);
任务更新图像大小(ImageDto对象);
任务更新其他thingonimage(ImageDto对象);
}
在顺序方面,我有实现接口(只是添加了重要代码)和构造函数的类:

//Class ServiceImage
private readonly IRepository<Image> _repo;

public ServiceImage(IRepository<Image> repo) //Dependecy Injection of the IRepository Generic
{
    _repo = repo;
}

//Class ServiceText
private readonly IRepository<Text> _repo;

public ServiceText(IRepository<Text> repo) //Dependecy Injection of the IRepository Generic
{
    _repo = repo;
}
//类ServiceImage
私人只读电子回购;
公共服务映像(IRepository repo)//IRepository类的依赖性注入
{
_回购=回购;
}
//类ServiceText
私人只读电子回购;
public ServiceText(IRepository repo)//IRepository类的依赖性注入
{
_回购=回购;
}
UpdateTextPosition和UpdateImagePosition的作用完全相同,但在不同的集合中(或者使用不同的IRepository实例,我使用的是MongoDB):

public异步任务UpdateTextPosition(TextDto对象)
{
var text=wait _repo.FindAsync(object.Id);
text.PosLeft=object.PosLef;
text.PosRight=object.PosRight;
等待回购更新(文本);
返回text.Id;
}
公共异步任务UpdateImagePosition(ImageDto对象)
{
var image=wait _repo.FindAsync(object.Id);
image.PosLeft=object.PosLef;
image.PosRight=object.PosRight;
等待回购更新(图片);
返回image.Id
}
编辑:(添加了ImageDto和TextDto)

公共类ImageDto
{
公共对象Id{get;set;}
公共int PosLeft{get;set;}
公共int PosTop{get;set;}
公共ICollection PropertyImage1{get;set;}
公共字符串PropertyImage2{get;set;}
}
公共类textdo
{
公共对象Id{get;set;}
公共int PosLeft{get;set;}
公共int PosTop{get;set;}
公共双属性ytext1{get;set;}
公共字符串PropertyText2{get;set;}
}
公共类映像:实体
{
公共对象Id{get;set;}
公共int PosLeft{get;set;}
公共int PosTop{get;set;}
公共ICollection PropertyImage1{get;set;}
公共字符串PropertyImage2{get;set;}
公共字符串OtherPropertyImage{get;set;}
}
公共类文本:实体
{
公共对象Id{get;set;}
公共int PosLeft{get;set;}
公共int PosTop{get;set;}
公共双属性ytext1{get;set;}
公共字符串PropertyText2{get;set;}
公共字符串OtherPropertyText{get;set;}
}
和客户:

/// <summary>
/// Generic Entity interface.
/// </summary>
/// <typeparam name="TKey">The type used for the entity's Id.</typeparam>
public interface IEntity<TKey>
{
    /// <summary>
    /// Gets or sets the Id of the Entity.
    /// </summary>
    /// <value>Id of the Entity.</value>
    [BsonId]
    TKey Id { get; set; }
}

/// <summary>
/// "Default" Entity interface.
/// </summary>
/// <remarks>Entities are assumed to use strings for Id's.</remarks>
public interface IEntity : IEntity<string>
{
}
/// <summary>
    /// Abstract Entity for all the BusinessEntities.
    /// </summary>
    //[DataContract]
    [Serializable]
    [BsonIgnoreExtraElements(Inherited = true)]
    public abstract class Entity : IEntity<string>
    {
        /// <summary>
        /// Gets or sets the id for this object (the primary record for an entity).
        /// </summary>
        /// <value>The id for this object (the primary record for an entity).</value>
        [DataMember]
        [BsonRepresentation(BsonType.ObjectId)]
        public virtual string Id { get; set; }
    }
//
///通用实体接口。
/// 
///用于实体Id的类型。
公共界面的开放性
{
/// 
///获取或设置实体的Id。
/// 
///实体的Id。
[BsonId]
TKey Id{get;set;}
}
/// 
///“默认”实体接口。
/// 
///假定实体对Id使用字符串。
公共接口的通用性:通用性
{
}
和实体类:

/// <summary>
/// Generic Entity interface.
/// </summary>
/// <typeparam name="TKey">The type used for the entity's Id.</typeparam>
public interface IEntity<TKey>
{
    /// <summary>
    /// Gets or sets the Id of the Entity.
    /// </summary>
    /// <value>Id of the Entity.</value>
    [BsonId]
    TKey Id { get; set; }
}

/// <summary>
/// "Default" Entity interface.
/// </summary>
/// <remarks>Entities are assumed to use strings for Id's.</remarks>
public interface IEntity : IEntity<string>
{
}
/// <summary>
    /// Abstract Entity for all the BusinessEntities.
    /// </summary>
    //[DataContract]
    [Serializable]
    [BsonIgnoreExtraElements(Inherited = true)]
    public abstract class Entity : IEntity<string>
    {
        /// <summary>
        /// Gets or sets the id for this object (the primary record for an entity).
        /// </summary>
        /// <value>The id for this object (the primary record for an entity).</value>
        [DataMember]
        [BsonRepresentation(BsonType.ObjectId)]
        public virtual string Id { get; set; }
    }
//
///所有业务实体的抽象实体。
/// 
//[数据合同]
[可序列化]
[BsonIgnoreExtraElements(继承=真)]
公共抽象类实体:通用性
{
/// 
///获取或设置此对象(实体的主记录)的id。
/// 
///此对象的id(实体的主记录)。
[数据成员]
[BsonRepresentation(BsonType.ObjectId)]
公共虚拟字符串Id{get;set;}
}
我需要在一个新的带有接口的CommonService(ICommonService)中移动“公共方法”,并获取对象类型并确定IMongoRepository使用什么。。。我想那是我的方式。。。但是我已经读过了,但是我不太理解这些例子,我想知道在我的实际解决方案中实现这一点的最佳方法


非常感谢你

如果我正确理解了你的问题,那么你应该这样做:

首先为DTO创建一个通用接口:

public interface IDtoCommon
{
    ObjectId Id { get; set; }
    int PosLeft { get; set; }
    int PosTop { get; set; }
}
以及实体的通用界面:

public interface IEntityCommon
{
    ObjectId Id { get; set; }
    int PosLeft { get; set; }
    int PosTop { get; set; }
}
下面是您的DTO和实体的外观:

public class ImageDto : IDtoCommon
{
     public ObjectId Id {get; set;}
     public int PosLeft {get; set;}
     public int PosTop {get; set;}
     public ICollection<string> PropertyImage1 {get; set;}
     public string PropertyImage2 {get; set;}
}


public class TextDto : IDtoCommon
{
     public ObjectId Id {get; set;}
     public int PosLeft {get; set;}
     public int PosTop {get; set;}
     public double PropertyText1 {get; set;}
     public string PropertyText2 {get; set;}
}

public class Image : Entity, IEntityCommon
{
    public ObjectId Id { get; set; }
    public int PosLeft { get; set; }
    public int PosTop { get; set; }
    public ICollection<string> PropertyImage1 { get; set; }
    public string PropertyImage2 { get; set; }
    public string OtherPropertyImage { get; set; }
}

public class Text : Entity, IEntityCommon
{
    public ObjectId Id { get; set; }
    public int PosLeft { get; set; }
    public int PosTop { get; set; }
    public double PropertyText1 { get; set; }
    public string PropertyText2 { get; set; }
    public string OtherPropertyText { get; set; }
}
public interface ICommonService<TDto> where TDto : IDtoCommon
{
    Task<string> UpdatePosition(TDto @object);
}

public class CommonService<TDto, TEntity> : ICommonService<TDto>
    where TDto : IDtoCommon
    where TEntity : IEntityCommon
{
    private readonly IRepository<TEntity> m_Repository;

    public CommonService(IRepository<TEntity> repository)
    {
        m_Repository = repository;
    }

    public async Task<string> UpdatePosition(TDto @object)
    {
        var entity = await m_Repository.FindAsync(@object.Id);
        entity.PosLeft = @object.PosLeft;
        entity.PosTop = @object.PosTop;
        await m_Repository.Update(entity);
        return entity.Id.Value; 
    }
}
公共类ImageDto:IDtoCommon
{
公共对象Id{get;set;}
公共int PosLeft{get;set;}
公共int PosTop{get;set;}
公共ICollection PropertyImage1{get;set;}
公共字符串PropertyImage2{get;set;}
}
公共类TextDto:IDtoCommon
{
公共对象Id{get;set;}
公共int PosLeft{get;set;}
公共int PosTop{get;set;}
公共双属性ytext1{get;set;}
公共字符串PropertyText2{get;set;}
}
公共类映像:实体、IEntityCommon
{
公共对象Id{get;set;}
公共int PosLeft{get;set;}
公共int PosTop{get;set;}
公共ICollection PropertyImage1{get;set;}
公共字符串PropertyImage2{get;set;}
公共字符串OtherPropertyImage{get;set;}
}
公共类文本:实体,IEntityCommon
{
公共对象Id{get;set;}
公共int PosLeft{get;set;}
公共int PosTop{get;set;}
公共双属性ytext1{get;set;}
公共字符串PropertyText2{get;set;}
公共字符串OtherPropertyText{get;set;}
}
下面是您的公共服务的样子:

public class ImageDto : IDtoCommon
{
     public ObjectId Id {get; set;}
     public int PosLeft {get; set;}
     public int PosTop {get; set;}
     public ICollection<string> PropertyImage1 {get; set;}
     public string PropertyImage2 {get; set;}
}


public class TextDto : IDtoCommon
{
     public ObjectId Id {get; set;}
     public int PosLeft {get; set;}
     public int PosTop {get; set;}
     public double PropertyText1 {get; set;}
     public string PropertyText2 {get; set;}
}

public class Image : Entity, IEntityCommon
{
    public ObjectId Id { get; set; }
    public int PosLeft { get; set; }
    public int PosTop { get; set; }
    public ICollection<string> PropertyImage1 { get; set; }
    public string PropertyImage2 { get; set; }
    public string OtherPropertyImage { get; set; }
}

public class Text : Entity, IEntityCommon
{
    public ObjectId Id { get; set; }
    public int PosLeft { get; set; }
    public int PosTop { get; set; }
    public double PropertyText1 { get; set; }
    public string PropertyText2 { get; set; }
    public string OtherPropertyText { get; set; }
}
public interface ICommonService<TDto> where TDto : IDtoCommon
{
    Task<string> UpdatePosition(TDto @object);
}

public class CommonService<TDto, TEntity> : ICommonService<TDto>
    where TDto : IDtoCommon
    where TEntity : IEntityCommon
{
    private readonly IRepository<TEntity> m_Repository;

    public CommonService(IRepository<TEntity> repository)
    {
        m_Repository = repository;
    }

    public async Task<string> UpdatePosition(TDto @object)
    {
        var entity = await m_Repository.FindAsync(@object.Id);
        entity.PosLeft = @object.PosLeft;
        entity.PosTop = @object.PosTop;
        await m_Repository.Update(entity);
        return entity.Id.Value; 
    }
}
公共接口ICommonService,其中TDto:IDtoCommon
{
任务更新位置(TDto@object);
}
公共类CommonService:ICommonService
其中TDto:IDtoCommon
地点:IEntityCommon
{
私有只读IRepository存储库;
公共公共服务(IRepository存储库)
{
m_Repository=存储库;
}
公众的