jeudi 13 mai 2021

Using AbstractFactory

I'm working on some project on which need to track final product of specific Animal. There is much more in background, like feeding, vaccines etc... In that project for keeping the code clean and easy for understanding and maintenance I had got idea to use Abstract Factory as solution. I had prepare something, but I would like to hear opinions from you much more experienced people if I'm on good way or not. Here it is my code:

  public interface IAnimal
    {
        void CreateAnimal(string code);
        Guid GetAnimalIdByCode(string code);
    }
    public interface IAnimalFactory
    {
        IAnimal CreateAnimal();
    }
    public class ICowFactory : IAnimalFactory
    {
        public IAnimal CreateAnimal()
        {
            return new Cow();
        }
    }
    public class CowFactory : IAnimalFactory
    {
        public IAnimal CreateAnimal()
        {
            return new Cow();
        }
    }
    public class Cow : IAnimal
    {
        public Guid Id { get; set; }
        public string Code { get; set; }


        public void CreateAnimal(string code)
        {
            Id = Guid.NewGuid();
            Code = code;
            //Should I here use DbContext for adding in DB ?
        }

        public Guid GetAnimalIdByCode(string code)
        {
            //Should I here use DbContext for searching ?
            return Id;
        }
    }

here Cow will be used as Entity in ApplicationDbContext from which will be generated Migration, is that okay ? Thanks and best regards. Stay healthy. Danijel

Aucun commentaire:

Enregistrer un commentaire