WCF KnownType Inputs Entity Framework - c#

I am trying to design a webservice that use the Knowntype attribute for the Derived class.
The reason i am using the knowtype is to have a single endpoint for all derived classes.
[DataContract]
[KnownType(typeof(Derived))]
[KnownType(typeof(DerivedTwo))]
//And other derived types
class Base
{
string Name {get; set;}
}
[DataContract]
class Derived :Base
{
string WorkedOn {get; set;}
}
[DataContract]
class DerivedTwo :Base
{
string CompletedOn {get; set;}
}
public class TestContext :DbContext
{
//ctor of context
public Dbset<Base> Base {get; set;}
public Dbset<Derived> Base {get; set;}
}
public class Repository<T> where T:Base
{
private readonly TestContext _testContext;
public Repository()
{
_testContext = new TestContext();
}
public void Add(T input)
{
var kew = _testContext.Set<T>().Add(input);
}
public void Save()
{
_testContext.SaveChanges();
}
}
public Interface IService
{
[OperationContract]
void Add(Base base);
}
public class Service :IService
{
public void Add(Base base)
{
var repository = new Repository<typeof(base)>();
repository.Add(base);
repository.Save();
}
}
When the client calls this endpoint and passes a derived class
i want a entry to be added to the derived class table (TPC -- table for concrete class)
Is this even possible???
what are my potions to design a webservice like this.
Edited to answer
I am pretty new i do not know where i am supposed reply and the edit time for the comment below expired so i am doing it here.
I tried what you have suggested and EF is inserting the properties from the Base class into the Base class table and from the derived class in to the corresponding table. Is there some way i can force it to insert all of the data into the derived table.
Edit2
I figure out the answer with the help of google i need to use the MapInheritedProperties on the DbSet.
Thank You for you help i will mark this answered

The only thing that's wrong with your existing code is that var repository = new Repository<typeof(base)>(); is not a valid form of a closed generic.
Given what you've shown, the design of your repository class is suspect - it would make more sense implemented as
public class Repository
{
private readonly TestContext _testContext;
public Repository()
{
_testContext = new TestContext();
}
public void Add<T>(T input) where T : Base
{
var kew = _testContext.Set<T>().Add(input);
}
public void Save()
{
_testContext.SaveChanges();
}
}
Which would allow your service implementation to be
public void Add(Base input)
{
var repository = new Repository();
repository.Add(input);
repository.Save();
}
and work as expected.

Related

Accessing virtual method from abstract class using repository pattern

I'm stuck in understanding - how to access and use (or is it even possible to use) base class virtual method.
So the code is:
Base class:
public abstract class Vehicle
{
public string VehicleIdentificationNumber { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public abstract string DisplayName { get; }
public virtual bool HasAnAmazingColor(){
}
}
Repository pattern:
public class VehicleRepository : IVehicleRepository, ICollection
{
private readonly List<Vehicle> _vehicles;
public int Count => _vehicles.Count;
public object SyncRoot => ((ICollection)_vehicles).SyncRoot;
public bool IsSynchronized => ((ICollection)_vehicles).IsSynchronized;
public void CopyTo(Array array, int index)
{
((ICollection)_vehicles).CopyTo(array, index);
}
public IEnumerator GetEnumerator()
{
return ((ICollection)_vehicles).GetEnumerator();
}
public VehicleRepository(List<Vehicle> aVehicles)
{
_vehicles = aVehicles;
}
Then int unittest I try to get the virtual method, but do know that I'm not understanding something, but cannot figure out what, how can I use the method without overriding it?
[TestClass()]
public class VehicleRepositoryTests
{
private VehicleRepository vehicleList = new VehicleRepository(new List<Vehicle>());
[TestMethod()]
public void HasAmazingColor()
{
//arrange
//act
vehicleList.??? -- I'm missing something
//assert
}
I can access virtual method in any of the derived class that implements Vehicle, but is there a way to use it in repository pattern?
Well, you have abstract base calls Vehicle.
From careful observation of your code, I found that you have not derived your VehicleRepository Class from Vehicle.
So you will not be able to access the virtual method from Vehicle on VehicleRepository instance.
The method you are looking for HasAmazingColor will be available on every single object in List<Vehicle> _vehicles;
May be you can expose the List of Vehicles as property and then use it in the unit test, just in case you want to use it.
Or
If your design does not allow to explose the _vehicles as public collection, then you can have a public method in VehicleRepository class which internally calls the HasAmazingColor method on appropriate Vehicle object or objects.
The solution will depend on your application design.

EntityObject to DbContext

I am not sure in terms of exact technical specification for this problem to me but in simple words I am trying to create a wrapper/extension method around to save my entities.
So I added new Entity Data Model (.edmx) file to my project. That generates DbSet(s) like this-
public partial class SUContextContainer : DbContext
{
public SUContextContainer()
: base("name=SUContextContainer")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Category> Categories { get; set; }
public DbSet<Gallery> Galleries { get; set; }
public DbSet<SuperUser> SuperUsers { get; set; }
public DbSet<UserType> UserTypes { get; set; }
}
Now here I am trying to wrap this into an extension method for database operations like (save, delete, update etc..)
I tried creating it as -
public static void Save(this EntityObject objEntity)
{
try // Update Record
{
((IObjectContextAdapter)Global.Context).ObjectContext.ObjectStateManager.ChangeObjectState(objEntity, EntityState.Modified);
Global.Context.SaveChanges();
}
catch (OptimisticConcurrencyException) // Insert Record
{
((IObjectContextAdapter)Global.Context).ObjectContext.ObjectStateManager.ChangeObjectState(objEntity, EntityState.Added);
Global.Context.SaveChanges();
}
}
This method is attached to EntityObject types. Where .edmx code which it generates are of type DbContext.
So Whenever I try to save some entity with this helper method it never finds out.
var galleryEntity = new Gallery {
IsActive = true,
CategoryId = model.CategoryId,
};
galleryEntity.Save(); // the save method is not found.
I tried above method to change in -
public static void Save(this DbSet objEntity)
But this also doesn't seem to take as extension method.
What am I doing wrong.
So Whenever I try to save some entity with this helper method it never
finds out.
It will not, because gallery is just a class and is not inherited from EntityObject.
I don't suggest adding inheritence or modifiying autogenerated classes.
Use power of partial classes:
You can create patial classess for your models with interface.
public partial class Gallery : IEntity
{
//This is your class different than auto generated class by Ef.
}
Also you shouldn't use try catch for decision. That's why you should seperate update and create and make decision on upper level (without try catch).
So your extension methods should be like this.
public static int Update<T>(this T entity) where T : IEntity
{
using(var dbContext=new SUContextContainer())
{
var entry = dbContext.Entry(entity);
dbContext.Set<T>().Attach(entity);
entry.State = EntityState.Modified;
return dbContext.SaveChanges();
}
}
public static int Create<T>(this T entity) where T : IEntity
{
using(var dbContext=new SUContextContainer())
{
dbContext.Set<T>().Add(entity);
return dbContext.SaveChanges();
}
}
Your extension method will only apply to types that inherit from EntityObject.
You will either need to make all of your entity classes inherit from this EntityObject class or create another extension method that applies to the correct type.
Typically when using these kind of persistence patterns you would create an entity base class
public class Entity
{
public int Id { get; set; }
}
and each entity type inherits from it
public class Gallery : Entity
{
public int Name { get; set; }
}
Then you can have common methods that you use across entity types:
public static void Save(this Entity entity);

Refactor my code : Avoiding casting in derived class

Firstly, I feel sorry about the title, I do not know how to describe my problem exactly. I hope it will be better explained through the code.
public abstract class AB {
public MyModel Model;
}
public class A : AB {
public A() {
Model = new MyModelA();
}
public void AMethod() {
var model = (MyModelA) model; // I have to do this all place
}
public void AnotherMethod() {
var model = (MyModelA) model; // same here
model.NewInt = 123;
}
}
public abstract class MyModel {
}
public class MyModelA : MyModel {
// new properties
public int NewInt {get;set;}
}
Take a look at the code, in order to use new properties from derived class, I have to do a cast but it is ugly when I have to use it same time all over places.
The method I think is declare another property: public MyModelA _tmp then I cast it in the constructor _tmp = (MyModelA) Model and use it instead of Model.
Are there any other appropriate ways to do this ?
Thanks !
You can make the base class generic:
public abstract class ServiceBase<TModel> where TModel : new() {
protected ServiceBase() { Model = new TModel(); }
public TModel Model { get; private set; }
}
public class AService : ServiceBase<MyModelA> {
...
}
You can maintain your Model reference in the derived class:
public abstract class AB {
public MyModel Model;
}
public class A : AB {
MyModel MyModel;
public A() {
MyModel = new MyModelA();
Model = MyModel;
}
public void AMethod() {
//just use MyModel
}
public void AnotherMethod() {
MyModel.NewInt = 123;
}
}
public abstract class MyModel {
}
public class MyModelA : MyModel {
// new properties
public int NewInt {get;set;}
}
The solution with _tmp rids you of having to write that manual cast all the time, but the problem of a strange object design remains.
I would guess your NewInt is there to perform some sort of functionality that was also present in MyModel (otherwise you'd be better off creating a new class for that to begin with). I'm wondering if you can't encapsulate that functionality in a way that MyModelA does not have to expose anything new. This may mean changing the definition of AB in order to allow for such generalizations.
The answer, I believe, is neither syntactic nor easily found in a OOP pattern without understanding the domain. Maybe you can provide some details on that.

C#: Generic Inherited Factories

I have a base class that takes a single generic argument. I then have several classes that inherit from this base class. Is there a simple way for the child classes to inherent a factory from the base class?
Example
class BaseClass<T>
{
T Value {get; set;}
string Name {get; set;}
public static BaseClass<T> Factory(T Value)
{
return new BaseClass<T>(Value);
}
}
class ChildClass : BaseClass<int>
{
public void Test()
{
// I want this below to work
// but Factory() returns a BaseClass
ChildClass bs = ChildClass.Factory(10);
}
}
I've noted in the code what I want to work. I can think of one way to overcome this, by adding an implicit operator to either BaseClass or SubClass that converts from BaseClass to ChildClass.
I can also just explicitly add the Factory to ChildClass but that defeats the point of inheritance.
Is there a better, more standardized way of doing this?
I would do something like this:
class BaseClass<T, K> where K : BaseClass<T, K>, new()
{
T Value { get; set; }
string Name { get; set; }
public static K Factory(T value)
{
return new K { Value = value };
}
}
class ChildClass : BaseClass<int, ChildClass>
{
public void Test()
{
ChildClass cs = Factory(10);
}
}
It's a bit hard to answer your question since you have described what you are trying to do, but not why. Hence I got to try to guess what you want.
I would not put the factory method in the same class as in the other answer or your question. How would you handle inheritance for once? It works for the two levels that you have. But what if you want to extend ChildClass?
Instead I would create a generic factory used for the object creation. Implement it has a singleton wrapped around a factory interface to be able to easy extend it or swap the implementation.
class MyFactory
{
private static IMyFactory _instance;
public static void Assign(IMyFactory factory) { _instance = factory; }
public static T Create<T>() { return _instance.Create<T>(); }
}
interface IMyFactory
{
T Create<T>();
}
class MyFactoryImp : IMyFactory
{
//do whatever needed in here
public T Create<T>(){ return new T(); }
}
class BaseClass<T>
{
T Value {get; set;}
string Name {get; set;}
}
class ChildClass : BaseClass<int>
{
public void Test()
{
ChildClass bs = MyFactory.Create<ChildClass>(10);
}
}
// start with this, you can easily switch implementation
MyFactory.Assign(new MyFactoryImp());
The other obvious answer would be to start using a Inversion Of Control container, for example autofac.

IDescription interface using generics and extension methods

I am trying to implement an IDescription Interface. Basic purpose of this interface is that I have many different classes that have a list of multilingual descriptions and I want the the basic AddDescription EditDescription and some other basic behaviours to be defined by the interface and not implemented by the classes individually that inherits the interface. I am trying to assign the behavior to the interface using extension methods.
I have some road blocks such as how do I access the descriptions collection of the entity that I am passing on to the IDescription interface (entity.Descriptions.Add)?
I am very new to generics, extension methods, anonymous types etc so please bear with me with my misunderstandings of how these are used. Will appreciate if you can help me correct the below code. I wrote it to give the idea of what I am trying to achieve, it obviously fundamental errors in it. Thanks
public class Company : IDescription<Company, CompanyDescription>
{
public IList<CompanyDescription> Desriptions { get; set; }
}
public class Location : IDescription<Location, LocationDescription>
{
public IList<LocationDescription> Desriptions { get; set; }
}
public interface IDescription<eT, dT>
{
void AddDescription(eT, string text);
void EditDescription(eT, dT, string text);
}
public static DescriptionInterfaceExtensions
{
public static void AddDescription(this IDescription<eT, dT> description, eT entity, string text)
{
dT newDescription = new dT(text);
entity.Descriptions.Add(newDescription);
}
}
Another possible rewrite that should work is to remove the Add/Edit methods from the interface and simply provide the required IList in the interface. Then, for ease of use, you can use the extension methods to make it easier.
I'm not saying this example is a great use of generics or extension methods, but it will work:
public class CompanyDescription : IDescription { public string Text { get; set; } }
public class LocationDescription : IDescription { public string Text { get; set; } }
public class Company : IHaveDescriptions<CompanyDescription>
{
public IList<CompanyDescription> Desriptions { get; set; }
}
public class Location : IHaveDescriptions<LocationDescription>
{
public IList<LocationDescription> Desriptions { get; set; }
}
public interface IDescription
{
string Text { get; set; }
}
public interface IHaveDescriptions<T>
where T : class, IDescription, new()
{
IList<T> Desriptions { get; set; }
}
public static class DescriptionInterfaceExtensions
{
public static void AddDescription<T>(this IHaveDescriptions<T> entity, string text)
where T : class, IDescription, new()
{
T newDescription = new T();
newDescription.Text = text;
entity.Desriptions.Add(newDescription);
}
public static void EditDescription<T>(this IHaveDescriptions<T> entity, T original, string text)
where T : class, IDescription, new()
{
T newDescription = new T();
newDescription.Text = text;
entity.Desriptions.Remove(original);
entity.Desriptions.Add(newDescription);
}
}
I your example it seems the contract is that objects that have a description store a list of descriptions. So, to avoid having to declare Add and Remove methods in the classes directly, you could do something like this:
Interfaces
public interface IDescription<T>
{
}
public interface IHasDescription<THasDescription, TDescription>
where THasDescription : IHasDescription<THasDescription, TDescription>
where TDescription : IDescription<THasDescription>
{
IList<TDescription> Descriptions { get; }
}
Concrete implementations
public class CompanyDescription : IDescription<Company>
{
}
public class Company : IHasDescription<Company, CompanyDescription>
{
private readonly IList<CompanyDescription> descriptions;
public IList<CompanyDescription> Descriptions
{
get { return this.descriptions; }
}
}
Extension methods
public static class DescriptionExtensions
{
public static void AddDescription<THasDescription, TDescription>(
this THasDescription subject,
TDescription description)
where THasDescription : IHasDescription<THasDescription, TDescription>
where TDescription : IDescription<THasDescription>
{
subject.Descriptions.Add(description);
}
}
But I don't think it's worth to do this just to have
mycompany.AddDescription(mydescription);
instead of
mycompany.Descriptions.Add(mydescription);
You can't add interface implementations to classes using extension methods, although that seems to be what you are trying to do.
The purpose of extension methods is to add behavior to existing types (classes or interfaces).
The problem with your code is that you declare that Company and Location should implement the IDescription interface; yet they don't. They have no AddDescription or EditDescription methods, so that's not going to work.
Why don't you instead define a concrete generic Description class and attach that class to Company and Location?

Categories