在C#中使用带有依赖项注入的泛型类

本文关键字:依赖 注入 泛型类 | 更新日期: 2023-09-27 18:27:06

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

例如,我有以下接口:

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);
}

在订单中,我有实现接口的类(只是添加了重要的代码)和构造函数:

//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;
}

UpdateTextPosition和UpdateImagePosition做的事情完全相同,但在不同的集合中(或者使用不同的IRepository实例,我使用的是MongoDB):

public async Task<string> UpdateTextPosition(TextDto object)
{
     var text = await _repo.FindAsync(object.Id);
     text.PosLeft = object.PosLef;
     text.PosRight = object.PosRight;
     await _repo.Update(text); 
     return text.Id; 
}
public async Task<string> UpdateImagePosition(ImageDto object)
{
     var image = await _repo.FindAsync(object.Id);
     image.PosLeft = object.PosLef;
     image.PosRight = object.PosRight;
     await _repo.Update(image);  
     return image.Id
}

编辑:(添加ImageDto和TextDto)

public class ImageDto
{
     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
{
     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
    {
         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
{
     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;}
}

以及实体:

/// <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; }
    }

我需要在一个新的带有接口(ICommonService)的CommonService中移动"通用方法",并获取对象类型并确定IMongoRepository使用什么。。。我认为通用是我的方式。。。但我读过它,但我不太理解这些例子,我想在我的实际解决方案中找到实现它的最佳方式。

非常感谢!!

在C#中使用带有依赖项注入的泛型类

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

首先为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; 
    }
}

这里有一个如何使用它的例子:

CommonService<TextDto, Text> service = new CommonService<TextDto, Text>(new Repository<Text>());
service.UpdatePosition(text_dto);

您应该考虑您的代码中是否有足够的重复来证明这样做是合理的。