how to design an interface for different object to using them in a builder design pattern? - oop

I have at least two different classes like following :
//NOTE : these two classes have getter and setter also
class Artist {
String artistName;
String artistWebsite;
String artistDbpedia;
String artistImage;
List<String> astistAlbumsName;
List<String> astistAlbumsUrl;
}
class Venu {
String VenuName;
String VenuWebsite;
String VenuDbpdia;
String VenuImage;
String VenuDescription;
List<String> venuFans;
}
I want to have a producer class to get an xml file as an input and detect the type of xml (venu/artist) then start to create a product object based on the input.
the problem :
I want to create an interface for aggregate the similarity between above two classes so my interface would be:
interface Model {
public String getImage();
public String getName();
public String getWebsite();
public String getdbpedia();
}
Then I can implement this interface in my builder class and above two classes but how about those different methods?
such as getVenuFans / getArtistAlbumName / etc....?
How can I call them from my producer?
this is my builder :
Class Builder implements Model {
public String getImage(){}
public String getName(){}
public String getWebsite(){}
public String getdbpedia(){}
}
and this can be my producer :
Class Producer {
public Producer()
{
Builder b = null;
//assume Venu and Artist implements Model
b = (Builder) new Venu();
//I don't have access to getVenuFans()!
b = (Builder) new Artist();
//I don't have access to getArtistAlbumsName() / etc...
}
}

You don't have access to those methods because you're casting the objects to a Builder, and Builder doesn't have those methods.
I see what you're trying to do, but I don't think it will work. For example, getVenueFans (I'm assuming you mean venue) is only appropriate for the Venue class. It doesn't make sense to try and abstract that into an interface that other non-Venue classes will implement.
I think what you have is good: You've abstracted the common methods into an interface. To call the methods on Venue and Artist, the consuming code will need to cast the objects to the appropriate type, then call the methods on it. And that's not as bad as you might think. It's the consuming code that knows what type it's dealing with (otherwise, why would it be trying to call getVenueFans?), so that's the point where it makes sense to cast and call the method directly.

Related

DDD ValueObject and Enumeration , is there any good way to implement serialization?

In DDD, Value Object and Enumeration are quite beautiful so that I want use both two in the daily program logic, not only domain logic. When use customized value objects and enumerations, serialization problem is coming : should I implemented all the value objects and enumeration with System.Text.Json.JsonConverter<T> , or is there any good way to handle serialization and deserialization ?
Update:
to make it clear, Eumeration demo as below(ValueObject derived classes are same.):
[JsonConverter(typeof(CustomizedConverter))]
public class CustomizedEnumeration1 : Enumeration
{
public string Customized { get; protected set; }
public ... // some other customized property or class
public CustomizedEnumeration(int id, string name, string customized) : base(id, string) {
Customized = customized;
}
}
public class Customized2 : Enumeration
{ ... }
public class OtherCustomized: Enumeration
{ ... }
In DDD, properties sometimes are sealed by protected/private setter, deserialization has no right to set the value. Many derived classes can't deserialize as expected, so we have to rewrite serialization with System.Text.Json.JsonConverter<T> one by one. rewrite every derived Enumeration / Valueobject converter is not good, can any one point out any easy abstraction for that ?
You can achieve your desired result. You need to switch to NewtonsoftJson serialization.
Call this in Startup.cs in the ConfigureServices method:
services.AddControllers().AddNewtonsoftJson();
After this, your constructor will be called by deserialization for classes with private setter.
There is no need for custom converters.
For reference, I am using ASP Net Core 3.1

enum vs Interface design

I have a design problem where the requirement is something like this :
Write a generate function that takes a parameter("TYPE")
Depending on the TYPE, I need to generate a String and return it. So TYPE effectively changes the way you generate the String.
I am deliberating between two design options :
Using enum : Create a enum having the TYPES. Then provide a generate method that depending on TYPE does the processing and returns a string.
Using Interface : Create an interface having a function generate(). Create implementations for each TYPE, to implement the generate().
Which do you feel is better and for what reasons.
Although, Approach # 2, follows the Open/Closed Principle of OOAD i.e You will be adding new interface implementation, everytime new TYPE is added and you will not modify existing code, which is very safe approach as it does not need testing of old code/method. So your code will be open for extension but closed for modification. However, if you are going to very frequently add new TYPE, then Approach # 2, makes sense.
IMO, in this case, I would suggest to use Approach # 1, as the business requirement is really simple i.e to generate a String based on Parameter TYPE. So using interface will be over-engineering in my opinion(if TYPES are not going to be added frequently).
It will be good to use some design pattern for this problem statement to make your code more robust and reusable. I will suggest to you Strategy Design Pattern. It is abstraction based pattern that uses Interface.
Basic Example:
public interface IMyStrategy
{
string Generate(string someValue);
}
public class StragegyA : IMyStrategy
{
public string Generate(string somevalue)
{
return /Implementation/;
}
}
public class StragegyB : IMyStrategy
{
public string Generate(string somevalue)
{
return /Implementation/;
}
}
public class MyStrategyContext
{
private readonly IMyStrategy _ImyStrategy;
public MyStrategyContextIMyStrategy(IMyStrategy myStragegy)
{
_ImyStrategy = myStragegy
}
public string GenerateResult(string someValue)
{
return _ImyStrategy .Generate(someValue);
}
}
[Test]
public void GenerateValue()
{
var abc = new MyStrategyContext(new StragegyA());
abc.GenerateResult("hey print");
}

Orika - how to map inheritance and flattening?

I have been struggling with a task how to tell Orika to map an inherited structure that is flattened to DTO so that it may correctly resolve the implementation on reconstruction of an object. Here is an example of a simple structure with many nested objects:
abstract class Document {
// common values
}
class LegalDocument extends Document {
// complex object with many nested objects
}
class PersonalDocument extends Document {
// complex object with many nested objects
}
And let's say I have a reason to have an object flattened of the structure above:
class FlattenedDocument {
private String documentType = "LEGAL"; // "LEGAL" or "PERSONAL"
// flattened properties of Document and both its subclasses
}
I am able to tell Orika via CustomMapper<Document, FlattenedDocument> to map correctly the property documentType with a correct value based on an actual type (class) of the input document, but what I don't know how to do is the reverse situation. I need to tell Orika that when it converts from FlattenedDocument to one of the implementations of abstract Document, whether it should create the former or the latter by the value of documentType property. I can do that via CustomConverter or ObjectFactory but in both cases I am losing the benefit of byDefault().
Is there any way how to use the standard ClassMap with byDefault() option
factory.classMap(Document.class, FlattenedDocument.class).byDefault().register();
but with the possibility to tell Orika that it should re-instantiate the object based on the value of documentType field?
Thanks.
You can create a CustomConverter, that decides the type based on your field:
public class ShapeReverseConverter extends CustomConverter<ShapeDTO, Shape> {
#Override
public Shape convert(ShapeDTO source, Type<? extends Shape> destinationType, MappingContext mappingContext) {
if (Circle.class.getSimpleName().equals(source.type)) {
return mapperFacade.map(source, Circle.class);
} else {
return mapperFacade.map(source, Rectangle.class);
}
}
}
In Config you can map setting the type:
DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(Shape.class, ShapeDTO.class).byDefault()
.field("class.simpleName", "type")
.register();
mapperFactory.getConverterFactory().registerConverter(new ShapeReverseConverter());

Inheriting ConstructorArguments in Ninject

I'm trying to find a method of passing a constructor argument to the constructors of child classes.
These objects are immutable so I'd prefer to use constructor arguments.
The issue I have encountered is that ConstructorArgument does not inherit to child instantiations and the following statements are not interchangeable:
_parsingProcessor = _kernel.Get<IParsingProcessor>(new ConstructorArgument("dataFilePath", dataFilePath);
and
_parsingProcessor = _kernel.Get<IParsingProcessor>(new Parameter("dataFilePath", dataFilePath, true);
So, how can get an inheritable ConstructorArgument and when does it makes sense, if ever, to new the Parameter class?
Yes, you can do this, but it's probably not what you really want. If the container is not actually responsible for instantiating its own dependencies, then its dependencies probably shouldn't be sharing its constructor arguments - it just doesn't make sense.
I'm pretty sure I know what you're trying to do, and the recommended approach is to create a unique binding specifically for your one container, and use the WhenInjectedInto conditional binding syntax, as in the example below:
public class Hello : IHello
{
private readonly string name;
public Hello(string name)
{
this.name = name;
}
public void SayHello()
{
Console.WriteLine("Hello, {0}!", name);
}
}
This is the class that takes a constructor argument which we want to modify, depending on who is asking for an IHello. Let's say it's this boring container class:
public class MyApp : IApp
{
private readonly IHello hello;
public MyApp(IHello hello)
{
this.hello = hello;
}
public virtual void Run()
{
hello.SayHello();
Console.ReadLine();
}
}
Now, here's how you do up the bindings:
public class MainModule : NinjectModule
{
public override void Load()
{
Bind<IApp>().To<MyApp>();
Bind<IHello>().To<Hello>()
.WithConstructorArgument("name", "Jim");
Bind<IHello>().To<Hello>()
.WhenInjectedInto<MyApp>()
.WithConstructorArgument("name", "Bob");
}
}
Basically all this binding is doing is saying the name should be "Jim" unless it's being requested by Hello, which in this case it is, so instead it will get the name "Bob".
If you are absolutely certain that you truly want cascading behaviour and understand that this is very dangerous and brittle, you can cheat using a method binding. Assuming that we've now added a name argument to the MyApp class for some unspecified purpose, the binding would be:
Bind<IHello>().ToMethod(ctx =>
ctx.Kernel.Get<Hello>(ctx.Request.ParentContext.Parameters
.OfType<ConstructorArgument>()
.Where(c => c.Name == "name")
.First()));
Please, please, make sure you are positive that this is what you want before doing it. It looks easy but it is also very likely to break during a simple refactoring, and 95% of the "customized dependency" scenarios I've seen can be addressed using the WhenInjectedInto binding instead.

Is it possible to serialize objects without a parameterless constructor in WCF?

I know that a private parameterless constructor works but what about an object with no parameterless constructors?
I would like to expose types from a third party library so I have no control over the type definitions.
If there is a way what is the easiest? E.g. I don't what to have to create a sub type.
Edit:
What I'm looking for is something like the level of customization shown here: http://msdn.microsoft.com/en-us/magazine/cc163902.aspx
although I don't want to have to resort to streams to serialize/deserialize.
You can't really make arbitrary types serializable; in some cases (XmlSerializer, for example) the runtime exposes options to spoof the attributes. But DataContractSerializer doesn't allow this. Feasible options:
hide the classes behind your own types that are serializable (lots of work)
provide binary formatter surrogates (yeuch)
write your own serialization core (a lot of work to get right)
Essentially, if something isn't designed for serialization, very little of the framework will let you serialize it.
I just ran a little test, using a WCF Service that returns an basic object that does not have a default constructor.
//[DataContract]
//[Serializable]
public class MyObject
{
public MyObject(string _name)
{
Name = _name;
}
//[DataMember]
public string Name { get; set; }
//[DataMember]
public string Address { get; set; }
}
Here is what the service looks like:
public class MyService : IMyService
{
#region IMyService Members
public MyObject GetByName(string _name)
{
return new MyObject(_name) { Address = "Test Address" };
}
#endregion
}
This actually works, as long as MyObject is either a [DataContract] or [Serializable]. Interestingly, it doesn't seem to need the default constructor on the client side. There is a related post here:
How does WCF deserialization instantiate objects without calling a constructor?
I am not a WCF expert but it is unlikely that they support serialization on a constructor with arbitrary types. Namely because what would they pass in for values? You could pass null for reference types and empty values for structs. But what good would a type be that could be constructed with completely empty data?
I think you are stuck with 1 of 2 options
Sub class the type in question and pass appropriate default values to the non-parameterless constructor
Create a type that exists soley for serialization. Once completed it can create an instance of the original type that you are interested in. It is a bridge of sorts.
Personally I would go for #2. Make the class a data only structure and optimize it for serialization and factory purposes.