使用带有C#的依赖注入的通用类

时间:2015-12-15 21:48:23

标签: c# generics dependency-injection asp.net-core

我需要为我的服务创建一个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;}
}

和IEntity:

/// <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使用的内容...我认为Generic是我的方式......但是我已经阅读了它,但我不太了解这些例子,我想在我的真实解决方案中实现这一点。

非常感谢你!

1 个答案:

答案 0 :(得分:3)

如果我理解你的问题,那么你应该做一些看起来像这样的事情:

首先为您的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);

你应该考虑一下你的代码是否有足够的重复来证明这一切。