I need to make a commons methods to my services, and use a Repository or other depending of the object type I am passing as argument.
For example, I have these interfaces:
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);
}
In the order hand, I have the classes that implements the Interfaces (just important code added) and with the constructors:
//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;
}
The UpdateTextPosition and UpdateImagePosition do exactly the same thing, but in differents collections (or using differents IRepository instances, I'm using 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
}
EDIT: (added ImageDto and 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;}
}
And the 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>
{
}
And Entity class:
/// <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; }
}
I need to move the "common methods" in a new CommonService with an Interface (ICommonService), and take the object type and identify what IMongoRepository use... I think that Generic is my way... but I have read about it, but I don't understand very well the examples and I would like to the best way to implement this in my real solution.
Thank you soooo much!!
UpdateOtherThingOnTextandUpdateOtherThingOnImage)? or just the methods you provided? Can you show the definition ofImageDtoandTextDto?UpdatePositionthat works on bothImageDtoandTextDtoand maybe more other DTOs, right?TextandImage?